Postgres Too Many Connections Error: Fix It the Right Way
Postgres too many connections error crashing your app? Diagnose the real root cause fast and fix it with proper connection pooling, not a config bump.
By Babul Jack
Why Postgres runs out of connections
The Postgres too many connections error means your database has hit max_connections and is refusing new sessions with (SQLSTATE 53300). The fix in almost every production case is not raising the limit — it's reusing one connection pool per process and routing traffic through a pooler like PgBouncer.
I've seen this exact page-at-3am error on client projects more times than any other database incident: a launch goes well, traffic rises, and suddenly every API request starts failing with a connection error even though CPU and memory look fine. The database isn't down. It's just full.
How Postgres actually limits connections
PostgreSQL spawns a dedicated OS process for every connection. That's why connections aren't free: each one holds memory and competes for scheduler time even while idle. The server caps this with the max_connections setting, which defaults to 100 and can only be changed with a restart.
A handful of those slots — superuser_reserved_connections, 3 by default — are set aside for superusers. When regular connections run out but psql -U postgres still works, you're in that reserved window, not a total outage.
Managed providers usually tie the limit to your plan or instance size, so "just increase max_connections" is sometimes a forced upgrade rather than a config edit.
What the error messages actually tell you
Postgres surfaces three closely related messages, and the exact wording tells you where to look first:
FATAL: sorry, too many clients already — the server-wide limit is exhausted.
FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute — you're down to the reserved tier.
FATAL: too many connections for role "..." or for database "..." — a per-role or per-database CONNECTION LIMIT is lower than the server cap, so this isn't a global capacity problem at all.
That third message is the one developers misdiagnose most often. Raising max_connections does nothing if the real limit is on the role.
Common causes I keep finding in client codebases
Cause
What it looks like
Fix
New pool per request
new Pool() inside a request handler
Create the pool once at module scope
More app instances than the DB allows
Serverless/autoscaled functions each holding their own pool
Shrink max per instance, add a pooler
Connections never released
Rows stuck in idle in transaction
Commit/rollback explicitly, set a timeout
Deploy-time connection storm
Spikes during rolling restarts
Drain connections on shutdown, stagger rollout
Background jobs and admin tools
Migrations, queue workers, GUI clients all share the limit
Give them their own small, capped pool
The node-postgres Pooldefaults to a maximum of 10 connections per instance. That sounds safe until a serverless platform scales to 20 warm instances under load — now you're asking for 200 connections against a server that allows 100.
How do I diagnose a Postgres too many connections error?
Run this query first, before touching any configuration:
SELECT state, count(*)
FROM pg_stat_activity
GROUP BY state
ORDER BY count(*) DESC;
Then check which sessions are stuck mid-transaction, since these are the ones holding row locks and blocking autovacuum:
SELECT pid, usename, application_name, now() - state_change AS idle_time
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY state_change
LIMIT 10;
If most rows are idle (not idle in transaction), you're looking at a pooling problem — too many open-but-unused connections. If most are idle in transaction, something in your app is opening transactions and not finishing them. If you're behind PgBouncer, also check its own pool state:
SHOW POOLS;
A connection-exhausted app with plenty of free Postgres backends underneath means the pooler itself, not the database, is the bottleneck.
Step-by-step fix
1. Free slots immediately if you're in an outage
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND state_change < now() - interval '5 minutes';
This is a safe emergency valve — it only kills sessions that have been stuck mid-transaction for five minutes or more. It buys time; it doesn't fix the root cause.
2. Create one pool per process
// db.ts
import pg from "pg";
export const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
max: 5, // size per instance — see step 4
});
Import this single instance everywhere. The most common bug I fix in client code is a new Pool() or new Client() call sitting inside a request handler, which opens a fresh OS-level connection on every single request.
3. Set an idle-in-transaction timeout
ALTER SYSTEM SET idle_in_transaction_session_timeout = '120s';
SELECT pg_reload_conf();
This parameter is disabled by default (0). Once set, Postgres automatically kills and rolls back any session that sits idle mid-transaction past the limit, instead of letting it hold a connection — and row locks — indefinitely.
4. Put a pooler in front of Postgres
This is the structural fix, not a workaround. A connection pooler like PgBouncer accepts many lightweight client connections and shares a much smaller number of real Postgres connections between them. In transaction mode, a server connection is only assigned to a client for the length of one transaction, then returned to the pool — which is why it can serve hundreds of app connections from a handful of real ones.
Most managed providers already run one for you, so the fix is often just a different connection string:
AWS RDS / Aurora — enable RDS Proxy and connect to its endpoint.
Neon — add -pooler to the endpoint hostname (PgBouncer, transaction mode).
Supabase — use the pooler connection string on port 6543.
Render — enable the integrated pooler and use its port-6432 URL.
Self-hosted — run PgBouncer as its own process next to Postgres.
Keep a second, direct (unpooled) connection string for migrations, pg_dump, and anything using LISTEN/NOTIFY or session-level SET commands — these don't work reliably in transaction-pooling mode.
5. Fix deploy-time connection storms
If errors cluster around deploys, your rolling restart is overlapping old and new instances before the old ones release their connections. Add a shutdown hook that drains the pool (pool.end()) before the process exits, and reduce max surge so you don't double your peak connection count during a rollout.
6. Raise max_connections only as a last resort
Increasing max_connections is almost always the wrong first move. Postgres sizes shared memory based on this value, so a bigger number means more RAM reserved whether or not it's used, and on most managed platforms it's tied to instance size — meaning "raise the limit" quietly becomes "upgrade your plan." Pool first, raise the ceiling only if the math genuinely still doesn't fit after pooling.
Why this matters more in serverless and fintech workloads
I deal with this constantly on client fintech and data-product projects where traffic is bursty — a payment webhook storm, a batch reconciliation job, or a reporting dashboard that fans out dozens of queries at once. Serverless platforms scale instance count automatically, and every new instance brings its own pool; that's exactly the "more instances than the limit allows for" scenario that trips up teams who tested fine on staging with a single long-running server.
If you're building or hardening a production backend and want this handled properly from day one — pooling, timeouts, and connection-storm-safe deploys included — that's core to the kind of web app development work I do for clients, and it's far cheaper to design in up front than to firefight once you're live.
PostgreSQL 19, released on November 19, 2026, didn't change how max_connections or the reserved-connection tiers work — the process-per-connection model and the same ALTER SYSTEM parameters still apply, so everything here holds on the newest release too.
Postgres too many connections vs. related errors
It's worth distinguishing this from errors that look similar but need a different fix:
A breaking upgrade changing default pool behavior is a different failure mode again — see how that played out in DuckDB 2.0 Upgrade Guide: Breaking Changes to Fix in 2026 for an analytics-engine example of the same "it worked yesterday" surprise.
Frequently asked questions
Does increasing max_connections fix "too many connections"?
Only temporarily, and only if your workload genuinely needs more concurrent connections after pooling. Each connection is a full OS process with its own memory footprint, so Postgres performance degrades as the count climbs well before you hit the hard limit, and on managed platforms the ceiling is usually tied to your instance size anyway.
What's the difference between "idle" and "idle in transaction"?
idle means the connection is open but not inside a transaction — normal pool behavior between queries. idle in transaction means a transaction was opened and never committed or rolled back, which holds locks and blocks autovacuum; it's almost always an application bug or a missing COMMIT.
Does PgBouncer work with every Postgres feature?
Not in transaction mode. Session-level features — LISTEN/NOTIFY, session SET, advisory locks held across statements, and temporary tables — don't survive transaction pooling because the underlying server connection can change between statements. Route those through a direct, unpooled connection instead.
Why do I still see this error with a small app and low traffic?
Usually a connection leak: a pool client checked out and never released, or a serverless function cold-starting a brand-new pool on every invocation because the pool was created inside the handler instead of at module scope.
Is this the same issue on Supabase, Neon, RDS, or self-hosted Postgres?
The root cause (process-per-connection, fixed max_connections) is identical everywhere; only the pooler setup differs. Supabase and Neon ship a pooler you opt into with a different connection string, RDS needs RDS Proxy as a separate resource, and self-hosted Postgres needs you to run PgBouncer yourself.
Key takeaways
The Postgres too many connections error (FATAL: sorry, too many clients already, SQLSTATE 53300) means active backends have hit max_connections, which defaults to 100 and requires a restart to change.
Diagnose first with pg_stat_activity grouped by state — idle in transaction points to a leak, plain idle points to a missing pooler.
Create exactly one connection pool per process, sized for how many instances actually run, never inside a request handler.
Put a transaction-mode pooler like PgBouncer (or your provider's built-in equivalent) in front of Postgres before you ever need to raise the connection limit.
Set idle_in_transaction_session_timeout so a stuck transaction can't hold a connection indefinitely.
Keep migrations, LISTEN/NOTIFY, and admin tools on a direct, unpooled connection string.
Raising max_connections is a last resort, not a first fix — and on most managed platforms it means upgrading your plan.