title: Django settings when using pgbouncer
body: <p>I have a Django website with Postgresql backend, for which I'm utilizing <code>pgbouncer</code> for db connection pooling (transaction mode).</p>
  
  <p>The application and the DB reside on separate servers (1 server each). I have installed <code>pgbouncer</code> on the application server. My question is: what should the config be in <code>settings.py</code>? Note that I'm using <strong>Unix sockets</strong> for connecting to pgbouncer.</p>
  
  <hr>
  
  <p>My current <code>settings.py</code> contains:</p>
  
  <pre><code>DATABASE_URL = 'postgres://user1:pass1@xx.xxx.xxx.xxx:5432/db1'
  DATABASES = {
  'default': dj_database_url.config(default=DATABASE_URL)
  }
  </code></pre>
  
  <p>Relevant sections of <code>pgbouncer.ini</code> are:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p><code>userlist.txt</code> contains:</p>
  
  <pre><code>"user1" "pass1"
  </code></pre>
  
  <hr>
  
  <p><strong>Note:</strong> One answer <a href="https://stackoverflow.com/questions/27418264/ideal-settings-for-pgbouncer-with-djangos-conn-max-age">is here</a>, 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 <code>default = '...'</code>.</p>
  
  <p>One suggestions seems to be to treat <code>pgbouncer</code> as a database in <code>settings.py</code>. In that case, would something like the following work?</p>
  
  <pre><code>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',
          }
  </code></pre>