I know it is a duplicate questions but their solution not working,

What I'm doing is, connecting to a remote Postgresql database from my local ubunut machine:

psql -h x.x.x.x -U username

But it gives error:

> psql: could not connect to server: Network is unreachable
    Is the server running on host "x.x.x.x" and accepting
    TCP/IP connections on port 5432?

What I did,

I updated my pg_hba.conf & postgresql.conf files and restarted server using below command:

/etc/init.d/postgresql restart

Which said Ok and restarted

I did NMAP Check from local machine to remote vps shows below output:

root@host:~# nmap x.x.x.x -p 5432
Starting Nmap 5.21 ( http://nmap.org ) at 2015-11-19 15:49 IST
 Nmap scan report for x.x.x.x
 Host is up (0.16s latency).
 5432/tcp filtered postgresql

Output of:

# netstat -plnt |grep 5432

tcp  0 0*  LISTEN      9167/postgres            
tcp6 0 0  :::5432        :::*  LISTEN      9167/postgres

How can i sort the issue?

Edited added iptables -L output: enter image description here

  • Is this rulset from client, server, or some other machine in between? Are both the client and server on same LAN?
    – Khaled
    Commented Nov 19, 2015 at 10:49
  • on client firewall is disabled, this is result of VPS iptables, Postgresql port is already opened there. Commented Nov 19, 2015 at 10:52
  • These are different possibilities like: routing problems, some other firewall. I can't help further unless you provide more details about your setup.
    – Khaled
    Commented Nov 19, 2015 at 10:56
  • @Khaled: Thanks for help, just one question, Is the above setup is ok to access remotly? Shall I ask hosting company for help? Commented Nov 19, 2015 at 11:00
  • Please don't post console output as a screenshot. Copy and paste, and format using the code formatter (the button marked {}).
    – Jenny D
    Commented Nov 19, 2015 at 11:24

1 Answer 1


You did not mention anything about having a firewall between your client and database server, but I can see you have iptables tag.

As the error message says, you need to make sure that you can reach the server x.x.x.x from your client and specifically the TCP port 5432 (default postgresql port).

If you are using iptables on the database machine, you may need to add a rule like:

-I INPUT -p tcp --dport 5432 -j ACCEPT

You can restrict this rule by source IP address by adding -s y.y.y.y.

You can even disable your firewall if you are within a trusted network.

  • Let me edit questions I'm adding iptables info Commented Nov 19, 2015 at 10:37
  • @Ravi: Please, post iptables rules if you want help adding an accept rule for postgresql.
    – Khaled
    Commented Nov 19, 2015 at 10:38
  • thanks you for pointing about iptables rule list, I added a screenshot Commented Nov 19, 2015 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .