- SQLSTATE
- 53300
- Condition name
too_many_connections- Product
- PostgreSQL
- Versions
- PostgreSQL 13 to 18 (reserved-slot wording changed in PostgreSQL 16)
- Last reviewed
- 24 October 2026
On this page
What it means
SQLSTATE 53300 means the server refused a new connection because a connection limit was reached: max_connections for the whole server, the reserved slots, or a CONNECTION LIMIT on the role or database.
max_connections sets the maximum number of concurrent connections to the server (typically 100). When every slot is in use, a new connection fails at once with sorry, too many clients already and SQLSTATE 53300 (too_many_connections). The last few slots are held back: superuser_reserved_connections (default 3) for superusers and, from PostgreSQL 16, reserved_connections (default 0) for roles with privileges of pg_use_reserved_connections. When only reserved slots remain, other roles get one of the "remaining connection slots are reserved" messages instead. Separately, CONNECTION LIMIT on a role or a database caps that role or database, and exceeding it gives "too many connections for role" or "for database"; superusers are not subject to these two limits.
Each PostgreSQL connection is a separate server process with its own memory, and the documentation notes that the server sizes some shared memory directly from max_connections. So the usual cure is not a much higher limit but fewer connections: a connection pool in the application or a pooler such as PgBouncer in front of the server, and closing sessions that sit idle, especially "idle in transaction" ones that also hold locks.
Common causes
- Many application instances (web workers, containers, serverless functions) each open their own connections or their own pool, and together they exceed max_connections.
- Connections are leaked: the application opens them and does not close or return them to the pool, so idle sessions accumulate.
- Sessions left "idle in transaction" by code that started a transaction and never committed, which keep their slots and their locks.
- A traffic spike or slow queries: each request holds its connection longer, so more are open at once.
- A CONNECTION LIMIT set on the role or database (ALTER ROLE / ALTER DATABASE ... CONNECTION LIMIT) that is lower than the application's pool size.
- A managed service plan whose max_connections is small for the instance size.
Diagnose it
Read-only These queries only read the system catalogs (pg_catalog, information_schema) and statistics views such as pg_stat_activity and pg_locks. They do not change data, roles or settings, so they are safe to run first.
What are the limits?
SELECT current_setting('max_connections')::int AS max_connections,
current_setting('superuser_reserved_connections')::int AS superuser_reserved,
current_setting('reserved_connections')::int AS reserved; Ordinary roles can use max_connections minus the two reserved values. reserved_connections exists from PostgreSQL 16; on older versions leave it out.
How many connections are open, and in which state?
A superuser can still connect while ordinary slots are full (that is what the reserved slots are for).
SELECT state, count(*) AS connections
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state
ORDER BY connections DESC; Many idle sessions point to oversized or leaking pools; idle in transaction sessions point to code that does not commit. active close to the limit means real load.
Who holds the connections?
SELECT usename, datname, application_name, client_addr,
count(*) AS connections
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY usename, datname, application_name, client_addr
ORDER BY connections DESC; The applications and hosts with the most connections, which tells you whose pool to shrink or fix.
Which sessions have been idle longest?
SELECT pid, usename, application_name, client_addr, state,
now() - state_change AS in_state_for
FROM pg_stat_activity
WHERE state IN ('idle', 'idle in transaction')
ORDER BY in_state_for DESC; Sessions idle for hours are usually leaked or oversized pools. An "idle in transaction" session that has been open for minutes is a bug to fix in the application.
Are there role or database connection limits?
SELECT rolname, rolconnlimit
FROM pg_roles
WHERE rolconnlimit <> -1;
SELECT datname, datconnlimit
FROM pg_database
WHERE datconnlimit <> -1; -1 means no limit. Any other value is a cap that produces "too many connections for role" or "for database".
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Pool connections
Client or network sideThe lasting fix. Use one connection pool per application process with a sensible maximum, and make sure the sum of all pools stays below the ordinary slots. When there are many processes or serverless functions, put a pooler such as PgBouncer (or the managed service's proxy) between them and the server, so thousands of client connections share a few dozen server connections.
End idle transactions automatically
Changes configurationidle_in_transaction_session_timeout ends sessions that stay idle inside an open transaction for longer than the limit, which frees their slots and locks. Set it for the application role; the value applies to new sessions.
ALTER ROLE app_user SET idle_in_transaction_session_timeout = '5min';
Terminate leaked idle sessions
Changes database stateAs an emergency measure, connect as a superuser (a reserved slot) and end sessions that have been idle in a transaction for a long time. Look at the list from the diagnostics first.
Caution: Terminating a session rolls back its open transaction and the application sees its connection drop. Restrict the WHERE clause to the role and age you mean, and fix the leak afterwards or the slots fill up again.
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND now() - state_change > interval '30 minutes'
AND usename = 'app_user';
Set a CONNECTION LIMIT that matches the pool
Changes configurationA per-role limit keeps one application from taking every slot. Set it a little above that application's total pool size, or raise an existing limit that is lower than the pool. The documentation notes the limit is approximate.
ALTER ROLE app_user CONNECTION LIMIT 40;
Raise max_connections only after pooling
Changes configurationIf the real number of concurrent active sessions needs more slots, raise the limit modestly. It takes effect only after a server restart, and more connections mean more memory per process and more contention, so it is not a substitute for pooling.
Caution: Needs a restart, which disconnects every session. Shared memory grows with max_connections, and work_mem is per operation in each session, so check memory before raising it. On a standby, max_connections must be at least the primary's value.
ALTER SYSTEM SET max_connections = 200;
-- then restart the server
Examples
Every connection slot is in use
With superuser_reserved_connections at its default of 3, ordinary roles see this message once only the reserved slots are free; "sorry, too many clients already" appears when every slot, including the reserved ones, is taken. The documentation describes the reserved slots (Connections and Authentication settings) but does not print the messages, so the text is quoted from the PostgreSQL 18.6 source code. This example needs a running PostgreSQL server (enough concurrent connections to fill max_connections), which PGlite does not provide, so it was not reproduced here.
-- max_connections = 100, superuser_reserved_connections = 3
-- 97 application sessions are already connected; a 98th tries:
-- psql "host=db.example.com dbname=shop user=app_user"
FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute
A role with a CONNECTION LIMIT
The documentation describes CONNECTION LIMIT (CREATE ROLE) but does not print this message, so the text is quoted from the PostgreSQL 18.6 source code. This example needs a running PostgreSQL server (several concurrent logins as one role), which PGlite does not provide, so it was not reproduced here.
ALTER ROLE app_user CONNECTION LIMIT 2;
-- app_user already has 2 sessions; a third login:
-- psql "host=db.example.com dbname=shop user=app_user"
FATAL: too many connections for role "app_user"
All slots taken, superuser slots included
The message text is quoted from the PostgreSQL 18.6 source code; the documentation describes max_connections but does not print it. This example needs a running PostgreSQL server (every connection slot in use), which PGlite does not provide, so it was not reproduced here.
-- Every one of the max_connections slots is in use and another client connects:
-- psql "host=db.example.com dbname=shop user=postgres"
FATAL: sorry, too many clients already
How to prevent it
Budget connections: list every application, job and tool that connects, give each a pool size, and keep the total below max_connections minus the reserved slots, with headroom for deployments (old and new instances overlap). Use a pooler for many small clients and serverless functions.
Set idle_in_transaction_session_timeout for application roles, alert when connections pass about 80 percent of the limit, and keep superuser_reserved_connections so an administrator can always get in to investigate.
Common questions
How do I fix "sorry, too many clients already" in PostgreSQL?
Connect as a superuser (a reserved slot), see who holds the connections in pg_stat_activity, end leaked idle sessions if necessary, then fix the cause: smaller or shared connection pools, a pooler such as PgBouncer, and idle_in_transaction_session_timeout. Raise max_connections only if real concurrent load needs it.
Should I just increase max_connections?
Usually not as the first step. Every connection is a server process with its own memory, and many active connections compete for CPU and locks. A pool that keeps fewer, busier connections normally gives better throughput. Changing max_connections also needs a restart.
What does "remaining connection slots are reserved" mean?
Only the slots kept for superusers (superuser_reserved_connections) or for pg_use_reserved_connections members (reserved_connections) are free, so ordinary roles are refused. The server is effectively full for them.
Learn more on CodeWithSQL
Official documentation
- Connections and authentication settings (max_connections, reserved slots)
- CREATE ROLE (CONNECTION LIMIT)
- The cumulative statistics system (pg_stat_activity)
- Server signaling functions (pg_terminate_backend)
Last technically reviewed 24 October 2026; examples and diagnostics run on PostgreSQL 18.6 documentation; SQL-level parts in PGlite 0.5.11. SQL-level examples were run in PGlite (PostgreSQL compiled to WebAssembly); examples that need a running server are shown as documented in the PostgreSQL documentation.