PostgreSQL "FATAL: sorry, too many clients already": causes and fixes
Postgres refuses new connections once max_connections is reached. How to find idle and leaked sessions, end them safely, add PgBouncer and size the limit so it stops happening.
· 6 min read · By the Spot Downtime team
New connections to your database start failing with one of these:
FATAL: sorry, too many clients already
FATAL: remaining connection slots are reserved for non-replication superuser connectionsBoth mean the same thing: PostgreSQL has reached max_connections. The second message appears when only the slots kept back for superusers are left (newer versions word it in terms of reserved roles). Every request that needs a new connection fails until one closes.
Why Postgres runs out sooner than you expect
The default max_connections is 100, and 3 of those are held back by superuser_reserved_connections. Each connection is a separate server process with its own memory, so Postgres is built for a modest number of busy connections, not thousands of idle ones. Modern deployments use them up fast: many app replicas each with a pool, background workers, migrations, and serverless functions that open a fresh connection per invocation.
Step 1: look at pg_stat_activity
Connect as a superuser, which can still use a reserved slot, and see what is holding connections:
SELECT usename, application_name, client_addr, state, COUNT(*) AS n
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY usename, application_name, client_addr, state
ORDER BY n DESC;The state column tells you the story:
- idle: connected but doing nothing. A few per pool is normal; hundreds means pools that are too large, or connections leaked by the app.
- idle in transaction: the app opened a transaction and never committed or rolled back. These are the most harmful, because they also hold locks and block vacuum.
- active: running a query. Many long-running active sessions point to a slow query or lock contention that makes requests pile up.
To find the long ones:
SELECT pid, usename, state, now() - state_change AS duration, left(query, 80) AS query
FROM pg_stat_activity
WHERE state <> 'idle' AND backend_type = 'client backend'
ORDER BY duration DESC
LIMIT 20;Free up slots in an emergency
-- End sessions stuck idle in a transaction for more than 10 minutes
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND now() - state_change > interval '10 minutes';Terminating a session rolls back its open transaction. Use pg_cancel_backend(pid) to stop only the current query while keeping the connection.
Step 2: let the server close stuck sessions
Two settings stop leaked sessions from holding slots forever:
ALTER SYSTEM SET idle_in_transaction_session_timeout = '5min';
ALTER SYSTEM SET idle_session_timeout = '30min'; -- PostgreSQL 14+
SELECT pg_reload_conf();Set idle_session_timeout carefully: a connection pool that keeps idle connections will see them closed and has to handle the reconnect. Set it longer than your pool's own idle timeout.
Step 3: put a pooler in front
The standard fix is PgBouncer. Apps open as many client connections as they like to PgBouncer, which shares a small pool of real server connections between them. In transaction mode, a server connection is used only for the length of a transaction, so a few dozen server connections can serve hundreds of clients.
; pgbouncer.ini
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20
max_prepared_statements = 100 ; PgBouncer 1.21+Transaction mode caveats
SET, advisory locks held across transactions and LISTEN do not work as expected. Older PgBouncer versions also break named prepared statements; version 1.21 and later support them when max_prepared_statements is set. Check your driver's documentation before switching.Managed providers often include a pooler: Supabase, Neon and others offer a pooled connection string, and AWS offers RDS Proxy. For serverless functions, always use the pooled endpoint.
Step 4: raise max_connections only with a budget
Add up what you actually need:
replicas × pool size + workers + cron jobs + migrations + admin headroom ≤ max_connections- First, make each pool smaller. A pool of 5 to 10 per instance is plenty for most web apps.
- Then, if you still need more, raise the limit in
postgresql.confor withALTER SYSTEM SET max_connections = 200. This one needs a full restart, not a reload. - Check memory: each connection can use
work_memseveral times over in complex queries. On managed databases such as RDS, the default limit is derived from instance memory for this reason.
Monitor it before users see it
A TCP check on port 5432 stays green during this outage, because Postgres still accepts the TCP connection before refusing the client. Instead, expose a health endpoint in your app that runs SELECT 1 through its normal pool and returns an error status on failure. A website monitor on that endpoint, with a keyword check, alerts you within a minute of the first refused connection. For the trend, alert when the connection count passes about 80% of max_connections, so you have time to act. If a monitor and a user disagree about whether the site is up, see monitor says down but the site works.
Get new guides by email
Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.
Keep reading
- Servers · HTTP errors · Troubleshootingnginx "Too many open files" (24) error: how to fix itnginx logs accept4() failed (24: Too many open files) when it hits its file descriptor limit. How to check the real limit, raise it with systemd and nginx, and avoid it.November 26, 2026 · 6 min read
- Servers · Troubleshooting · DockerRedis "Connection refused" on port 6379: how to fix itCould not connect to Redis at 127.0.0.1:6379, ECONNREFUSED, Error 111: Redis is not running, listening elsewhere or unreachable from a container. Check each cause in order.November 25, 2026 · 5 min read