An incorrect entry in the configuration file or firewall restriction generally triggers PostgreSQL connection refused error.
As a part of our PostgreSQL Management Services, we help webmasters and web hosts resolve PostgreSQL such as this every day.
Today let’s discuss the top reasons for connection refused error in PostgreSQL and how to resolve the error.
What triggers PostgreSQL connection refused error?
PostgreSQL is a highly extensible and handy database management system. And thus it is one of the popular database systems.
But many times, when users try to connect to psql it ends up in connection errors. psql is the interactive terminal-based program, that enables PostgreSQL queries.
A typical error message looks like
Error: psql: could not connect to server: Connection refused Is the server running on host “xxx.xxx.xxx.xxx” and accepting TCP/IP connections on port 5432?
When the PostgreSQL’s server daemon is not running in the server, it can trigger connection refused error. At the same time, several other reasons can also trigger this error.
Let’s discuss a few reasons why the psql service fails. We will also discuss how our Support Engineers fix this error.
PostgreSQL service status
First parameter to cross-check during a psql connection refused error is the status of the PostgreSQL service.
We can confirm this by cross-checking if a Postgres process in running in the server. In a Linux like environment, we can use
ps -ef | grep postgres
service postgresql status
If the service is not running in the server, then we restart it using the command.
service postgresql restart
At times, this failure could be a result of a corrupted Postgresql service. A reinstall of the service with the apt-get install or yum install would be required to fix the issue.
Enable TCP/IP connections
If the error is not related to the failed status of the service, it is more likely to be related to TCP/IP connections disabled in the Postgresql configuration file.
To correct this, we need to edit posgresql.conf file. The file location in Ubuntu and Centos is :
In Ubuntu: vi /etc/postgresql/9.5/main/posgresql.conf
in CentOS 7: vi /usr/pgsql-10/share/postgresql.conf
The listen_address parameter in the configuration file determines the restrictions on TCP/IP connections. To allow TCP/IP connections, from all IP addresses it should be set to 0.0.0.0. Alternatively, to allow connections to the specific IP address of the server, listen_address parameter need to be edited with only those IP addresses.
If this configuration is left blank or set to localhost, PostgreSQL will not allow external TCP/IP connections.
Finally, we restart the service to save the changes.
Allow remote host
If the users are not able to connect to psql from a remote IP, then also we get the same error.
In this case, the Support Team edits the postgresql.conf file and allow the IP from which the customer is trying to access.
Finally, we restart the service to save the changes.
Firewall restrictions
At times, the restrictions placed by the server firewall can also trigger connection refused error.
If the psql port 5432 is not open for connection or if there is any restriction on the IP address from which the TCP/IP connection occurs, it may trigger connection refused error.
Our Support engineers cross-check the firewall for any restrictions and then remove them to fix the issue.
[Still, having trouble connecting PostgreSQL? – We will fix it for you.]
Conclusion
In short, several reasons can trigger the PostgreSQL connection refused error. This includes corrupted Postgresql service, firewall restrictions, incorrect entries in the configuration file etc. We saw the top 4 reasons for this error and how our Support Engineers fix them.
0 Comments