Debian – psql: could not connect to server: Network is unreachable

debianiptablespostgresql

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).
 PORT     STATE    SERVICE
 5432/tcp filtered postgresql

Output of:

# netstat -plnt |grep 5432

tcp  0 0  0.0.0.0:5432   0.0.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

Best Answer

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.