SQLSTATE 08006 ERROR Class 08: Connection Exception

connection_failure Connection Failure — SQLSTATE 08006

The connection to the server was lost or could not be completed — a network or availability problem, not a SQL problem.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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:

  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).

Was this helpful?