"psql: could not connect to server: No route to host" often happens when users try to connect to PostgreSQL.
A common reason for this error is Firewall restriction.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related PostgreSQL queries.
While connecting through pgAdmin users often notice the "could not connect to server: No route to host".
Some of the common reasons for pgAdmin to show this error include:
Also, this error could also trigger due to restrictions in the Firewall or using a different port for PostgreSQL.
1. Restart PostgreSQL
This error could trigger when the PostgreSQL database is not working. We can fix this by performing a restart of the service.
$ sudo service postgresql restart
Once we restart the service, try reconnecting to PostgreSQL.
2. Accept TCP/IP requests
For security reasons, a PostgreSQL server "out of the box" doesn't listen on TCP/IP ports. Instead, it must be enabled to listen for TCP/IP requests.
We need to edit the /var/lib/pgsql/data/postgresql.conf to enable this. Add the following line in the conf file to make the server accept connections on any IP interface:
listen_addresses=’*’;
3. Firewall restrictions
A common reason for this error is the firewall restrictions blocking the connections to the port 5432. We may try a telnet request to the port 5432 to check if t is reachable:
telnet 192.x1.xx.x1 5432
Trying 192.x1.xx.x1...
telnet: connect to address 192.x1.xx.x1: No route to host
We may need to run the following code to allow incoming connections through the OS firewall in Redhat\CentOS for postgresql default port 5432:
firewall-cmd --add-service=postgresql --permanent
This article covers how to fix the "could not connect to server: No route to host" PostgreSQL error. Basically, the "could not connect to server: No route to host" triggers while connecting to PostgreSQL due to a number of reasons.
Make the Iptables rules different in Master and Slave.
iptables -L INPUT
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD