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.
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.
Check if the PostgreSQL daemon is active on the target machine.
On Linux/Mac with systemd:
sudo systemctl status postgresqlOn macOS with Homebrew:
brew services listOn Windows, check Services.msc for "postgresql-x64" service.
If the service is not running, start it:
sudo systemctl start postgresql
# or
brew services start postgresqlVerify that PostgreSQL is listening on port 5432 (or your configured port).
# Check listening ports on Linux/Mac
sudo lsof -i :5432
# or
sudo netstat -ltnp | grep postgresIf PostgreSQL is not listening, it may not be running or listen_addresses is misconfigured. Verify your postgresql.conf:
sudo -u postgres grep -E "^listen_addresses|^port" /etc/postgresql/*/main/postgresql.confEdit postgresql.conf to ensure the server accepts connections from your client IP.
sudo -u postgres vi /etc/postgresql/*/main/postgresql.confFor local connections only (default):
listen_addresses = 'localhost'For accepting connections from any IP (use with caution):
listen_addresses = '*'For specific network:
listen_addresses = '192.168.1.5, localhost'After changing this setting, restart PostgreSQL:
sudo systemctl restart postgresqlThe pg_hba.conf file controls which hosts can connect and how they authenticate.
sudo -u postgres cat /var/lib/postgresql/*/main/pg_hba.confLook for a line matching your connection attempt. For local TCP connections, ensure you have:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5For remote connections from specific network:
host all all 192.168.1.0/24 md5After modifying pg_hba.conf, reload the configuration:
sudo -u postgres psql -c "SELECT pg_reload_conf();"Use psql to diagnose connection issues with verbose output.
psql -h localhost -U postgres -d postgres -v ON_ERROR_STOP=onAdd verbose flags for more debugging:
psql -h 192.168.1.10 -p 5432 -U myuser -d mydb -vCheck if port is reachable using netstat or telnet:
telnet 192.168.1.10 5432
# or on macOS
nc -zv 192.168.1.10 5432If telnet/nc connects, PostgreSQL is listening. If it times out, check firewall rules.
Firewall blocking is a common cause of connection failures on remote connections.
# 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 5432On Windows, ensure Windows Firewall has a rule allowing inbound TCP on port 5432.
If PostgreSQL is running but rejecting connections, you may have hit the max_connections limit.
# 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:
max_connections = 200Restart PostgreSQL after changing:
sudo systemctl restart postgresqlAlternatively, implement connection pooling with PgBouncer to manage connections more efficiently.
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).
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL