Search code examples
postgresqlpgbouncer

OperationalError: ERROR: pgbouncer cannot connect to server


I'm trying to do python manage.py syncdb on a Django installation, but I keep getting OperationalError: ERROR: pgbouncer cannot connect to server. pgbouncer.log contains lines such as:

2017-09-19 19:44:15.107 1128 LOG C-0x8a9930: mydb/myuser@unix:6432 closing because: pgbouncer cannot connect to server (age=0)
2017-09-19 19:44:15.107 1128 WARNING C-0x8a9930: mydb/myuser@unix:6432 Pooler Error: pgbouncer cannot connect to server
2017-09-19 19:44:15.107 1128 LOG S-0x8c72e0: mydb/[email protected]:5432 new connection to server
2017-09-19 19:44:15.107 1128 LOG C-0x8a9930: mydb/myuser@unix:6432 login failed: db=mydb user=myuser
2017-09-19 19:44:30.108 1128 LOG S-0x8c72e0: mydb/[email protected]:5432 closing because: connect failed (age=15)

In case needed, ps -aef | grep pgbouncer yields:

postgres  1128     1  0 18:38 ?        00:00:00 /usr/sbin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
myuser    1919  1533  0 19:45 pts/0    00:00:00 grep --color=auto pgbouncer

Moreover, grep port /etc/pgbouncer/pgbouncer.ini results in:

;;   dbname= host= port= user= password=
mydb = host=xx.xxx.xxx.xxx port=5432 dbname=mydb
;forcedb = host=127.0.0.1 port=300 user=baz password=foo client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
listen_port = 6432

Lastly, the relevant parts of settings.py contain:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': '/var/run/postgresql',
        'PORT': '6432',
    }

I turned log_connections to on in postgresql.conf, restarted PG and tried again. Here's the relevant lines:

2017-09-20 07:50:59 UTC LOG:  database system is ready to accept connections
2017-09-20 07:50:59 UTC LOG:  autovacuum launcher started
2017-09-20 07:51:00 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:00 UTC LOG:  incomplete startup packet
2017-09-20 07:51:00 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:00 UTC LOG:  connection authorized: user=postgres database=postgres
2017-09-20 07:51:01 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:01 UTC LOG:  connection authorized: user=postgres database=postgres
2017-09-20 07:51:01 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:01 UTC LOG:  connection authorized: user=postgres database=postgres

It seems the connection is going through, but the user and database name is postgres. Those credentials aren't what I supplied in pgbouncer.ini.

However, explicitly adding myuser in the connection string described in pgbouncer.ini leads to:

2017-09-20 09:37:37 UTC FATAL:  Peer authentication failed for user "myuser"
2017-09-20 09:37:37 UTC DETAIL:  Connection matched pg_hba.conf line 90: "local   all             all                                     peer"

Totally stumped.


Solution

  • It seems the mis-configuration emanated from this line in settings.py:

    'PORT': '6432',
    

    I commented it and pgbouncer started working.

    Though I'm not sure 'why'.

    Maybe there's a collision on this port; Pgbouncer and PG coexist on a single server in my case. I've set them up over different VMs in the past without a hitch (and without needing to comment 'PORT': '6432',)