41

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
16
  • you're missing an actual question, so it's not entirely clear what you want us to answer. Additionally, "Whenever I try I get the following error:" what are you trying that gives you that error? Have you tried to telnet into the server? Also, there's not much in your iptables, but if you have a default deny enabled then nothing is getting to that box EXCEPT what is specified to be allowed in your iptables Commented Jul 24, 2013 at 15:43
  • @RussellUhl Sorry, I thought I had asked it, but apparently not. I've added the question at the end. I have not tried telnetting into the server. I'll try that now. Commented Jul 24, 2013 at 15:45
  • no worries. I edited my comment above to include something on your iptables. Make sure you check that as well Commented Jul 24, 2013 at 15:46
  • 1
    I'm afraid I'm at a loss, sorry. Make sure you close your firewall back up to where it was before until you find a solution Commented Jul 24, 2013 at 16:59
  • 1
    @Aram looks like we were at the same speed with the comment above :)
    – TeTeT
    Commented Jul 24, 2013 at 20:53

4 Answers 4

61

Edit /etc/postgresql/<version>/main/postgresql.conf and set the listen_addresses to your outgoing interface or all. Restart postgresql: sudo service postgresql restart.

2
  • 1
    This was also helping for me: cyberciti.biz/tips/…
    – nikeee
    Commented Mar 30, 2014 at 13:40
  • 4
    Just a comment for anyone doing this: the listen_addresses IP address parameter is the one that posgresql is listening from (the INPUT destination in iptables syntax), not the IP that it is listening to (the INPUT source).
    – Escher
    Commented Apr 29, 2016 at 10:28
56

It works for me the last method (thks Julio):

Edit: postgresql.conf

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

Enable or add:

listen_addresses = '*'

Restart the database engine:

sudo service postgresql restart


Besides, you can check the file: pg_hba.conf

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

And add your network or host address:

host all all 192.168.1.0/24 md5

0
16

If you have edited postgresql.conf and main/pg_hba.conf and still having a problem, please try

sudo ufw allow 5432/tcp

to unblock psql port

0
0

In case you are using docker to connect to the host's postgresql you have to use the host's ip which you can obtain by running ip addr show docker0 hope it helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.