【问题标题】:Postgres "Connection timed out" when connecting from debian and ubuntu clients, but fine from Windows从 debian 和 ubuntu 客户端连接时 Postgres“连接超时”,但从 Windows 可以正常
【发布时间】:2021-10-23 08:22:17
【问题描述】:

更新

这似乎与 Docker 如何更改 iptables 并保留其设置有关,即使在禁用其 iptables 规则设置功能后也是如此。问题可以通过修改/etc/docker/daemon.json和设置{ "iptables": true }来解决

我刚刚重新安装了 Debian 11。在没有 docker 的情况下安装了 postgres,并且能够连接到我的服务器。我刚刚安装了 docker 并尝试从 postgres 容器连接,但无法访问服务器。这个问题似乎与 docker 周围的防火墙有关。

我通过 docker 在 debian 服务器上运行 postgres 13.4,托管在 DigitalOcean 上。我已经设置了一个非常开放的配置,只是为了看看我是否可以从非本地的各种客户端连接到我的服务器。以下是我配置 pg_hba.conf 文件的方式:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5
# IPv6 local connections:
#host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

postgresql.conf:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'

我从客户端连接的方法是使用 postgres docker 容器,这样我就可以使用 psql 命令,而无需在该客户端上安装 postgres。我尝试了 3 台不同的客户端机器。

来自家庭网络的 Windows 10 工作正常:

root@7eb2296a9cf6:/# psql -h 111.222.143.193 -p 5432 -U postgres
Password for user postgres:

来自家庭网络的 Debian 10 失败:

root@38161cc233c1:/# psql -h 111.222.143.193 -p 5432 -U postgres
psql: error: could not connect to server: Connection timed out
        Is the server running on host "111.222.143.193" and accepting
        TCP/IP connections on port 5432?

来自 Linode 网络的 Ubuntu 20.04 失败:

root@3706fd7cbdd4:/# psql -h 111.222.143.193 -p 5432 -U postgres
psql: error: could not connect to server: Connection timed out
        Is the server running on host "111.222.143.193" and accepting
        TCP/IP connections on port 5432?

这里是 UFW 状态(我已经更改了 IP 以保护隐私)

Status: active

To                         Action      From
--                         ------      ----
787/tcp                    ALLOW       Anywhere
Anywhere                   ALLOW       93.551.148.352
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
5432                       ALLOW       Anywhere
787/tcp (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)
5432 (v6)                  ALLOW       Anywhere (v6)

【问题讨论】:

  • 想打赌这是 IPv4 与 IPv6 的事情吗?是否设置了防火墙并配置为允许流量通过 v4 和 v6 通过端口 5432?您可能还想更改 pg_hba.conf 以允许来自 v6 的远程连接,例如::0/0.
  • @AdrianKlaver 我的客户端都配置为仅使用 IPV4。也就是说,我按照您的建议进行了更改以允许 IPV6 host all all ::0/0 md5 不走运
  • pg_hba.conf 可以筛选连接之前,它需要通过任何防火墙。我只是建议使用 v6 设置来覆盖该协议上的连接。您遇到的错误与防火墙阻止访问一致。验证您是否有一个或多个生效的防火墙以及它们的设置。
  • 我在 Ubuntu 资产上使用 UFW 作为防火墙。我已禁用它并尝试连接但没有运气。我还在客户端开放了 80、443 和 5432 端口,只是因为我没有主意了。
  • 你检查过这个Question 吗?

标签: postgresql docker debian digital-ocean psql


【解决方案1】:

这似乎与 Docker 如何更改 iptables 并在禁用其 iptables 规则设置功能后保留其设置有关。问题可以通过设置/etc/docker/daemon.json和设置{ "iptables": true }来解决

另一种方法是直接使用 iptables。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2019-12-13
    • 2019-03-16
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多