I have a Django website with Postgresql backend, for which I'm utilizing pgbouncer
for db connection pooling (transaction mode).
The application and the DB reside on separate servers (1 server each). I have installed pgbouncer
on the application server. My question is: what should the config be in settings.py
? Note that I'm using Unix sockets for connecting to pgbouncer.
My current settings.py
contains:
DATABASE_URL = 'postgres://user1:pass1@xx.xxx.xxx.xxx:5432/db1'
DATABASES = {
'default': dj_database_url.config(default=DATABASE_URL)
}
Relevant sections of pgbouncer.ini
are:
[databases]
db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1
listen_addr = *
listen_port = 6432
auth_type = md5
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 200
default_pool_size = 300
userlist.txt
contains:
"user1" "pass1"
Note: One answer is here, but doesn't work for me since the DB isn't available locally in my case. I need to set the DATABASE_URL environment variable, instead of using default = '...'
.
One suggestions seems to be to treat pgbouncer
as a database in settings.py
. In that case, would something like the following work?
if PRODUCTION == '1':
#PRODUCTION is set to '1' if in production environment
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'pgbouncer',
'USER': 'user1',
'PASSWORD': 'pass1',
'HOST': '/var/run/postgresql',
'PORT': '6432',
}