Connection Failure — SQLSTATE 08006

SQLSTATE 08006 condition connection_failure class 08 — Connection Exception severity ERROR
Reproduced & verified on PostgreSQL 14.23, 15.18, 16.14, 17.10 and 18.4 — identical message on every version.
Last reviewed 29 May 2025 · Reproduced live with the SQL on this page.

! Symptoms Free

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.

1 Environment & reproduce Free

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 Free

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.

3 Recovery & verify Free

Steps to resolve 08006:

  1. Confirm the server is accepting connections: pg_isready -h host -p 5432.
  2. Scan the PostgreSQL log around the timestamp for crashes, OOM kills, or FATAL lines.
  3. Verify the network path and firewall/security-group rules allow port 5432, and rule out NAT/idle timeouts that drop long-lived sockets.
  4. Add reconnect and retry-with-backoff in the client; never reuse a connection that returned this error.
  5. With a pooler (PgBouncer), enable server keepalives and validate connections on checkout.

Reference: PostgreSQL error codes — Class 08 (Connection Exception).