Your app suddenly can't reach its database, and the logs show one of these:
FATAL: sorry, too many clients already
FATAL: remaining connection slots are reserved for non-replication superuser connections
ERROR 1040 (HY000): Too many connectionsThe first two come from Postgres, the third from MySQL or MariaDB. They all mean the same thing: the database has a fixed number of connection slots, and every one is taken. New requests are refused until something lets go.
Raising the limit is the tempting fix, and occasionally the right one. More often something is holding connections it doesn't need, and the lasting fix is connection pooling.
Why the limit exists
Every connection costs the database memory, whether or not it's doing anything. The defaults are deliberately modest: 100 for Postgres and 151 for MySQL. Managed databases usually set the limit by plan size, so a small plan might allow only a few dozen.
Postgres also holds back a few slots for administrators, which is why the "reserved" message appears slightly before the hard limit. PostgreSQL "remaining connection slots are reserved" explains those slots and how the message reads in each version.
Step 1: See what's using the connections
Connect as an admin. If you're locked out too, the reserved slots in Postgres usually let a superuser in, and most managed providers show connection counts in their dashboard.
Postgres
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;MySQL and MariaDB
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW PROCESSLIST;Then look at what's holding the slots. Each pattern points to a different cause:
| What you see | Likely cause |
|---|---|
Mostly idle (Postgres) or Sleep (MySQL) | Connections opened and never closed, or app pools set too large |
Many idle in transaction (Postgres) | Code that starts a transaction and never commits, often after an error |
| Many connections, each from a different short-lived source | Serverless functions each opening their own connection |
Mostly active and slow | Slow queries piling up, so connections are busy, not leaked |
Step 2: Get unstuck right now
If the app is down, close idle connections to free slots. In Postgres, this closes connections that have been idle for more than 10 minutes:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state IN ('idle', 'idle in transaction')
AND state_change < now() - interval '10 minutes'
AND pid <> pg_backend_pid();In MySQL, find long-sleeping connections in SHOW PROCESSLIST and end them with KILL followed by the connection's Id. Restarting the app also releases everything it holds. Either way, this buys time: the next steps stop it from happening again.
Step 3: Fix the cause
Size your app's pool
Most database libraries keep their own pool of connections, and every copy of your app has its own. Four app servers with a pool of 25 each can use 100 connections between them, the entire Postgres default. Add up pool size times the number of app instances, including background workers, and keep the total comfortably under the limit.
Close what you open
A connection that isn't returned to the pool on every code path, including errors, leaks a slot each time. Use your language's automatic cleanup (using in C#, with in Python, try/finally elsewhere) so connections are released even when a query fails, and make sure every transaction ends in a commit or rollback.
Add timeouts for forgotten connections
- Postgres:
idle_in_transaction_session_timeoutcloses sessions left in an open transaction, andidle_session_timeout(Postgres 14 and later) closes idle ones. - MySQL:
wait_timeoutcloses idle connections. Its default is 8 hours, far longer than most apps need.
Set them generously, in minutes rather than seconds, so you don't cut off connections your app's pool expects to reuse.
Step 4: Put a connection pooler in front
Serverless and edge functions are the most common cause of this error today. Each copy of a function opens its own connection, and a traffic spike can start hundreds of copies at once. No app-side pool setting can fix that, because each copy only knows about itself.
A connection pooler sits between your app and the database. Your app can open as many connections to the pooler as it likes, and the pooler funnels them through a small, fixed number of real database connections.
- Self-hosted Postgres: PgBouncer is the standard choice. Run it in transaction mode, where a database connection is borrowed only for the length of each transaction.
- Supabase: every project includes a pooler. Use its transaction mode connection string, on port
6543, for serverless functions. - Other managed databases: most offer a built-in pooler or proxy. Look for "connection pooling" in your provider's dashboard.
- MySQL: ProxySQL fills the same role.
One catch: in transaction mode, each transaction can land on a different database connection, so features that depend on staying on one connection, such as session settings and some prepared statements, may not behave as expected. Many libraries have a setting to turn off prepared statements for use with a pooler, so check your library's documentation.
When raising the limit makes sense
If the connections are genuinely busy, your pools are sized sensibly, and the database has memory to spare, raising max_connections is reasonable. On Postgres it takes a restart. Raise it in modest steps and watch memory, because each extra connection costs some whether it's used or not. On a managed database, the limit is usually tied to your plan, so raising it may mean a larger plan.
A different connection error?
If Postgres says no pg_hba.conf entry for host, the database isn't full: it has no rule that lets your connection in. No pg_hba.conf entry for host covers that fix. And if the connection fails before it even reaches the database, check the connection string itself with the connection string checker.