【问题标题】:Connection refused with postgresql using psycopg2使用 psycopg2 拒绝与 postgresql 的连接
【发布时间】:2017-02-17 03:53:44
【问题描述】:

psycopg2.OperationalError:无法连接到服务器:连接被拒绝

服务器是否在主机“45.32.1XX.2XX”上运行并接受端口 5432 上的 TCP/IP 连接?

在这里,我已经打开了我的插座。

tcp        0      0 127.0.0.1:5432          0.0.0.0:*  LISTEN      11516/postgres                
tcp6       0      0 ::1:5432                :::*       LISTEN      11516/postgres

我google了一下,我应该修改这个pg_hba.conf,但是在我的postgresqlroot文件中,我根本没有找到这个文件。

我也成功连接了另一台服务器。

谢谢。

这里,我已经修改了pg_hba.conf,更新了这个host all all 218.3.A.B trust并重新加载。但它也没有工作。

【问题讨论】:

  • PostgreSQL 在什么操作系统下运行?
  • 在 Ubuntu 14.04 中

标签: python postgresql python-3.x


【解决方案1】:

您的 netstat 输出显示 postgres 正在侦听 127.0.0.1,但您的错误表明您正在尝试连接到 45.32.1XX.2XX。我很确定您已经诊断出您的问题。

您需要修改listen_addresses 设置您的postgresql.conf 文件(不是 pg_hba.conf)。 postgresql.conf 文件位于您的 postgresql 数据目录中,通常类似于 /var/lib/postgresql/data/var/lib/pgsql/data

listen_addresses 参数记录在 here

【讨论】:

  • 非常感谢。最后我将这个listen_addresses = '*' 附加到postgresql.conf 中。效果很好。
【解决方案2】:
1. Modify two configure files
# vi /var/lib/pgsql/data/postgresql.conf
 Replace the line:
listen_addresses = 'localhost'  -> listen_addresses = '*'
# vi /var/lib/pgsql/data/pg_hba.conf
 Add the line at the very end:
host all all 0.0.0.0/0 trust
(If IPv6:
host all all ::/0 trust) 
2. Restart the database service
# service postgresql restart
3. Disable the firewall
# rcSuSEfirewall2 stop
# chkconfig SuSEfirewall2 off
# chkconfig SuSEfirewall2_init off

【讨论】:

  • 接受的答案对我不起作用。但是在修改pg_hba.conf 文件后,它起作用了。另外,我在 postgresql 11
猜你喜欢
  • 2016-10-02
  • 2017-08-05
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多