[Fixed]-Django settings when using pgbouncer


From the docs:

pgbouncer is a PostgreSQL connection pooler. Any target application
can be connected to pgbouncer as if it were a PostgreSQL server, and
pgbouncer will create a connection to the actual server, or it will
reuse one of its existing connections.


Have your application (or the psql client) connect to pgbouncer
instead of directly to PostgreSQL server.

The configurations:

pgbouncer.ini: An example pgbouncer.ini with comments about defaults

db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1

listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = userlist.txt
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 100
default_pool_size = 20


"user1" "pass1"

to put in settings.py:

if PRODUCTION == '1':
    #PRODUCTION is set to '1' if in production environment
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'db1',
            'USER': 'user1',
            'PASSWORD': 'pass1',
            'HOST': '/var/run/postgresql',
            # 'PORT': '6432',


In case not using unix socket – you can set HOST : ‘’ or ‘localhost’ if pgbouncer is running locally, or whatever the IP of server pgbouncer will be running on.
From the docs:

If you’re using PostgreSQL, by default (empty HOST), the connection to
the database is done through UNIX domain sockets (‘local’ lines in
pg_hba.conf). If your UNIX domain socket is not in the standard
location, use the same value of unix_socket_directory from
postgresql.conf. If you want to connect through TCP sockets, set HOST
to ‘localhost’ or ‘’ (‘host’ lines in pg_hba.conf). On
Windows, you should always define HOST, as UNIX domain sockets are not

In case of postgreSQL For ENGINE you can use postgresql or postgresql_psycopg2 – there’s difference between the both given your Django version – postgresql_psycopg2 vs posgresql.


All of your DB settings in settings.py should be identical to the settings in your pgbouncer config, except the host in settings.py will point to pgbouncer. You probably need to change 'NAME': 'pgbouncer' to 'NAME': 'db1'. Since you’re using a unix socket the port shouldn’t matter.

Leave a comment