Menu

Django + PostgreSQL Connection Pooling: CONN_MAX_AGE, Native Pool, and PgBouncer
📰

Django + PostgreSQL Connection Pooling: CONN_MAX_AGE, Native Pool, and PgBouncer

DEV Community: django·sizan mahmud0·1 day ago
#ObHHoJFj
#devtodjango#devto#dev#pool#connections
Reading 0:00
15s threshold

How to avoid 'too many clients already' with persistent connections, Django 5.1's native pool, and PgBouncer.

If you've seen FATAL: sorry, too many clients already , you've run out of PostgreSQL connections. Here are the three ways to deal with it in Django.

Why it happens

PostgreSQL runs one OS process per connection, so connections are expensive to open and expensive to keep in large numbers. Django's default ( CONN_MAX_AGE = 0 ) opens a connection per request.

1. Persistent connections

DATABASES = { "default" : { # ... "CONN_MAX_AGE" : 60 , "CONN_HEALTH_CHECKS" : True , } }

Enter fullscreen mode

Exit fullscreen mode

Reuse, not a true pool

One connection per thread, held even when idle

Not recommended for ASGI

2. Native pool (Django 5.1+, psycopg 3)

pip install "psycopg[binary,pool]"

Enter fullscreen mode

Exit fullscreen mode

DATABASES = { "default" : { # ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.