【问题标题】:psycopg2/psql unable to connect to the postgres dbpsycopg2/psql 无法连接到 postgres 数据库
【发布时间】:2016-07-31 21:22:55
【问题描述】:

我有两个 VPS:

  • 网络服务器 10.0.0.5
  • 数据库服务器 10.0.0.6

我已经对它们设置了一些防火墙规则:

#webserver to allow for the 10.0.0.6 dbserver
iptables -A INPUT -p tcp -s 10.0.0.6 --dport 5432 -m conntrack --ctstate ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d 10.0.0.6 --sport 5432 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

#dbserver to allow for the 10.0.0.5 webserver
iptables -A INPUT -p tcp -s 10.0.0.5 --dport 5432 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -d 10.0.0.5 --sport 5432 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

我正在使用具有静态 IP 的 Azure VM。我认为我不需要在他们的防火墙上定义任何安全规则,因为流量是管理程序组内部的(我可以从一个虚拟机 ssh 到另一台虚拟机)。

我不能 ./manage.py migrate 我的 django 应用程序,因为 psycopg2 无法连接到数据库服务器。 (我相信我的settings.py 是正确的。)

pg_hba.conf中的相关条目:

#accept connections from the 10.0.0.0 subnet
local   all  10.0.0.0/24    trust

postgresql.conf 中的相关条目:

listen_addresses = 'localhost, 10.0.0.5'

我可以在本地连接到数据库服务器上的psql。我无法从网络服务器通过网络连接psql -h 10.0.0.6 -U postgres -W。只是为了确保它不是防火墙规则,当我从数据库服务器刷新所有规则并尝试从网络服务器连接时,它告诉我:

psql: could not connect to server: Connection refused
Is the server running on host "10.0.0.6" and accepting
TCP/IP connections on port 5432?

nmap 10.0.0.6 -p5432 说:

Starting Nmap 6.47 ( http://nmap.org ) at 2016-04-11 05:42 UTC
Nmap scan report for 10.0.0.6
Host is up (0.0026s latency).
PORT     STATE  SERVICE
5432/tcp closed postgresql

很明显,postgres 并没有像应有的那样监听 5432。我猜我的 pg_hba.conf 或 postgresql.conf 有问题,但我看不到是什么。

编辑:为了以防万一,我在管理程序防火墙中打开了 10.0.0.0/24 子网的 5432 端口。没有什么不同。

【问题讨论】:

  • 您是否启用了 postgres 本身以允许远程连接?检查您的pg_hba.conf
  • 我已经更新了配置信息。感谢您的建议。

标签: django postgresql azure


【解决方案1】:

我正在测试 postgresql.conf,似乎 listen_addresses 是目标地址列表,而不是源地址。将此条目更改为 10.0.0.6 解决了问题。

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 2017-08-20
    • 2017-11-12
    • 1970-01-01
    • 2019-11-17
    • 2020-10-09
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多