Skip to main content
PostgreSQL is a powerful, advanced open-source relational database with excellent support for complex queries, JSON data, and spatial operations. This guide covers the complete installation process for the Countries States Cities database.
PostgreSQL installation typically takes 3-6 minutes and requires approximately 220MB of disk space.

Prerequisites

Before starting, ensure you have:
  • PostgreSQL 10+ installed (PostgreSQL 13+ recommended)
  • Administrative access to your PostgreSQL server
  • At least 500MB free disk space
  • Downloaded the pgsql/world.sql file from our GitHub repository

Method 1: Command Line Import

1

Create Database

Or using psql:
PostgreSQL uses UTF-8 encoding by default, which properly handles all international characters.
2

Import Database

PostgreSQL import includes automatic index creation for optimal performance.
3

Verify Installation

Expected output:

Method 2: pgAdmin GUI

For users who prefer a graphical interface:
1

Connect to Server

Open pgAdmin and connect to your PostgreSQL server.
2

Create Database

  • Right-click on “Databases”
  • Select “Create” → “Database”
  • Name: “world”
  • Encoding: “UTF8”
  • Template: “template0”
3

Import SQL File

  • Right-click on the “world” database
  • Select “Query Tool”
  • Click the folder icon to open file
  • Select pgsql/world.sql
  • Execute the script (F5)

PostgreSQL-Specific Features

Spatial Queries with PostGIS

1

Enable PostGIS Extension

2

Create Spatial Indexes

3

Example Spatial Queries

PostgreSQL provides powerful full-text search capabilities:

Advanced Configuration

Create Application User and Database

1

Create Role

Replace ‘secure_password_here’ with a strong, unique password.
2

Grant Permissions

Performance Optimization

Database Structure Verification

Connection Examples

JSON Support

PostgreSQL has excellent JSON support. Here are some examples:

Troubleshooting

Problem: could not connect to server: Connection refusedSolutions:
  1. Check if PostgreSQL service is running: sudo systemctl status postgresql
  2. Verify PostgreSQL is listening on correct port: netstat -an | grep 5432
  3. Check pg_hba.conf for connection permissions
  4. Ensure firewall allows connections on port 5432
Problem: FATAL: password authentication failedSolutions:
  1. Verify username and password are correct
  2. Check pg_hba.conf authentication method
  3. Reset password: ALTER USER username PASSWORD 'newpassword';
  4. Ensure user has login privileges: ALTER USER username LOGIN;
Problem: ERROR: permission denied for table countriesSolutions:
  1. Grant table permissions: GRANT SELECT ON ALL TABLES IN SCHEMA public TO username;
  2. Check current permissions: \dp in psql
  3. Ensure user has schema usage: GRANT USAGE ON SCHEMA public TO username;
  4. Grant database connection: GRANT CONNECT ON DATABASE world TO username;
Problem: Queries are running slowlySolutions:
  1. Add appropriate indexes (see Performance Optimization section)
  2. Update table statistics: ANALYZE;
  3. Check query plan: EXPLAIN ANALYZE your_query;
  4. Increase shared_buffers in postgresql.conf
  5. Consider using connection pooling (pgbouncer)

Backup and Maintenance

Create Backup Script

Regular Maintenance

PostgreSQL’s autovacuum process handles most maintenance automatically, but manual VACUUM ANALYZE can help after large data changes.

Next Steps

After successful installation:
  1. Configure connection pooling with pgbouncer for production
  2. Set up monitoring with pg_stat_monitor or similar tools
  3. Implement backup strategy using the provided script
  4. Explore spatial features with PostGIS extension
  5. Optimize queries using EXPLAIN ANALYZE

Need Help?

Join our community discussions for PostgreSQL-specific questions and advanced optimization tips.