繁体   English   中英

无法使用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

因此,如果我确定我有正确的数据库名称,其中包含空格,例如我的示例中的“ABC”,以及正确的用户名/密码/主机/端口,为什么我无法连接? 另外,为什么没有错误传递到异常处理程序? 我正在使用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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM