How to fix Too many connections in PostgreSQL
PostgreSQL rejects new connections when the max_connections limit is reached. Fix this by tuning max_connections, implementing connection pooling with PgBouncer, or terminating idle sessions.
Quick fix
Fixes Too many connections
SHOW max_connections;
SHOW superuser_reserved_connections;
SELECT count(*) FROM pg_stat_activity;- Check current connection limits and active connections
- Check for idle or long-running connections
- Implement connection pooling (recommended)
- Increase max_connections if pooling is not an option
SHOW max_connections;SHOW superuser_reserved_connections;SELECT count(*) FROM pg_stat_activity;Too many connections
What this error means
This error occurs when PostgreSQL server has reached its maximum allowed concurrent connections (default: 100). When a new connection attempt arrives and all available slots are occupied, the server rejects it with a FATAL error. This is a safety mechanism to prevent the server from being overwhelmed by too many simultaneous clients, which would degrade performance and consume excessive memory and CPU resources. The error can also occur at the database level or per-role level if those limits are configured.
Typical symptoms
- New database connections are rejected with "FATAL: too many connections" error
- Applications fail to connect even though the database server is running
- Connection errors appear during traffic spikes or load tests
- Error message shows "too many connections for database" or "too many connections for role"
- Intermittent connection failures that get worse during peak hours
Common causes
- The max_connections parameter is set too low for your workload
- Connection pooling is not in use, causing each client to hold a dedicated connection
- Application code is not properly closing connections, leading to connection leaks
- Monitoring or third-party tools are opening persistent connections and exhausting the pool
- Database has per-database or per-role connection limits that are too restrictive
- Long-running idle sessions are occupying connection slots unnecessarily
How to fix "Too many connections"
Connect to PostgreSQL as a superuser and check your current settings:
SHOW max_connections;
SHOW superuser_reserved_connections;
SELECT count(*) FROM pg_stat_activity;Note the current value and how many connections are active. The reserved slots (default 3) are reserved for superusers only.
Query the activity log to see if idle connections are consuming slots:
SELECT pid, usename, application_name, state, query_start, state_change
FROM pg_stat_activity
WHERE state = 'idle' OR (NOW() - query_start) > interval '30 minutes'
ORDER BY query_start;If you find many idle connections, they may be leaked connections from your application. Check that your application is properly closing connections.
Instead of increasing max_connections indefinitely, use a connection pooler like PgBouncer to multiplex many client connections onto fewer database connections:
1. Install PgBouncer (on Debian/Ubuntu: sudo apt-get install pgbouncer)
2. Configure /etc/pgbouncer/pgbouncer.ini:
[databases]
mydb = host=localhost port=5432 dbname=mydb
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 25
reserve_pool_size = 53. Point your application to PgBouncer (localhost:6432) instead of PostgreSQL (localhost:5432)
Pool mode "transaction" releases the database connection after each transaction, allowing many clients to share fewer connections.
If you cannot use connection pooling, increase max_connections:
1. Connect as superuser and modify the setting:
ALTER SYSTEM SET max_connections = 200;2. Restart PostgreSQL for the change to take effect:
sudo systemctl restart postgresql3. Verify the change:
SHOW max_connections;Note: Increasing max_connections requires more shared memory. Monitor system resources after the change. Generally, 200-500 connections is reasonable; higher values require careful resource planning.
Check if limits are set at the database or role level:
SELECT datname, datconnlimit FROM pg_database WHERE datconnlimit > 0;
SELECT rolname, rolconnlimit FROM pg_roles WHERE rolconnlimit > 0;If limits are too low, adjust them:
ALTER DATABASE mydb CONNECTION LIMIT 100;
ALTER ROLE myuser CONNECTION LIMIT -1; -- -1 means unlimitedIf you need to free up connections immediately, terminate idle sessions:
-- Terminate idle connections (be careful not to kill active work)
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle'
AND pid != pg_backend_pid();This is a temporary measure. The real fix is to prevent idle connections from forming in the first place through connection pooling or application fixes.
Advanced notes
Reserved connections (superuser_reserved_connections, default 3) allow superusers to connect even when all regular slots are full. With max_connections=100 and 3 reserved, only 97 are available for regular users. PostgreSQL allocates shared memory at startup for all max_connections, so increasing it requires sufficient system RAM. Connection leaks often occur in frameworks that don't properly return connections to the pool. Use monitoring tools like pg_stat_statements to identify slow queries that hold connections longer than needed. For serverless deployments with Vercel or similar, connection pooling is essential because each function invocation would otherwise create a new connection.
Frequently asked questions
- What does "Too many connections" mean?
- This error occurs when PostgreSQL server has reached its maximum allowed concurrent connections (default: 100). When a new connection attempt arrives and all available slots are occupied, the server rejects it with a FATAL error. This is a safety mechanism to prevent the server from being overwhelmed by too many simultaneous clients, which would degrade performance and consume excessive memory and CPU resources.
- How do I fix "Too many connections"?
- PostgreSQL rejects new connections when the max_connections limit is reached. Fix this by tuning max_connections, implementing connection pooling with PgBouncer, or terminating idle sessions.
- What causes "Too many connections"?
- The max_connections parameter is set too low for your workload Connection pooling is not in use, causing each client to hold a dedicated connection Application code is not properly closing connections, leading to connection leaks
- Is the "Too many connections" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Documentation - Connections and Authentication(Official Docs)
- ๐PostgreSQL Error Codes(Official Docs)