What's Actually Happening

PgBouncer rejects new client connections because maximum limit reached. Applications cannot connect to PostgreSQL.

The Error You'll See

```bash $ psql -h pgbouncer -U myuser -d mydb

psql: error: connection to server at "pgbouncer" (10.0.0.1), port 6432 failed: FATAL: sorry, too many clients already ```

Why This Happens

  1. 1.max_client_conn too low
  2. 2.Connection leak
  3. 3.Pool exhausted
  4. 4.Long-running transactions
  5. 5.Insufficient pool size

Step 1: Check Current Connections

bash
psql -h pgbouncer -U admin -d pgbouncer -c "SHOW CLIENTS;"
psql -h pgbouncer -U admin -d pgbouncer -c "SHOW POOLS;"

Step 2: Check Configuration

bash
cat /etc/pgbouncer/pgbouncer.ini
grep max_client_conn /etc/pgbouncer/pgbouncer.ini
grep default_pool_size /etc/pgbouncer/pgbouncer.ini

Step 3: Increase Client Limit

ini
# In pgbouncer.ini
max_client_conn = 1000
default_pool_size = 20
min_pool_size = 5
reserve_pool_size = 5

Step 4: Check Pool Settings

ini
[databases]
mydb = host=localhost port=5432 dbname=mydb pool_size=30

Step 5: Kill Idle Connections

bash
psql -h pgbouncer -U admin -d pgbouncer -c "SHOW CLIENTS;" | grep idle

Step 6: Restart PgBouncer

bash
systemctl restart pgbouncer
systemctl reload pgbouncer

Step 7: Check File Descriptors

bash
ulimit -n
ls /proc/$(pidof pgbouncer)/fd | wc -l

Step 8: Increase System Limits

bash
# In /etc/security/limits.conf
pgbouncer soft nofile 65535
pgbouncer hard nofile 65535

Step 9: Monitor Connections

bash
watch -n 5 'psql -h pgbouncer -U admin -d pgbouncer -c "SHOW POOLS;"'

Step 10: Check Backend Connections

bash
psql -h localhost -U postgres -c "SELECT count(*) FROM pg_stat_activity;"
  • [Fix PgBouncer Connection Pool Exhausted](/articles/fix-pgbouncer-connection-pool-exhausted)
  • [Fix PostgreSQL Connection Timeout](/articles/fix-postgresql-connection-timeout)