0

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

Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • Is this rulset from client, server, or some other machine in between? Are both the client and server on same LAN? – Khaled Nov 19 '15 at 10:49
  • on client firewall is disabled, this is result of VPS iptables, Postgresql port is already opened there. – Ravikumar Sharma Nov 19 '15 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 Nov 19 '15 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? – Ravikumar Sharma Nov 19 '15 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 Nov 19 '15 at 11:24
  • Are you connecting via an IP address, or is xxx.xxx.xxx.xxx a substitute for an FQDN? – Kassandry Nov 21 '15 at 07:31
  • @Kassandry: I have two Ip address on that machine, One public IP, and one Private IP (for LAN), When use VPN and connect with Private IP it works, but I try with Public IP it rejects. (I did research on server and found these details(there are to network interfaces) after Khaled's suggestion) – Ravikumar Sharma Nov 23 '15 at 06:30

1 Answers1

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.

Khaled
  • 36,533
  • 8
  • 72
  • 99