无法使用 psycopg2 连接到远程 postgreSQL
Can't connect to remote postgreSQL using psycopg2
我正在尝试使用以下代码连接到远程 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",以及正确的 username/password/host/port,为什么我无法连接?另外,为什么没有错误传递给异常处理程序?我正在使用 python 2.7.9。这是输出,对于任何 psycopg2.connect 语句都是相同的:
I am unable to connect to the database
None
None
您应该在 e
异常中看到更多信息,并且还可以使用非常有用的 traceback
模块:
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()
我正在尝试使用以下代码连接到远程 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",以及正确的 username/password/host/port,为什么我无法连接?另外,为什么没有错误传递给异常处理程序?我正在使用 python 2.7.9。这是输出,对于任何 psycopg2.connect 语句都是相同的:
I am unable to connect to the database
None
None
您应该在 e
异常中看到更多信息,并且还可以使用非常有用的 traceback
模块:
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()