# Database connection in Django

**Published:** January 1, 2023
**Tags:** Backend, Django, Database


---

By default, Django re-establish a new connection to the database in each request. That will be overhead and increase the request time, especially when your database is not on the same server/cluster as your backend server. Fortunately, Django provides us “persistent connection” option to keep and reuse the database connection. A connection pool can also be set up through middle-wares.

## 1. Setup persistent connections

The persistent connection can be enabled in Django by setting [CONN_MAX_AGE](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CONN_MAX_AGE) to a positive number (the timeout of the database connection). The default value is 0, meaning that the database connection is established on a new request and closed right after the request finishes. For unlimited persistent connections, set this value to **None**.

Setting [CONN_MAX_AGE](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CONN_MAX_AGE) to a high value does not mean that the connection to the database will never close. There are other timeouts in the specific DBMS you are using.

Django 4.1 also added [CONN_HEALTH_CHECKS](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CONN_HEALTH_CHECKS) option. Setting this value to `True` will check the connection before using, preventing the closed connection to be used in the request.

Since each thread maintains its own connection, you may need to care about the number of connections to your database if your limit of connections is low (for example, when you are using the **Heroku** database service).

## 2. Setup database connection pool

The pooling mechanism will help you to keep a number of connections (pool size) for shared use in your system. This can be set up by using [**pgbounder**](https://www.pgbouncer.org/) (your application makes requests to **pgbounder** instead of the actual database. This will be the middleman between your Django application and the database.

Another alternative to pgbounder is a Django middleware, for example, **[django-db-connection-pool](https://pypi.org/project/django-db-connection-pool/)** or **[django-postgrespool2](https://pypi.org/project/django-postgrespool2/)**. That kind of middleware can be set up in Django settings, for example, with **[django-db-connection-pool](https://pypi.org/project/django-db-connection-pool/):**

```python
DATABASES = {
    'default': {
        'ENGINE': 'dj_db_conn_pool.backends.postgresql',
				'POOL_OPTIONS' : {
            'POOL_SIZE': 10,
            'MAX_OVERFLOW': 10,
            'RECYCLE': 24 * 60 * 60
        }
    }
}
```

## References

- [https://docs.djangoproject.com/en/4.1/ref/databases/#persistent-connections](https://docs.djangoproject.com/en/4.1/ref/databases/#persistent-connections)
- [https://pypi.org/project/django-db-connection-pool/](https://pypi.org/project/django-db-connection-pool/)
- [https://pypi.org/project/django-postgrespool2/](https://pypi.org/project/django-postgrespool2/)
- **Django Database pooling vs. persistent connections:** [https://groups.google.com/g/django-developers/c/NwY9CHM4xpU](https://groups.google.com/g/django-developers/c/NwY9CHM4xpU)
- **Fixing Database Connections in Django:** [https://www.craigkerstiens.com/2013/03/07/fixing-django-db-connections/](https://www.craigkerstiens.com/2013/03/07/fixing-django-db-connections/)

