How to Fix PostgreSQL 'too many connections' Error
In this tutorial, you'll learn about How to Fix PostgreSQL 'too many connections' Error. We cover key concepts, practical examples, and best practices.
The Problem
Your application logs in and gets FATAL: sorry, too many clients already or FATAL: remaining connection slots are reserved for non-replication superuser connections. PostgreSQL has a default connection limit (typically 100) and all slots are used. Each application instance, worker, and background job holds one or more connections, and they add up quickly.
Quick Fix
1. Kill idle connections immediately
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle'
AND pid <> pg_backend_pid();
2. Kill connections from a specific database or user
-- Kill all connections to a specific database
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'my_database'
AND pid <> pg_backend_pid();
-- Kill connections from a specific user
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE usename = 'my_user'
AND pid <> pg_backend_pid();
3. Check current connection count
SELECT count(*), usename, datname, state
FROM pg_stat_activity
GROUP BY usename, datname, state
ORDER BY count(*) DESC;
4. Increase max_connections
Edit <a href="/databases/postgresql/">postgresql</a>.conf:
max_connections = 200
Then restart:
sudo systemctl restart postgresql
Or set via SQL (requires restart):
ALTER SYSTEM SET max_connections = 200;
5. Set up PgBouncer for connection pooling
sudo apt-get install pgbouncer
Edit /etc/pgbouncer/pgbouncer.ini:
[databases]
mydb = host=127.0.0.1 port=5432 dbname=mydb
[pgbouncer]
listen_addr = 127.0.0.1
listen_port = 6432
auth_type = md5
pool_mode = transaction
max_client_conn = 200
default_pool_size = 20
Connect to port 6432 instead of 5432.
6. Set idle timeout on the server
ALTER SYSTEM SET idle_in_transaction_session_timeout = '60000';
ALTER SYSTEM SET statement_timeout = '30000';
SELECT pg_reload_conf();
Common Causes
| Cause | Description | Fix |
|---|---|---|
| Application doesn't close connections | Each request opens a new connection | Always release connections with finally or context managers |
| Too many app instances | Multiple workers * pool size > max_connections | Use PgBouncer for connection pooling |
| Stuck idle transactions | idle in transaction connections accumulate |
Set idle_in_Transaction_session_timeout |
| Monitoring tools | pgAdmin, datadog, etc. each use connections | Account for monitoring in your connection budget |
| Connection pool too large | Pool size set higher than necessary | Reduce max in your pool config |
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
Prevention
- Always close database connections in your application code (
conn.close()or context managers) - Use PgBouncer or other connection poolers in production
- Monitor with
SELECT count(*) FROM pg_stat_activityand set alerts at 80% of max_connections - Set
idle_in_Transaction_session_timeoutto automatically kill stuck transactions - Use
ALTER SYSTEM SET max_connections = <value>to persist the change permanently
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro