traggatmot
traggatmot

Reputation: 1463

Can't connect to remote postgreSQL using psycopg2

I am trying to connect to a remote postgreSQL database, using the following code:

import psycopg2


try:

    # this:
    conn = psycopg2.connect(database="A B C", user="user", password="pass", host="yyyy.xxxxx.com", port= 5432)
    # or this:
    conn = psycopg2.connect("dbname=A B C user=user password=pass host=yyyy.xxxxx.com port=5432")
    # or this:
    conn = psycopg2.connect("dbname='A B C' user='user' password='pass' host='yyyy.xxxxx.com' port=5432")

    print "connected"

except psycopg2.Error as e:
    print "I am unable to connect to the database"
    print e.pgcode
    print e.pgerror

So if I am sure I have the correct database name, which has spaces in it like "A B C" in my example, and the correct username/password/host/port, why can I not connect? Also, why does no error get passed onto the exception handler? I am using python 2.7.9. Here is the output, which is the same for any of the psycopg2.connect statements:

I am unable to connect to the database
None
None

Upvotes: 3

Views: 5724

Answers (1)

Michał Niklas
Michał Niklas

Reputation: 54292

You should see more info in e exception and also use very useful traceback module:

import traceback

...

except psycopg2.Error as e:
    print "I am unable to connect to the database"
    print e
    print e.pgcode
    print e.pgerror
    print traceback.format_exc()

Upvotes: 4

Related Questions