pg8000 postgres connection issues from python

disruptive picture disruptive · Jul 16, 2014 · Viewed 8.2k times · Source

I'm having trouble connecting to a db on a new system. The version of Python is the same. Here is the connection string for the DB that is there and you'll see the error message change, but I cannot work out why when the connection string is correct I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pg8000/__init__.py", line 148, in connect
    user, host, unix_sock, port, database, password, socket_timeout, ssl)
  File "/usr/local/lib/python2.7/dist-packages/pg8000/core.py", line 1157, in __init__
    raise exc_info()[1]
TypeError: cannot concatenate 'str' and 'int' objects

Here is my connection string when correct:

conn = DBAPI.connect(host='sql2', user='XXX', password='XX', database='XX', socket_timeout=100, port=5432)

and when I change to something incorrect:

conn = DBAPI.connect(host='sql2', user='XXX', password='XX', database='XX', socket_timeout=100, port=100)

and the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/opt/Centos5.8/python-2.7.4_scipy0.13/lib/python2.7/site-packages/pg8000/__init__.py", line 148, in connect
    user, host, unix_sock, port, database, password, socket_timeout, ssl)
  File "/mnt/opt/Centos5.8/python-2.7.4_scipy0.13/lib/python2.7/site-packages/pg8000/core.py", line 854, in __init__
    raise InterfaceError("communication error", exc_info()[1])
pg8000.errors.InterfaceError: ('communication error', error(111, 'Connection refused'))

I cannot figure out where this error message is coming from - it seems bizzare. I've tried:

wrapping string in str() putting u'ss' removing port, adding socket-timeout etc etc.

When I looked in the core.py code from pg8000 it appears that this comes from an authentication error if you chase it through. I think this error (int and str) might be eclipsing the authentication error.

Message codes

AUTHENTICATION_REQUEST = b("R")

Answer

Tony Locke picture Tony Locke · Jul 20, 2014

This is a bug in pg8000. It was fixed in pg8000-1.9.11. The bug is an error in reporting the error that pg8000 encountered while trying to authenticate against the database.