postgresql 无法从远程主机连接到postgres

x4shl7ld  于 12个月前  发布在  PostgreSQL
关注(0)|答案(7)|浏览(166)

我有一个数据库服务器(192.168.1.50)运行postgres。我已经创建了一个名为“testdb”的数据库和一个用户“testuser”,密码为“testuserpw”。
在本地,我可以使用以下命令连接到db:

psql -d testdb -U testuser

当我从另一台主机(192.168.1.60)发出命令时:

psql -h 192.168.1.50 -d testdb -U testuser

我有错误:

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

有什么想法吗?

wmomyfyw

wmomyfyw1#

检查postgresql.conf文件中的listen_addresses设置。许多发行版将其默认为127.0.0.1,即只监听来自localhost的连接。它应该设置为'*'以侦听所有接口上的连接。
如果仍然有问题,可以使用lsof查看postgres进程正在监听哪些网络套接字。

lnvxswe2

lnvxswe22#

在Ubuntu上,我注意到远程访问在某个时候停止工作(目前使用9.1.9)。原因是,postgres不再使用-i开关[1]启动,因此无论您为listen_addresses配置什么,它都将被忽略。
幸运的是,在/etc/environment中添加以下行可以解决注销并再次登录(或重新启动)后的问题:

PGOPTIONS="-i"

[2]查看更多选项。请注意,将此添加到/etc/postgresql/9.1/main/environment对我不起作用。
现在,当执行nmap ip-of-my-remote-server时,我终于又得到了这个:

5432/tcp open  postgresql

好耶!
[1][http://www.postgresql.org/docs/9.1/static/runtime-config-short.html](http://www.postgresql.org/docs/9.1/static/runtime-config-short.html)
[2][http://www.postgresql.org/docs/9.1/static/libpq-envars.html](http://www.postgresql.org/docs/9.1/static/libpq-envars.html)

fkvaft9z

fkvaft9z3#

防火墙允许连接通过吗?或者,检查pg_hba.conf是否允许从localhost以外的地址连接。

3ks5zfa0

3ks5zfa04#

我遇到了和你一样的问题,我的问题来源是防火墙设置。如果您使用的是Ubuntu,请打印您的防火墙状态:sudo ufw status verbose
它可能看起来像这样:

Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
...

传入连接的默认规则是“deny”,因此您需要指定“allow“ed端口。
型号:sudo ufw allow 5432/tcp
参考:https://www.vultr.com/docs/how-to-configure-ufw-firewall-on-ubuntu-14-04

n3schb8v

n3schb8v5#

postgresql.conf中的listen_address函数并不是让postgres监听非本地IP地址的唯一方法。
如果从pg_ctl启动postgres,请使用选项“-o -h *”,否则请将"-h" "*"添加到postgres命令行,例如。

/usr/local/pgsql/bin/postgres -D /pg/data "-h" "*"

当然,/pg/data必须更改为您当前的数据路径。
这在实验时尤其有用。

0x6upsns

0x6upsns6#

连接被拒绝(0x 0000274 D/10061)我已在此处修复,并使用:
打开终端并键入:

VIM /var/lib/pgsql/data/postgresql.conf

编辑“listen_adresses”,它应该设置为“*”

在此之后,将其放置在终端上:

/scripts/restartsrv_postgres
tnkciper

tnkciper7#

我在Ubuntu 22.04,PostgreSQL 16安装时也遇到了同样的问题,并且卡住了很长一段时间。除了与 *. conf文件有关的其他答案外,我还必须使用以下命令配置防火墙:

root# firewall-cmd --zone=public --add-port=5432/tcp --permanent
success
root# firewall-cmd --reload
success

我在这个网页上找到了这个:https://www.project-open.com/en/howto-postgresql-port-secure-remote-access
我希望它能对你有帮助,并祝你(和其他有关的人)“好运”!

相关问题