Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
SQLSTATE 53300 is too_many_connections. When a role or database has a CONNECTION LIMIT, the next connection over that limit fails at login with FATAL: too many connections for role "app_user" or FATAL: too many connections for database "shop".
We reproduced both on PostgreSQL 17 with two sessions: session A stays connected, session C shows the open connections in pg_stat_activity, and session B's new connection is refused. psql does not print the SQLSTATE for connection failures, so the server log line from the same moment is shown below the output. The same code is also used for sorry, too many clients already (server-wide max_connections), which is not reproduced on this page.
01The role has CONNECTION LIMIT and its sessions are all in use
app_user was created with CONNECTION LIMIT 1. While session A is connected, pg_stat_activity shows 1 connection for app_user, and session B is refused with too many connections for role "app_user". A connection pool or a few forgotten idle sessions reach the limit the same way.
Setup
CREATE DATABASE shop;
CREATE ROLE app_user LOGIN PASSWORD 'Shop2026' CONNECTION LIMIT 1;CREATE DATABASE
CREATE ROLE$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)SELECT usename, datname, count(*) FROM pg_stat_activity WHERE usename = 'app_user' GROUP BY usename, datname; usename | datname | count
----------+---------+-------
app_user | shop | 1
(1 row)$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database();psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: too many connections for role "app_user"FATAL: 53300: too many connections for role "app_user"SELECT rolname, rolconnlimit FROM pg_roles WHERE rolname = 'app_user'; rolname | rolconnlimit
----------+--------------
app_user | 1
(1 row)ALTER ROLE app_user CONNECTION LIMIT 10;ALTER ROLEpg_roles.rolconnlimit shows the limit (1, while -1 means no limit). After ALTER ROLE app_user CONNECTION LIMIT 10; both A and B connect and pg_stat_activity shows 2 connections.
$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)SELECT usename, datname, count(*) FROM pg_stat_activity WHERE usename = 'app_user' GROUP BY usename, datname; usename | datname | count
----------+---------+-------
app_user | shop | 2
(1 row)02The database has CONNECTION LIMIT
The role has no limit, but the database shop was created with CONNECTION LIMIT 1. The second connection fails with too many connections for database "shop"; the limit counts all users connected to that database.
Setup
CREATE ROLE app_user LOGIN PASSWORD 'Shop2026';
CREATE DATABASE shop CONNECTION LIMIT 1;CREATE ROLE
CREATE DATABASE$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)SELECT usename, datname, count(*) FROM pg_stat_activity WHERE usename = 'app_user' GROUP BY usename, datname; usename | datname | count
----------+---------+-------
app_user | shop | 1
(1 row)$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database();psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: too many connections for database "shop"FATAL: 53300: too many connections for database "shop"SELECT datname, datconnlimit FROM pg_database WHERE datname = 'shop'; datname | datconnlimit
---------+--------------
shop | 1
(1 row)ALTER DATABASE shop CONNECTION LIMIT 20;ALTER DATABASECheck pg_database.datconnlimit, then raise it with ALTER DATABASE shop CONNECTION LIMIT 20;. Both sessions then connect.
$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)$ PGPASSWORD='Shop2026' psql -h 127.0.0.1 -U app_user -d shopSELECT current_user, current_database(); current_user | current_database
--------------+------------------
app_user | shop
(1 row)SELECT usename, datname, count(*) FROM pg_stat_activity WHERE usename = 'app_user' GROUP BY usename, datname; usename | datname | count
----------+---------+-------
app_user | shop | 2
(1 row)FAQ
How do I fix too many connections for role in PostgreSQL?
Check the role's limit with SELECT rolname, rolconnlimit FROM pg_roles; and the current connections in pg_stat_activity. Close unused sessions or raise the limit with ALTER ROLE name CONNECTION LIMIT n; (-1 = no limit).
How do I see who is using the connections?
SELECT usename, datname, count(*) FROM pg_stat_activity GROUP BY usename, datname; shows connections per user and database, as in the check step on this page.
What is the difference between too many connections for role and for database?
The first is the role's CONNECTION LIMIT (pg_roles.rolconnlimit), the second is the database's (pg_database.datconnlimit). Both use SQLSTATE 53300; the message names which one was hit.
Is sorry, too many clients already the same error?
It has the same SQLSTATE 53300 but comes from the server-wide max_connections, not a role or database limit. That case is not reproduced on this page.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12