Postgres Connection Pool Exhaustion: Finding the Leak

Symptom

"too many connections" errors under load; pg pool saturated.

Root cause

App opens connections faster than it closes them, or pool size > max_connections.

Fix, in order

1. See who holds the connectionsSELECT client_addr, state, count(*) FROM pg_stat_activity GROUP BY 1,2 ORDER BY 3 DESC;
2. Kill idle connections over 10 minutesSELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state='idle' AND now()-state_change > interval '10 min';
3. Right-size: pool size x instances < max_connectionsSHOW max_connections;
4. Add PgBouncer in transaction modepool_mode = transaction

Prevent it coming back

Every app needs a cap on connections; monitor pg_stat_activity trends.

Run it on clean infra

Spinning up a fresh box to reproduce or escape this error?

Managed cloud hosting

Related

Cloud & DevOps tool comparisons · Hardware build guides

NinjaOps publishes free engineering guides. Some outbound links are affiliate links: they cost you nothing and support the site.

Last reviewed 2026-10-02 · NinjaOps SEO grid · DevOps blog