在python3

时间:2016-02-05 14:34:08

标签: postgresql python-3.x psycopg2

我是psycopg2版本2.6.1 我想获取一些错误,但它没有像预期的那样工作:

try:
    pgconn = psycopg2.connect(database="foobar", host="dbtest.example.de", user="postgresss")
except psycopg2.OperationalError:
    print(psycopg2.OperationalError.pgerror)
   .....:     
<member 'pgerror' of 'psycopg2.Error' objects>

或:

try:                       
    pgconn = psycopg2.connect(database="foobar", host="dbtest.example.de", user="postgresss")
except psycopg2.OperationalError:
    print(psycopg2.OperationalError.diag)
   .....:     
<attribute 'diag' of 'psycopg2.Error' objects>

我怎样才能看到正确的消息,例如&#34;没有pg_hba.conf主机xyz&#34;

1 个答案:

答案 0 :(得分:0)

您需要引用异常实例:

try:
    pgconn = psycopg2.connect(database="foobar", host="dbtest.example.de", user="postgresss")
except psycopg2.OperationalError as e:
    print(e)