PostgreSQL connection failure occurs when a client cannot establish a connection to the database server. This can happen due to the server being down, incorrect connection parameters, network issues, or authentication problems.
A PostgreSQL connection failure means the client application failed to establish a TCP connection to the PostgreSQL server. This is a fundamental networking or server availability issue that prevents any database operations from occurring. The error occurs before authentication or query execution, indicating either the server is unreachable, not listening on the expected port, or a firewall is blocking the connection. The specific cause depends on whether you're connecting locally via Unix socket or remotely via TCP/IP. Local connections may fail due to missing socket files or permission issues, while remote connections typically fail due to server not running, listening on wrong port, or firewall restrictions.
Check if the PostgreSQL service is active and running.
On Linux/macOS:
# Check service status
sudo systemctl status postgresql
# If not running, start it
sudo systemctl start postgresqlOn macOS with Homebrew:
brew services list
brew services start postgresqlOn Windows:
# Check in Services application or use:
net start postgresql-x64-14If the service won't start, check the PostgreSQL logs for startup errors. The log location is typically /var/log/postgresql/ on Linux or in the PostgreSQL data directory.
Verify the server is listening on the port you're connecting to.
# Check what port PostgreSQL is listening on
sudo netstat -tlnp | grep postgres
# Or with ss (newer systems)
sudo ss -tlnp | grep postgresThe output should show PostgreSQL listening on port 5432 (default) or your custom port. If nothing appears, PostgreSQL is not listening on TCP/IP.
If using a non-standard port, ensure your connection string includes it:
psql -h localhost -p 5433 -U username -d databasePostgreSQL must be configured to accept TCP/IP connections.
1. Find and edit the postgresql.conf file:
# Find the file location
sudo -u postgres psql -c "SHOW config_file"
# Edit it (commonly at /etc/postgresql/14/main/postgresql.conf)
sudo nano /etc/postgresql/14/main/postgresql.conf2. Find the listen_addresses setting. It should be:
listen_addresses = '*' # Listen on all interfacesIf it's set to localhost only, remote connections will fail. The * value allows connections from any network interface.
3. Save and restart PostgreSQL:
sudo systemctl restart postgresqlThe pg_hba.conf file controls which hosts can connect. Verify your client IP is allowed.
1. Find and edit the file:
# Commonly at:
sudo nano /etc/postgresql/14/main/pg_hba.conf2. Look for a line matching your connection type. For TCP/IP connections from localhost:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md53. For remote connections, you need an entry like:
# Allow from specific IP
host all all 192.168.1.100/32 md5
# Or allow from entire subnet
host all all 192.168.1.0/24 md54. Save and reload PostgreSQL:
sudo systemctl reload postgresqlNote: Lines are processed top-to-bottom, so place more specific entries before general ones.
Before troubleshooting PostgreSQL internals, test basic network connectivity:
# Test with telnet (if installed)
telnet localhost 5432
# Or with netcat
nc -zv localhost 5432
# For remote server
nc -zv 192.168.1.50 5432If you get 'Connection refused' or timeout, the server isn't listening. If it connects, PostgreSQL is reachable but the client may have wrong credentials or pg_hba.conf issues.
Check firewall if connecting remotely:
# On Linux with ufw
sudo ufw allow 5432/tcp
# Or with iptables
sudo iptables -A INPUT -p tcp --dport 5432 -j ACCEPTEnsure your application is using correct connection parameters.
Common formats:
postgresql://username:password@localhost:5432/dbname
host=localhost port=5432 user=postgres password=secret dbname=mydbVerify each parameter:
- host: Should be 'localhost' for local Unix socket, or IP/hostname for TCP
- port: Default is 5432; verify it matches your postgresql.conf
- user: Must be a valid PostgreSQL role
- password: Must match the user's password
- dbname: Database must exist
Test with psql directly:
psql -h localhost -p 5432 -U postgres -d postgresIf this works, your connection parameters are correct. If not, you've found the issue in one of the above steps.
If PostgreSQL was running but now rejects all connections, the max_connections limit may be reached.
# Connect as superuser (if you can)
sudo -u postgres psql
# View current setting
SHOW max_connections;
# View active connections
SELECT count(*) FROM pg_stat_activity;
# Terminate idle connections
SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE pid <> pg_backend_pid() AND state = 'idle';To increase the limit:
1. Edit postgresql.conf and change max_connections
2. Restart PostgreSQL: sudo systemctl restart postgresql
Note: Each connection uses system resources. Setting it too high can cause performance issues.
Unix Socket vs TCP/IP: PostgreSQL supports two connection methods. Local Unix socket connections (default on some systems) are faster but require the socket file to exist at the expected location (usually /var/run/postgresql/ or /tmp/). If you see 'could not connect to server: No such file or directory' with local connections, check the unix_socket_directories setting in postgresql.conf.
SSL/TLS Connections: If your application requires SSL, PostgreSQL must be compiled with SSL support. Check with psql --version or SHOW ssl; in psql. SSL connection failures appear similar to general connection failures but are distinguished in the logs.
Replication and Streaming: If this error occurs during streaming replication setup, verify that the replication role is created, has REPLICATION privilege, and pg_hba.conf has a matching entry with the replication keyword.
Container/Cloud Deployments: In Docker or cloud environments (AWS RDS, Azure Database for PostgreSQL), connection failures are often due to security groups/network ACLs not allowing traffic, or the server endpoint being incorrect. Always verify the endpoint URL and port from your cloud provider's console.
Connection Pool Exhaustion: With application connection pools (pgBouncer, PgPool-II), a full pool can look like a connection failure. Check the pool's configuration and connection counts before investigating PostgreSQL itself.
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