无法通过外部主机连接连接到PostgreSQL服务器

时间:2020-10-27 18:54:50

标签: postgresql psql portforwarding

我的ubuntu PC上有一个运行于Postgresql服务器。

我已经将服务器设置为后转以接受外部连接。

但是由于某些原因,我只能在本地PC上连接它,似乎无法通过外部连接来连接它。

这是我对/etc/postgresql/12/main/pg_hba.conf的设置,

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

这是/etc/postgresql/12/main/postgresql.conf的设置,

listen_addresses = '*'
port = 5432

我已将外部端口设置为2345,并在路由器上完成了端口转发。

运行psql -h 127.0.0.1 -p 5432 -d postgres -U postgres时,我可以连接到服务器。

但是当我运行psql -h xxx.xx.xx.xx -p 2345 -d postgres -U postgres(隐藏IP地址)时,出现以下错误

psql: error: could not connect to server: could not connect to server: Operation timed out
        Is the server running on host "xxx.xx.xx.xx" and accepting
        TCP/IP connections on port 2345?

我也不认为路由器有任何问题,因为我正在使用它转发SSH连接,并且可以很好地从外部连接进行连接。

我的设置出了什么问题?

1 个答案:

答案 0 :(得分:1)

与@anarchy进行了一次对话,他启用了“不复杂防火墙(uwf)”。 向防火墙添加规则可以正常工作。

要检查,请使用网络实用程序打开哪些端口

netstat -lntu

检查UFW是否正在运行

sudo uwf status

如果已启用,请添加您要打开的端口

sudo ufw allow 22

然后重新加载防火墙

sudo ufw reload