How to fix Unable to establish connection in PostgreSQL

PostgreSQLINTERMEDIATEHIGHUpdated Dec 28, 2025ยท 128 views

PostgreSQL connection failures occur when the database server is unavailable, misconfigured, or unreachable. Fix it by verifying the service is running, checking network connectivity, and validating authentication settings.

Quick fix

Fixes Unable to establish connection

sudo systemctl status postgresql
  1. Verify PostgreSQL service is running
  2. Check PostgreSQL is listening on the correct port
  3. Validate listen_addresses configuration
  4. Check pg_hba.conf authentication rules
postgresql โ€” error
sudo systemctl status postgresql
Unable to establish connection

What this error means

The "Unable to establish connection" error indicates that your client cannot reach the PostgreSQL server. This can happen for several reasons: the PostgreSQL service may not be running, the server might be listening on a different port or address than expected, firewall rules may be blocking access, authentication credentials may be incorrect, or network connectivity issues may prevent communication. This is a critical error because without a database connection, your application cannot function.

Typical symptoms

  • Application fails to start with database connection error
  • psql command-line tool hangs or immediately fails
  • Connection timeout errors after a few seconds
  • Remote applications cannot reach PostgreSQL on different machines
  • Local connections work but remote connections fail

Common causes

  1. PostgreSQL service is not running on the target machine
  2. Server is listening on wrong port (not default 5432)
  3. listen_addresses in postgresql.conf does not include the connecting host IP
  4. Firewall rules block TCP port 5432
  5. pg_hba.conf authentication method does not allow the connection
  6. Network routing or DNS issues prevent reaching the server
  7. Too many existing connections (max_connections limit reached)
  8. Incorrect hostname or IP address in connection string

How to fix "Unable to establish connection"

1Verify PostgreSQL service is running

Check if the PostgreSQL daemon is active on the target machine.

On Linux/Mac with systemd:

bash
sudo systemctl status postgresql

On macOS with Homebrew:

bash
brew services list

On Windows, check Services.msc for "postgresql-x64" service.

If the service is not running, start it:

bash
sudo systemctl start postgresql
# or
brew services start postgresql
2Check PostgreSQL is listening on the correct port

Verify that PostgreSQL is listening on port 5432 (or your configured port).

bash
# Check listening ports on Linux/Mac
sudo lsof -i :5432
# or
sudo netstat -ltnp | grep postgres

If PostgreSQL is not listening, it may not be running or listen_addresses is misconfigured. Verify your postgresql.conf:

bash
sudo -u postgres grep -E "^listen_addresses|^port" /etc/postgresql/*/main/postgresql.conf
3Validate listen_addresses configuration

Edit postgresql.conf to ensure the server accepts connections from your client IP.

bash
sudo -u postgres vi /etc/postgresql/*/main/postgresql.conf

For local connections only (default):

bash
listen_addresses = 'localhost'

For accepting connections from any IP (use with caution):

bash
listen_addresses = '*'

For specific network:

bash
listen_addresses = '192.168.1.5, localhost'

After changing this setting, restart PostgreSQL:

bash
sudo systemctl restart postgresql
4Check pg_hba.conf authentication rules

The pg_hba.conf file controls which hosts can connect and how they authenticate.

bash
sudo -u postgres cat /var/lib/postgresql/*/main/pg_hba.conf

Look for a line matching your connection attempt. For local TCP connections, ensure you have:

bash
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

For remote connections from specific network:

bash
host    all             all             192.168.1.0/24          md5

After modifying pg_hba.conf, reload the configuration:

bash
sudo -u postgres psql -c "SELECT pg_reload_conf();"
5Test connectivity with psql and netstat

Use psql to diagnose connection issues with verbose output.

bash
psql -h localhost -U postgres -d postgres -v ON_ERROR_STOP=on

Add verbose flags for more debugging:

bash
psql -h 192.168.1.10 -p 5432 -U myuser -d mydb -v

Check if port is reachable using netstat or telnet:

bash
telnet 192.168.1.10 5432
# or on macOS
nc -zv 192.168.1.10 5432

If telnet/nc connects, PostgreSQL is listening. If it times out, check firewall rules.

6Verify firewall rules allow port 5432

Firewall blocking is a common cause of connection failures on remote connections.

bash
# Check UFW (Ubuntu)
sudo ufw status
sudo ufw allow 5432/tcp

# Check firewalld (CentOS/RHEL)
sudo firewall-cmd --list-ports
sudo firewall-cmd --permanent --add-service=postgresql
sudo firewall-cmd --reload

# Check iptables directly
sudo iptables -L -n | grep 5432

On Windows, ensure Windows Firewall has a rule allowing inbound TCP on port 5432.

7Check for connection limit exceeded

If PostgreSQL is running but rejecting connections, you may have hit the max_connections limit.

bash
# Connect as superuser to check
sudo -u postgres psql -c "SHOW max_connections;"
sudo -u postgres psql -c "SELECT count(*) FROM pg_stat_activity;"

Increase the limit in postgresql.conf:

bash
max_connections = 200

Restart PostgreSQL after changing:

bash
sudo systemctl restart postgresql

Alternatively, implement connection pooling with PgBouncer to manage connections more efficiently.

Advanced notes

Connection pooling: For high-traffic applications, use PgBouncer or pgpool-II to manage connections more efficiently than creating new connections for each request. Unix socket connections (connect to /var/run/postgresql) bypass network configuration and can be faster for local connections. PostgreSQL logs in /var/log/postgresql/ contain detailed connection attempt information when log_connections = on. For remote debugging, enable query logging in postgresql.conf (log_statement = 'all') and check logs. On containerized deployments (Docker/Kubernetes), ensure the container networking allows traffic on port 5432 and health checks are configured properly. SSL/TLS connections require additional configuration in both postgresql.conf and pg_hba.conf (hostssl vs host).

Frequently asked questions

What does "Unable to establish connection" mean?
The "Unable to establish connection" error indicates that your client cannot reach the PostgreSQL server. This can happen for several reasons: the PostgreSQL service may not be running, the server might be listening on a different port or address than expected, firewall rules may be blocking access, authentication credentials may be incorrect, or network connectivity issues may prevent communication. This is a critical error because without a database connection, your application cannot function.
How do I fix "Unable to establish connection"?
PostgreSQL connection failures occur when the database server is unavailable, misconfigured, or unreachable. Fix it by verifying the service is running, checking network connectivity, and validating authentication settings.
What causes "Unable to establish connection"?
PostgreSQL service is not running on the target machine Server is listening on wrong port (not default 5432) listen_addresses in postgresql.conf does not include the connecting host IP
Is the "Unable to establish connection" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading