SQLSTATE 08004 ERROR Class 08: Connection Exception

sqlserver_rejected_establishment_of_sqlconnection Sqlserver Rejected Establishment Of Sqlconnection — SQLSTATE 08004

The server actively rejected the connection during startup.

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

Symptoms

The server actively rejected the connection during startup.

  • The error is written to the server log and returned to the client carrying SQLSTATE 08004.
  • 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 sqlserver_rejected_establishment_of_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 server received the request but refused it before or during authentication.

Common causes:

  • No matching pg_hba.conf rule for this host/database/role.
  • The target database does not exist.
  • max_connections is reached (see 53300).
  • The role lacks the LOGIN attribute.
  • The server is in recovery and not yet accepting connections.

Diagnostic Queries

Recovery

Steps to resolve 08004:

  1. Read the exact reason in the server log — it names the failing rule or limit.
  2. Add or correct the pg_hba.conf entry and reload: SELECT pg_reload_conf();.
  3. Verify the database and login role exist and the role has LOGIN.
  4. If slots are full, add pooling or raise max_connections (see 53300).

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

Was this helpful?