大网数据广告活动
服务器运维问答

postgresql port closed for remote access

· 52CE 我爱测

I'm trying to connect to a PostgreSQL database in an Ubuntu Server, which I have root access, but somehow I can't open port 5432 for remote access, just local. This is what happens when I use "nmap" command on the server (XXX.XXX…

问题原文

原帖链接 · alexandre9865 · CC BY-SA 3.0

I'm trying to connect to a PostgreSQL database in an Ubuntu Server, which I have root access, but somehow I can't open port 5432 for remote access, just local. This is what happens when I use "nmap" command on the server (XXX.XXX.X.XX represents server's IP):

nmap -p 5432 localhost
PORT      STATE  SERVICE 
5432/tcp  open   postgresql


nmap -p 5432 XXX.XXX.X.XX
PORT      STATE  SERVICE 
5432/tcp  closed postgresql

I have already edited the files 'pg_hba.conf' and 'postgresql.conf' but it didn't work.

The changes:

pg_hba.conf file:

# IPv4 local connections:
host    all             all             all            md5
# IPv6 local connections:
host    all             all             all            md5

postgresql.conf file:

listen_addresses = '*'

And when i try to connect with pgAdmin III, this is what it shows me:

Server doesn't listen

could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "XXX.XXX.X.XX" and accepting TCP/IP connections on port 5432?

I have tried to open the port using "ufw" command but it didin't work as well.

Can anyone help me?

已采纳解答

原帖链接 · adambg · CC BY-SA 3.0

Make sure your firewall is not blocking traffic.

Append the following rules to your iptables (change X.X.X.X to your server IP address):

# iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d X.X.X.X  --dport 5432 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A OUTPUT -p tcp -s X.X.X.X --sport 5432 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

If it works, save the iptables and restart it:

# apt-get install iptables-persistent
# /etc/init.d/iptables restart

If you have an external firewall on your network, you should allow the connection there as well.

Security note: opening your PostgreSQL port to public might be a security concern. You should consider limiting the incoming traffic to specific IP address/range by changing the -s 0/0 parameter in the first iptables command to -s X.X.X.X/X

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。