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.confrule for this host/database/role. - The target database does not exist.
max_connectionsis reached (see 53300).- The role lacks the
LOGINattribute. - The server is in recovery and not yet accepting connections.
Diagnostic Queries
Recovery
Steps to resolve 08004:
- Read the exact reason in the server log — it names the failing rule or limit.
- Add or correct the
pg_hba.confentry and reload:SELECT pg_reload_conf();. - Verify the database and login role exist and the role has
LOGIN. - If slots are full, add pooling or raise
max_connections(see 53300).
Reference: PostgreSQL error codes — Class 08 (Connection Exception).
Thanks — noted. This helps keep the database accurate.