Your app suddenly can't connect to PostgreSQL, and the log or the error says:
FATAL: remaining connection slots are reserved for non-replication superuser connectionsThat's the wording in PostgreSQL 15 and earlier. PostgreSQL 16 reworded it and added a second kind of reserved slot, so newer servers say the remaining slots are reserved for superusers, or for roles with privileges of pg_use_reserved_connections. It's the same problem either way: the database is full for ordinary users.
Why it happens before the limit
PostgreSQL accepts at most max_connections connections at once (100 by default on a self-installed server; managed services set their own). It holds a few of those back so an administrator can still get in when everything else is used up:
superuser_reserved_connections, 3 by default: only superusers may use these.reserved_connections, added in PostgreSQL 16, 0 by default: for roles grantedpg_use_reserved_connections, such as a monitoring user.
So with the defaults, an ordinary app user is refused at 97 connections, not 100. When you see this error, your app (or several of them) has used every ordinary slot. It's PostgreSQL's version of "too many connections".
Step 1: See what's using the connections
The reserved slots exist for exactly this moment: connect as a superuser (for example sudo -u postgres psql on the server) and look:
SHOW max_connections;
SELECT usename, application_name, client_addr, state, count(*)
FROM pg_stat_activity
GROUP BY 1, 2, 3, 4
ORDER BY 5 DESC;What to look for:
- Lots of
idleconnections from one app: a connection pool sized too large, or many copies of the app each with their own pool. idle in transaction: code that opened a transaction and never finished it. These hold their slot, and often locks, until they're closed.- Connections growing without limit: a leak, where code opens connections and never returns them. Serverless functions that open a fresh connection on every call do this too.
Step 2: Get unstuck right now
Restarting the app usually frees its connections. If you can't, close the idle ones from your superuser session. This ends every connection from the user app that has been idle for over 10 minutes; adjust the user and time to suit:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE usename = 'app'
AND state = 'idle'
AND state_change < now() - interval '10 minutes';Closing a connection that's mid-task rolls back its work, so leave active connections alone unless you know what they're doing.
Step 3: Stop it coming back
- Size the pools. Add up every connection pool that talks to this database: pool size × copies of the app × every service. Keep the total comfortably under
max_connectionsminus the reserved slots. - Close forgotten transactions automatically.
idle_in_transaction_session_timeoutends sessions that sit in an open transaction too long, for exampleALTER ROLE app SET idle_in_transaction_session_timeout = '5min';. PostgreSQL 14 and later also haveidle_session_timeoutfor connections that sit idle outside a transaction. - Put a pooler in front. A connection pooler such as PgBouncer lets hundreds of app connections share a few dozen real database connections. It's the standard fix when many processes or serverless functions talk to one database. Too many connections covers pooling in more detail.
Should I just raise max_connections?
Sometimes, but not first. Every connection costs memory on the server whether it's busy or not, and raising the limit needs a restart. If the connections are mostly idle, raising the limit only hides a pool or a leak that will fill the new limit too. Raise it in modest steps once the pools are sized and the leaks are fixed, and on a managed service check what your plan allows.
A different connection error?
If the message is no pg_hba.conf entry for host instead, the server turned the connection away by rule, not because it was full: see no pg_hba.conf entry for host. To rule out a mistake in the connection string itself, paste it into the connection string checker; it runs in your browser and sends nothing.