31

So I'm using AWS using EC2 and I'm trying to open up a port for Postgresql. In AWS I already have it open:

TCP
Port (Service)      Source                  Action
0 - 65535           sg-92aadda2 (default)   Delete
22 (SSH)            0.0.0.0/0               Delete
80 (HTTP)           0.0.0.0/0               Delete
5432                0.0.0.0/0               Delete

When I do netstat it looks as though the port is listening:

# netstat -an | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN

When I do a localhost nmap I get the following:

 Nmap scan report for localhost (127.0.0.1)
 Host is up (0.000010s latency).
 Not shown: 997 closed ports
 PORT      STATE SERVICE
 22/tcp    open  ssh
 80/tcp    open  http
 5432/tcp  open  postgresql

And here's where the fun begins. When I do an nmap from an alternative host I get the following:

PORT      STATE  SERVICE
22/tcp    open   ssh
80/tcp    open   http
5432/tcp  closed postgresql

I also looked at my iptables to see if I was missing something, but the iptables look empty (which should mean they aren't really doing much)

$ iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             127.0.0.0/8          reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:postgresql
ACCEPT     icmp --  anywhere             anywhere
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP       all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

Am I missing something cause I can't seem to figure out how to access the ip. Whenever I try I get the following error:

Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?

How do I make it so that I can open up the port so that external servers have access to it? Thanks in advance =) Lemme know if you need any additional data.

EDIT: As asked below, I tested telnetting, and I was able to telnet into the localhost, but when attempting from the outside I get:

$ telnet xx.xx.xx.xx 5432
Trying xx.xx.xx.xx...
telnet: Unable to connect to remote host: Connection refused

Also, I double checked and I was properly able to telnet into ssh:

$ telnet xx.xx.xx.xx 22
Trying xx.xx.xx.xx...
Connected to xx.xx.xx.xx.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
4

4 回答 4

54

编辑/etc/postgresql/<version>/main/postgresql.conf并将其设置listen_addresses为您的传出界面或全部。重新启动 postgresql: sudo service postgresql restart

于 2013-07-24T18:12:55.950 回答
47

最后一种方法对我有用(thks Julio):

编辑: postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf

启用或添加:

listen_addresses = '*'

重启数据库引擎:

sudo service postgresql restart


此外,您可以检查文件:pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf

并添加您的网络或主机地址:

host all all 192.168.1.0/24 md5

于 2014-10-09T12:56:46.580 回答
10

如果你编辑了postgresql.confmain/pg_hba.conf仍然有问题,请尝试

sudo ufw allow 5432/tcp

解锁 psql 端口

于 2018-07-30T15:32:50.463 回答
0

如果您使用 docker 连接到主机的 postgresql,则必须使用主机的 ip,您可以通过运行ip addr show docker0希望它对某人有所帮助来获得该 ip。

于 2021-03-23T04:01:53.407 回答