我正在尝试使用以下代码连接到远程的postgreSQL数据库:
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
如果我确定我有正确的数据库名称,例如我的示例中带有空格的“A B C”,以及正确的用户名/密码/主机/端口,为什么我无法连接?另外,为什么没有错误传递到异常处理程序?我正在使用Python 2.7.9。以下是输出,对于任何psycopg2.connect语句都是相同的:
I am unable to connect to the database
None
None
psycopg2
异常都设置了pgcode
或pgerror
。您应该打印e
以查看实际的异常消息。 - Lukas Graf