“psql: could not connect to server: No route to host” often triggers when users try to connect to PostgreSQL.
A common reason for this error is Firewall restriction. As a part of our Server Management Services, we help our customers with PostgreSQL errors related to regularly.
Today, let us discuss the possible reasons and fixes for this error.
What causes “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” in the format given below:
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.
Other than the reasons above, our Support Engineers have noticed that this error could also trigger due to restrictions in the Firewall or using a different port for PostgreSQL. Let us now discuss the steps to fix this error.
How to fix “psql: could not connect to server: No route to host”
As we saw earlier, 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.
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.
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
[Need help to fix PostgreSQL error? We are available 24*7]
In short, the “could not connect to server: No route to host” triggers while connecting to PostgreSQL due to a number of reasons. Today, we saw how our Support Engineers fix it.