【问题标题】:How to connect postgres using python如何使用python连接postgres
【发布时间】:2017-09-04 10:32:09
【问题描述】:

我尝试通过 followimg 代码使用 python 连接 postgres。但是我无法连接它,我收到了一些错误。

import psycopg2
import sys

def main():
    #Define our connection string
    conn_string = "host='11.20.89.233' dbname='postgres' user='good_user' password='secret'"

    # print the connection string we will use to connect
    print "Connecting to database\n ->%s" % (conn_string)

    # get a connection, if a connect cannot be made an exception will be raised here
    conn = psycopg2.connect(conn_string)

    # conn.cursor will return a cursor object, you can use this cursor to perform queries
    cursor = conn.cursor()
    print "Connected!\n"

if __name__ == "__main__":
    main()

我收到以下错误。

grtmgr@BLR1000005591:~/mppcases> python testcon.py
Connecting to database
        ->host='11.20.89.233' dbname='good_user' user='secret' 
password='mpp_tool'
Traceback (most recent call last):
  File "testcon.py", line 19, in <module>
    main()
  File "testcon.py", line 12, in main
     conn = psycopg2.connect(conn_string)
  File "/usr/lib64/python2.6/site-packages/psycopg2/__init__.py", line 179, 
in connect
    connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "11.20.89.233" and accepting
       TCP/IP connections on port 16100?

【问题讨论】:

  • 您确定您的 postgres 正在侦听端口 16100?这不是标准的。
  • 验证 IP、端口和凭据是否正确
  • 验证位置。 IP 和凭据正确。端口我不知道在哪里验证。
  • 尝试使用标准客户端(如 psql 或 pgAdmin)与他们连接
  • 谢谢你们的帮助。

标签: python database postgresql port


【解决方案1】:

首先,您需要确保使用正确的端口,其次,您需要将 IP 地址添加到 PostgreSQL pg_hba.conf 文件中。

host all all x.x.x.x/32 md5

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2012-08-21
    相关资源
    最近更新 更多