PG
PRO
08004ERRORTier 2 — Caution✅ HIGH confidence

sqlserver rejected establishment of sqlconnection

Category: Connection ExceptionVersions: All Postgres versions

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

  1. 1Server has reached max_connections and cannot accept new connections
  2. 2Server is in startup, recovery, or shutdown mode and not accepting user connections
  3. 3pg_hba.conf contains a reject rule matching the client address or database
  4. 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.

trigger — this will ERROR
FATAL: sorry, too many clients already

Fix 1: Increase max_connections or use a connection pooler

When the server is consistently at connection capacity.

fix
-- In postgresql.conf:
-- max_connections = 200   (increase, then restart)
-- Better: use PgBouncer to pool connections

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

fix
-- 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-256

Why 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

⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →