Skip to content

Too Many Connections

Error message

                too many connections
              

Quick summary

The database server's max_connections limit has been reached — no new connections can be opened.

Why this happens

  • The application is opening too many connections without closing them

  • Connection pool size is too large across multiple app instances

  • Connection leaks — connections are not being returned to the pool

  • max_connections is set too low for the workload

Minimal example

✗ Broken code
// Each request opens a new connection
app.get('/users', async (req, res) => {
  const client = new Client({ connectionString });
  await client.connect(); // never closed!
  const result = await client.query('SELECT * FROM users');
  res.json(result.rows);
});
✓ Fixed code
// Use a shared connection pool
const pool = new Pool({ connectionString, max: 10 });
app.get('/users', async (req, res) => {
  const result = await pool.query('SELECT * FROM users');
  res.json(result.rows);
});

Using a connection pool reuses connections instead of opening a new one per request, preventing connection exhaustion.

How to diagnose

  • Check current connection count: SELECT count(*) FROM pg_stat_activity

  • Check max_connections setting: SHOW max_connections

  • Look for idle connections that are not being used

  • Check for connection leaks in the application

How to fix

  • Close idle connections and fix connection leaks

  • Reduce the connection pool size

  • Increase max_connections on the server

  • Use a connection pooler like PgBouncer to multiplex connections

How to prevent

  • Use connection pooling with appropriate pool sizes

  • Always release connections back to the pool

  • Use a external pooler (PgBouncer, ProxySQL) for many app instances

  • Monitor connection count and alert before the limit is reached

Related Resources

Related Glossary

Related Lessons

← Back to language