"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.
What triggers "psql: could not connect to server: No route to host" ?
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:
- Database server isn't running.
- Server isn't configured to accept TCP/IP requests on the address shown.
Also, this error could also trigger due to restrictions in the Firewall or using a different port for PostgreSQL.
Methods to fix "psql: could not connect to server: No route to host" ?
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:
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
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