08004
PostgreSQLERRORNotableConnection ExceptionHIGH confidence

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

expected output

FATAL:  sorry, too many clients already

Fix 1

Increase max_connections or use a connection pooler

WHEN When the server is consistently at connection capacity.

Increase max_connections or use a connection pooler
-- 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 When the server refuses specific clients rather than being at capacity.

Review pg_hba.conf for reject rules
-- 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

Each Postgres connection consumes ~5-10 MB; very high max_connections can exhaust server memory.

Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors