sqlserver rejected establishment of sqlconnection
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.
- 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
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.
-- 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.
-- 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.
✕ Set max_connections very high without a pooler
Each Postgres connection consumes ~5-10 MB; very high max_connections can exhaust server memory.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev