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
Connection pool size is too small for the concurrent request volume
Transactions or queries that hold connections for too long
Connection leaks — connections are not released back to the pool
Serverless functions creating new pools on every invocation
Slow queries tying up connections while waiting for results
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:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.