简体   繁体   English

Psycopg2和Postgresql的远程连接问题

[英]Remote connection issues with psycopg2 and postgresql

I'm trying to connect to a postgresql database using the following python code: 我正在尝试使用以下python代码连接到Postgresql数据库:

try:
    conn = psycopg2.connect("host = '10.47.65.237' dbname = 'testDB' user = 'pi' password = 'raspberry'")
except:
    print("Unable to connect to testDB at 10.47.65.237. Sending Alert.")

This code works with localhost 127.0.0.1 but when I go to a different machine and try to run it with its ip above it won't connect. 这段代码适用于localhost 127.0.0.1,但是当我进入另一台计算机并尝试以其ip以上的IP运行它时,它将无法连接。

Things I've done: 1. Port 5432 is open 2. edited postgresql.conf by adding the line "listen_addresses='10.47.65.138'" 3. edited pg_hba.conf by adding the following configuration "host all all 10.47.65.138 md5" 我已经做的事情:1.打开端口5432 2.通过添加“ listen_addresses = '10 .47.65.138'”行编辑postgresql.conf。3.通过添加以下配置“托管所有10.47.65.138 md5主机”来编辑pg_hba.conf。 “

Any other things I could try or I'm missing? 还有其他我可以尝试的东西,或者我想念吗?

Running telnet 10.47.65.237 5432 on the client should result in a Connection Refused error, which indicates that the problem has nothing to do with psycopg2. 在客户端上运行telnet 10.47.65.237 5432应该会导致Connection Refused错误,这表明该问题与psycopg2没有关系。

You have misconfigured the server. 您已错误配置服务器。 listen_addresses controls which IPs the server will answer on, not which IPs the server will permit connections from. listen_addresses控制服务器将在哪个IP上应答,而不控制服务器允许从哪个IP连接。 Your server's postgresql.conf should have either listen_addresses='10.47.65.237' or listen_addresses='*' . 您服务器的postgresql.conf应该具有listen_addresses='10.47.65.237'listen_addresses='*' Edit the configuration and restart PostgreSQL on the server, then you should be able to connect successfully using telnet and psycopg2. 编辑配置并在服务器上重新启动PostgreSQL,那么您应该能够使用telnet和psycopg2成功连接。

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

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