08004ERRORTier 2 — Caution✅ HIGH confidencesqlserver rejected establishment of sqlconnection
What this means
SQLSTATE 08004 is raised when the Postgres server actively refuses a connection attempt from the client. Common causes include exceeding max_connections, the server not accepting connections during startup/shutdown, or pg_hba.conf rules that reject the client.
Why it happens
- 1Server has reached max_connections and cannot accept new connections
- 2Server is in startup, recovery, or shutdown mode and not accepting user connections
- 3pg_hba.conf contains a reject rule matching the client address or database
- 4Standby server is in read-only mode and the client requested a read-write connection
How to reproduce
Application attempts to connect when the server is at capacity.
Fix 1: Increase max_connections or use a connection pooler
When the server is consistently at connection capacity.
-- In postgresql.conf:
-- max_connections = 200 (increase, then restart)
-- Better: use PgBouncer to pool connectionsWhy this works
Raising max_connections reserves more memory per connection. A pooler like PgBouncer multiplexes many application connections over fewer server connections, which is more efficient.
Fix 2: Review pg_hba.conf for reject rules
When the server refuses specific clients rather than being at capacity.
-- In pg_hba.conf, ensure an accept rule precedes any reject rule for your client:
-- host mydb myuser 10.0.0.0/8 scram-sha-256Why this works
pg_hba.conf rules are evaluated top-to-bottom; the first matching rule wins.
What not to do
Set max_connections very high without a pooler
Why it's wrong: Each Postgres connection consumes ~5-10 MB; very high max_connections can exhaust server memory.
Sources
📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 08 — Connection Exception
📖 Further reading: pg_hba.conf documentation
Confidence assessment
✅ HIGH confidence
Standard SQLSTATE. Postgres commonly uses this for max_connections exhaustion and hba-reject scenarios.
See also
🔗 Related errors
📄 Reference pages