Error Encyclopedia

Database Connection Pool Exhausted Error

Fix 'connection pool exhausted' or 'too many connections' errors in PostgreSQL, MySQL, and other databases.

What Does This Error Mean?

The 'connection pool exhausted' error means all database connections in the pool are in use and no new connections can be opened. This typically happens under high load or when connections are not properly released back to the pool.

Common Causes

1

Connection pool size is too small for the concurrent request volume

2

Transactions or queries that hold connections for too long

3

Connection leaks — connections are not released back to the pool

4

Serverless functions creating new pools on every invocation

5

Slow queries tying up connections while waiting for results

6

Database server max_connections limit reached

How to Fix It

Increase pool size

Configure a larger pool size based on your expected concurrency.

// Prisma
const prisma = new PrismaClient({
  datasources: { db: { url: process.env.DATABASE_URL } },
  // connection_limit defaults to num_cpus * 2 + 1
})

// node-postgres
const { Pool } = require("pg")
const pool = new Pool({
  max: 20, // maximum connections in pool
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 5000,
})

Close idle connections

Configure idle timeout to release unused connections.

// Prisma: set connection limit in datasource
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  // connection_limit = 10
}

// PostgreSQL: set statement timeout
ALTER DATABASE mydb SET statement_timeout = '30s';

Diagnose connection usage

Check how many connections are active and what queries are running.

-- PostgreSQL: check active connections
SELECT pid, state, query_start, query
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY query_start DESC;

-- Check total connections
SELECT count(*) FROM pg_stat_activity;

-- Kill idle connections
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle'
AND query_start < NOW() - INTERVAL '5 minutes';

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: