Symptoms
The client could not establish a connection — typically the server is unreachable or not listening for this client.
- The error is written to the server log and returned to the client carrying
SQLSTATE 08001. - Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
- PL/pgSQL can trap it by name:
EXCEPTION WHEN sqlclient_unable_to_establish_sqlconnection THEN.
Environment
Severity: ERROR | PostgreSQL versions: 12, 13, 14, 15, 16, 17
Commonly coincides with restarts, failover, maintenance, or network events; check server uptime and the log around the timestamp.
Root Cause
The driver failed before authentication completed. PostgreSQL (or the network) refused or never answered the connection attempt.
Common causes:
- Wrong host, port, or database name in the connection string.
listen_addressesdoes not include the client's network interface.- The server is not started, or a firewall blocks port 5432.
- TLS is required but the client is not configured for it.
Diagnostic Queries
Recovery
Steps to resolve 08001:
- Verify host, port, and database name in the connection string.
- Set
listen_addresses = '*'(or the correct interface) in postgresql.conf and restart. - Add a matching
pg_hba.confentry for the client IP, database, and role. - Test raw connectivity:
psql "host=... port=5432 user=... dbname=..."andpg_isready. - Check firewalls/security groups and the required
sslmode.
Reference: PostgreSQL error codes — Class 08 (Connection Exception).
Thanks — noted. This helps keep the database accurate.