PostgreSQL “Could not connect to server” Error: How to troubleshoot

The PostgreSQL error “Could not connect to server” can happen for various reasons. Often it is enough to restart the open source database management system or adjust the TCP/IP settings.

Requirements

  • Cloud Server running Linux (CentOS 7 or Ubuntu 16.04)
  • PostgreSQL installed and running.

vServer (VPS) from IONOS

Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.

100 % SSD storage
Ready in 55 sec.
SSL certificate

“Could not connect to server: No such file or directory”

The PostgreSQL error “Could not connect to server: No such file or directory” usually means that PostgreSQL is not running. However, the error is actually often related to issues with permission.

First, use the systemctl status postgresql command to check PostgreSQL’s status:

user@localhost:~# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: **active** (exited) since Thu 2017-03-23 21:34:03 UTC; 14s ago
 Main PID: 24289 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/postgresql.service
Mar 23 21:34:03 localhost.localdomain systemd[1]: Starting PostgreSQL RDBMS...
Mar 23 21:34:03 localhost.localdomain systemd[1]: Started PostgreSQL RDBMS.
Mar 23 21:34:08 localhost.localdomain systemd[1]: Started PostgreSQL RDBMS.

If the status is shown as active, restart PostgreSQL with the systemctl restart postgresql command. If the status is shown as inactive, start PostgreSQL with the systemctl start posgresql command.

If a restart does not fix the problem, look at the permissions of your directory with the path /var/lib/postgresql/9.6/main. The version number 9.6 may differ depending on your installation. It is expected that permissions for folders are set to “0700” and permissions for files are set to “0600”. This means that folders must have read, write, and execute permissions, and files must have read and write permissions. Use the ls command in the path above to display current permissions.

If you find permissions are different, run the following commands in the command line:

sudo chown -R postgres:postgres /var/lib/postgresql/9.6/
sudo chmod -R u=rwX,go= /var/lib/postgresql/9.6/

Restart PostgreSQL as shown above.

“Could not connect to server: Connection refused”

Another variant of the PostgreSQL error is “Could not connect to server: Connection refused”.

First, use the systemctl-status postgresql command to verify that PostgreSQL is running. To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql.

If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.

To correct this, edit your posgresql.conf file. These can be found in one of the following file paths, depending on your Linux distribution:

  • Ubuntu 16.04: sudo nano /etc/postgresql/9.5/main/posgresql.conf
  • CentOS 7: sudo nano /usr/pgsql-10/share/postgresql.conf

Check the listen_address configuration. To allow TCP/IP connections, it should be set to “0.0.0.0” (to allow connections from all IP addresses) or to the specific IP address of the server it will allow to connect.

If this configuration is left blank or set to localhost, PostgreSQL will not allow external TCP/IP connections. This also corresponds to the default setting of PostgreSQL.

PostgreSQL will not be able to connect to the server when the connection is blocked by a firewall. Note that all Cloud Servers are affected by the default Firewall Policy which is controlled from the Cloud Panel.

Cloud backup from IONOS

Make costly downtime a thing of the past and back up your business the easy way!

Simple
Secure
Integrated
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.