无法从客户端连接到 PostgreSQL - 错误超时
Cannot connect to PostgreSQL from client - Error timed out
在我尝试连接到我的 PostgreSQL 实例很多天之后,我决定是时候寻求帮助了。
我正在尝试从 Windows 机器连接到我的 PostgreSQL 数据库。
我正在尝试 pgAdmin 4 和 dBeaver,但都无法连接。下面是我在使用 dBeaver 连接时收到的错误的屏幕截图。
我创建的连接是这样的:
我的用户是(\du
):
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
umberto | Superuser, Create role, Create DB | {}
我的数据库(\l
):
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
umberto | umberto | UTF8 | C.UTF-8 | C.UTF-8 |
wondermap | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
不知道在哪里搜索日志来深入研究服务器机器上的这个问题。我唯一能找到的是文件夹 /var/log/postgresql
,我在其中只看到两个非 gzip 文件,但消息指的是我尝试连接前几天。
最后,我的 pg_hba.conf
:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
host all all ::0/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all ::/0 md5
可能是什么问题?
我通常不会查看 postgres 日志来解决连接超时错误,因为如果 postgres 拒绝连接,它们会立即被拒绝而不是给你超时,所以如果你得到一个超时通常意味着它从未到达 postgres,因此日志中不会有任何相关内容。
根据我的经验,连接超时错误通常是由于 windows/networking 问题引起的,例如服务器上(或前面)的防火墙不允许访问端口 5432,或者什么都没有实际上监听端口 5432(可能是 postgres 实际上不是 运行,或者它被配置为监听不同的端口,等等)。
我最喜欢在 Windows 上解决此类连接问题的工具是 portqry。用法是 portqry -n [hostname] -e [port number]
。它将尝试连接到端口 [port number]
上的 [hostname]
并为您提供结果:
Listening
:portqry 能够连接到指定端口上的主机,并且应用程序正在侦听该端口。这就是你想要的。
Not listening
: portqry 能够到达指定端口上的主机,但没有任何东西在该端口上侦听。对于 postgres,这可能是因为该服务不是 运行,或者正在侦听不同的端口。
Filtered
: portqry 无法到达指定端口上的主机。这意味着它实际上被阻止连接,这通常是由于主机上或客户端与主机之间的防火墙导致无法访问该端口上的主机。
如果您将 pgadmin 托管在与您的 postgres 数据库相同的服务器上,请尝试在设置与该数据库的 pgadmin 连接期间将“localhost”插入服务器/主机连接字段,同时尝试“127.0.0.1” .在排除所有网络问题后,这对我来说很奇怪。
就我而言,我试图从 AWS RDS 加载数据库。将我的 IP 地址列入白名单后,错误已解决。
我得到了这个确切的错误。在向 AWS 数据库授予 public 可访问性后,该问题得到解决。可以在AWS控制台查看。
在我尝试连接到我的 PostgreSQL 实例很多天之后,我决定是时候寻求帮助了。
我正在尝试从 Windows 机器连接到我的 PostgreSQL 数据库。
我正在尝试 pgAdmin 4 和 dBeaver,但都无法连接。下面是我在使用 dBeaver 连接时收到的错误的屏幕截图。
我创建的连接是这样的:
我的用户是(\du
):
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
umberto | Superuser, Create role, Create DB | {}
我的数据库(\l
):
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
umberto | umberto | UTF8 | C.UTF-8 | C.UTF-8 |
wondermap | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
不知道在哪里搜索日志来深入研究服务器机器上的这个问题。我唯一能找到的是文件夹 /var/log/postgresql
,我在其中只看到两个非 gzip 文件,但消息指的是我尝试连接前几天。
最后,我的 pg_hba.conf
:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
host all all ::0/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all ::/0 md5
可能是什么问题?
我通常不会查看 postgres 日志来解决连接超时错误,因为如果 postgres 拒绝连接,它们会立即被拒绝而不是给你超时,所以如果你得到一个超时通常意味着它从未到达 postgres,因此日志中不会有任何相关内容。
根据我的经验,连接超时错误通常是由于 windows/networking 问题引起的,例如服务器上(或前面)的防火墙不允许访问端口 5432,或者什么都没有实际上监听端口 5432(可能是 postgres 实际上不是 运行,或者它被配置为监听不同的端口,等等)。
我最喜欢在 Windows 上解决此类连接问题的工具是 portqry。用法是 portqry -n [hostname] -e [port number]
。它将尝试连接到端口 [port number]
上的 [hostname]
并为您提供结果:
Listening
:portqry 能够连接到指定端口上的主机,并且应用程序正在侦听该端口。这就是你想要的。Not listening
: portqry 能够到达指定端口上的主机,但没有任何东西在该端口上侦听。对于 postgres,这可能是因为该服务不是 运行,或者正在侦听不同的端口。Filtered
: portqry 无法到达指定端口上的主机。这意味着它实际上被阻止连接,这通常是由于主机上或客户端与主机之间的防火墙导致无法访问该端口上的主机。
如果您将 pgadmin 托管在与您的 postgres 数据库相同的服务器上,请尝试在设置与该数据库的 pgadmin 连接期间将“localhost”插入服务器/主机连接字段,同时尝试“127.0.0.1” .在排除所有网络问题后,这对我来说很奇怪。
就我而言,我试图从 AWS RDS 加载数据库。将我的 IP 地址列入白名单后,错误已解决。
我得到了这个确切的错误。在向 AWS 数据库授予 public 可访问性后,该问题得到解决。可以在AWS控制台查看。