DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
'CONN_MAX_AGE': 600, # Keep connections alive for 10 minutes
'OPTIONS': {
'connect_timeout': 10,
'options': '-c statement_timeout=30000' # 30 second query timeout
},
}
}
# Connection pool settings (if using django-db-pool)
DATABASES['default']['ENGINE'] = 'django_db_pool.backends.postgresql'
DATABASES['default']['POOL_OPTIONS'] = {
'POOL_SIZE': 10,
'MAX_OVERFLOW': 20,
'RECYCLE': 3600,
}
Connection pooling reuses database connections instead of creating new ones per request. I use django-db-pool or configure persistent connections with CONN_MAX_AGE. This reduces connection overhead significantly. For high-traffic sites, I set appropriate pool sizes. Too many connections can overwhelm the database. I monitor active connections and adjust settings. Connection pooling works with PostgreSQL's pgbouncer or MySQL's connection pool. This is crucial for production performance, especially with many concurrent requests.