Tuesday, November 13, 2012

Postgres pgdb connect with connect timeout

So, here's a little FYI.

I'm trying to connect to a Postgres database and pass in a connection timeout. Whatever the default is seems to be way too short. This is via Python 2.7 on Linux Ubuntu 12.10, if you're interested, but I don't suppose it matters, I'll be doing the same thing in a moment on Centos 6.

I finally figured out how to do this on the command line:

psql connect_timeout=5 -h myhost.namegoeshere.com -p 5432 -U myname

But, I'm trying to do this programmatically from python using pgdb pgdb.connect(). Here's the command I'm trying:

    connString = "%s:%s:%s:%s:%s" % (self.pgServer, 'xxx', 'xxxzzzzx', 'xyyyy', "connect_timeout=5")
    db_connection = pgdb.connect(dsn=connString, host="myhost.namegoeshere.com :5432")
    db_cursor = db_connection.cursor()
    db_cursor.execute("SELECT 42;")
    if 42 not in db_cursor.fetchone():
        raise Exception("Test query failed")


No joy yet.