// PostgreSQL error reference

PostgreSQL 53300: too many connections

The server has reached max_connections and refuses new sessions. Usually a connection leak or pools that together open more connections than allowed.

SQLSTATE
53300
Name
too_many_connections
Category
Insufficient Resources
Error message
sorry, too many clients already

What it means

PostgreSQL runs one server process per connection and caps the total with max_connections (100 by default). When every slot is taken, a new connection fails with 53300 before it can log in.

A few slots are kept back for superusers (superuser_reserved_connections, 3 by default) and, on PostgreSQL 16 and later, for roles in pg_use_reserved_connections. Ordinary roles then see remaining connection slots are reserved for roles with the SUPERUSER attribute (the wording varies by version) with the same code. A role or database can also have its own CONNECTION LIMIT, which fails with too many connections for role "app".

Common causes

  • An application leaks connections: it opens them and never closes or returns them to the pool.
  • Several app instances, each with its own pool, add up to more than max_connections.
  • Many sessions sitting idle in transaction, holding slots and locks.
  • A traffic spike, or a job that opens one connection per task.

How to fix it

  1. See who is connected and what they are doing (connect as a superuser, who can still get in).

    SELECT usename, application_name, client_addr, state, count(*)
    FROM pg_stat_activity
    GROUP BY 1, 2, 3, 4
    ORDER BY count(*) DESC;
  2. End sessions that are stuck idle in a transaction or that belong to a runaway client.

    SELECT pg_terminate_backend(pid)
    FROM pg_stat_activity
    WHERE state = 'idle in transaction'
      AND state_change < now() - interval '10 minutes';
  3. Fix the leak or shrink the pools so their total stays under the limit, and put a pooler such as PgBouncer in front of many small clients.

  4. Have the server end abandoned sessions automatically.

    ALTER SYSTEM SET idle_in_transaction_session_timeout = '5min';
    SELECT pg_reload_conf();
  5. Raise max_connections only if the server has the memory for it; it needs a restart.

    ALTER SYSTEM SET max_connections = 200;  -- takes effect after restart

Questions

How many connections can PostgreSQL handle?

The default max_connections is 100. Each connection is a separate process with its own memory, so hundreds of mostly idle connections are better served by a connection pooler than by a very high max_connections.

Related errors

More insufficient resources errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.