Symptoms
The connection to the server was lost or could not be completed — a network or availability problem, not a SQL problem.
- The error is written to the server log and returned to the client carrying
SQLSTATE 08006. - 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 connection_failure 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
PostgreSQL raises connection_failure when an established connection drops or a new one cannot finish the startup handshake. The cause lives in the network path or in server availability, not in the query.
Common causes:
- The server crashed or was restarted (check uptime and logs).
- A network interruption, firewall, or idle timeout silently dropped the TCP socket.
- The backend was terminated by the OOM killer or an administrator.
- TLS negotiation failed between client and server.
- A connection pooler closed a stale or recycled connection.
Diagnostic Queries
Recovery
Steps to resolve 08006:
- Confirm the server is accepting connections:
pg_isready -h host -p 5432. - Scan the PostgreSQL log around the timestamp for crashes, OOM kills, or
FATALlines. - Verify the network path and firewall/security-group rules allow port 5432, and rule out NAT/idle timeouts that drop long-lived sockets.
- Add reconnect and retry-with-backoff in the client; never reuse a connection that returned this error.
- With a pooler (PgBouncer), enable server keepalives and validate connections on checkout.
Reference: PostgreSQL error codes — Class 08 (Connection Exception).
Thanks — noted. This helps keep the database accurate.