Fix PostgreSQL FATAL: sorry, too many clients already
PostgreSQL sorry, too many clients already: all max_connections slots are taken. Use a reserved slot, end idle sessions from pg_stat_activity, add timeouts.
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: sorry, too many clients alreadyEvery connection slot on the server (max_connections, typically 100 by default) is in use, so new connections are refused. Usually it isn't traffic but idle connections and sessions stuck with an open transaction holding the slots. Connect as an admin role, use pg_stat_activity to see who holds them, end them, and set timeouts and pool sizes so it doesn't happen again. Raising max_connections comes last, and it needs a restart.
[1] Read the message to see how full it is. Ordinary roles hit this one first.
FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute| Message | State |
|---|---|
remaining connection slots are reserved for roles with the SUPERUSER attribute | Ordinary slots are gone; only superuser-reserved slots remain |
sorry, too many clients already | Reserved slots are full too — even superusers can't connect |
The reserved count is superuser_reserved_connections (default 3). The test server below reserves 2 of 6, so ordinary roles could use only 4.
$ psql -U postgres -c "SELECT name, setting FROM pg_settings WHERE name IN ('max_connections','superuser_reserved_connections','reserved_connections')" name | setting --------------------------------+--------- max_connections | 6 reserved_connections | 0 superuser_reserved_connections | 2(3 rows)[2] Connect as a superuser while reserved slots remain. If you already see sorry, too many clients already, admins are locked out too; stop one application holding connections, or wait, to free a slot.
$ psql -U postgres -c 'SELECT 1'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: sorry, too many clients already$ psql -U postgres -Atc 'SELECT 1'1The second command ran after one superuser connection ended. If your application connects as a superuser, it eats the reserved slots as well. Give application accounts an ordinary role.
[3] See who holds the slots. Count by role and state, and look at the oldest.
$ psql -U postgres -c "SELECT usename, state, count(*), max(now() - state_change) AS longest FROM pg_stat_activity WHERE backend_type = 'client backend' GROUP BY 1, 2 ORDER BY 3 DESC" usename | state | count | longest ----------+---------------------+-------+------------------ appuser | idle | 2 | 00:00:20.000401 appuser | active | 1 | 00:00:19.724808 appuser | idle in transaction | 1 | 00:00:19.814797 postgres | active | 1 | -00:00:00.000002 postgres | idle | 1 | 00:00:17.397643(5 rows)state | Meaning |
|---|---|
active | Running a query |
idle | Open but doing nothing — held by a pool, or abandoned without closing |
idle in transaction | Stopped after BEGIN with no commit or rollback — may hold locks as well as a slot |
The postgres | active row is your own session running this query. For idle in transaction, find where it came from.
$ psql -U postgres -c "SELECT pid, usename, application_name, client_addr, state, now() - xact_start AS xact_age, left(query, 40) AS query FROM pg_stat_activity WHERE state = 'idle in transaction'" pid | usename | application_name | client_addr | state | xact_age | query -----+---------+------------------+-------------+---------------------+-----------------+----------- 143 | appuser | psql | | idle in transaction | 00:00:19.934335 | SELECT 1;(1 row)query is the last statement that session ran, a clue to where the code forgot to commit. An empty client_addr means a socket connection.
[4] End the offending sessions one PID at a time. pg_terminate_backend(<pid>) rolls back the session's transaction and closes the connection.
This ran on a server restarted with the same settings (6 slots, 2 reserved), so the PIDs differ from [3]:
$ psql -U postgres -c "SELECT pid, usename, datname, application_name, state, now() - state_change AS idle_for FROM pg_stat_activity WHERE state = 'idle in transaction'" pid | usename | datname | application_name | state | idle_for -----+---------+----------+------------------+---------------------+----------------- 118 | appuser | postgres | psql | idle in transaction | 00:00:07.227024(1 row) $ psql -U postgres -Atc 'SELECT pg_terminate_backend(118)'t$ psql -U appuser -d postgres -Atc 'SELECT current_user'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: remaining connection slots are reserved for roles with the SUPERUSER attributeOne session ended, yet the ordinary role still can't get in: the 2 free slots equal the reserved count (2). Superuser sessions use ordinary slots too. Checking and ending the idle superuser connection the same way let it in.
$ psql -U postgres -c "SELECT pid, usename, application_name, state, now() - state_change AS idle_for FROM pg_stat_activity WHERE usename = 'postgres' AND state = 'idle' AND pid <> pg_backend_pid()" pid | usename | application_name | state | idle_for -----+----------+------------------+-------+---------------- 135 | postgres | psql | idle | 00:00:05.52707(1 row) $ psql -U postgres -Atc 'SELECT pg_terminate_backend(135)'t$ psql -U appuser -d postgres -Atc 'SELECT current_user'appuserOn a real server, add a time condition (now() - state_change > interval '10 minutes' and so on) plus usename, datname or application_name to the query to narrow the candidates, then end only the PIDs you picked.
[5] Add timeouts so it doesn't come back. With idle_in_transaction_session_timeout, the server closes a session that sits in an open transaction longer than the limit. You can set it per role.
$ psql -U postgres -c "ALTER ROLE appuser SET idle_in_transaction_session_timeout = '3s'"ALTER ROLE$ (echo 'BEGIN;'; echo 'SELECT 1;'; sleep 5; echo 'SELECT 2;') | psql -U appuser -d postgresBEGIN ?column? ---------- 1(1 row) FATAL: terminating connection due to idle-in-transaction timeoutserver closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.connection to server was lostIn production, allow well over your longest legitimate transaction (minutes). To remove a setting you added, RESET it. Per the ALTER ROLE docs, a role setting becomes the default for sessions started afterwards, so both setting and resetting take effect from new connections. From the restarted test server:
$ psql -U postgres -Atc "SELECT rolconfig FROM pg_roles WHERE rolname = 'appuser'"{idle_in_transaction_session_timeout=3s}$ psql -U postgres -c 'ALTER ROLE appuser RESET idle_in_transaction_session_timeout'ALTER ROLE$ psql -U postgres -Atc "SELECT coalesce(rolconfig::text, '(none)') FROM pg_roles WHERE rolname = 'appuser'"(none)If the role had a different value before, SET it back to the value you noted instead of RESET. Plain idle connections can be closed with idle_session_timeout, but that also cuts connections your pool is holding, so tune the pool's idle limit first.
[6] Count the connections your applications can open. "servers × processes × pool size" must stay below max_connections minus the reserved slots. As instances grow, shrink the pool or put a connection pooler (PgBouncer and the like) in front.
If you still need more, change max_connections and restart. Per the docs this value is set only at server start, so a reload doesn't apply it.
$ psql -U postgres -c 'ALTER SYSTEM SET max_connections = 20' -c 'SELECT pg_reload_conf()' -c "SELECT name, setting, pending_restart FROM pg_settings WHERE name = 'max_connections'"ALTER SYSTEM pg_reload_conf ---------------- t(1 row) name | setting | pending_restart -----------------+---------+----------------- max_connections | 6 | f(1 row)setting stays at 6, and in this test pending_restart was f too. That is most likely because the test server was started with the -c max_connections=6 option: per the docs, command-line options take precedence over postgresql.conf and ALTER SYSTEM, so even after the file value changes, the value after a restart would still be 6. On an ordinary server configured only through files, the docs say pending_restart is t when the value has been changed in the configuration file but needs a restart to apply (not tested here). If your server gets the value from a startup option (some container or service definitions), change that option. Each connection gets its own server process and memory, so work out memory (with work_mem and friends) before a big increase. To undo, run ALTER SYSTEM RESET max_connections and restart. Since PostgreSQL 16, reserved_connections can also keep slots for monitoring or ops accounts granted pg_use_reserved_connections (also restart-only). For the same problem in MySQL, see Fix MySQL ERROR 1040 Too many connections.
For how to split memory as you add connections, see PostgreSQL 17 memory tuning: shared_buffers and work_mem; for dead rows piling up behind long-open transactions, see PostgreSQL autovacuum tuning: n_dead_tup, thresholds, per-table.
Official references: Connection settings — max_connections, reserved_connections, superuser_reserved_connections · pg_stat_activity · pg_terminate_backend · idle_in_transaction_session_timeout, idle_session_timeout