The Post­greSQL error “Could not connect to server” can happen for various reasons. Often it is enough to restart the open source database man­age­ment system or adjust the TCP/IP settings.

Re­quire­ments

  • Cloud Server running Linux (CentOS 7 or Ubuntu 16.04)
  • Post­greSQL installed and running.
VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centers
  • 24/7 premium support with a personal con­sul­tant

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

The Post­greSQL error “Could not connect to server: No such file or directory” usually means that Post­greSQL is not running. However, the error is actually often related to issues with per­mis­sion.

First, use the systemctl status post­gresql command to check Post­greSQL’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 Post­greSQL with the systemctl restart post­gresql command. If the status is shown as inactive, start Post­greSQL with the systemctl start posgresql command.

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

If you find per­mis­sions 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 Post­greSQL as shown above.

“Could not connect to server: Con­nec­tion refused”

Another variant of the Post­greSQL error is “Could not connect to server: Con­nec­tion refused”.

First, use the systemctl-status post­gresql command to verify that Post­greSQL is running. To be sure that Post­greSQL is running, you can also restart it with systemctl restart post­gresql.

If this does not fix the problem, the most likely cause of this error is that Post­greSQL is not con­fig­ured to allow TCP/IP con­nec­tions.

To correct this, edit your posgresql.conf file. These can be found in one of the following file paths, depending on your Linux dis­tri­b­u­tion:

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

Check the listen_address con­fig­u­ra­tion. To allow TCP/IP con­nec­tions, it should be set to “0.0.0.0” (to allow con­nec­tions from all IP addresses) or to the specific IP address of the server it will allow to connect.

If this con­fig­u­ra­tion is left blank or set to localhost, Post­greSQL will not allow external TCP/IP con­nec­tions. This also cor­re­sponds to the default setting of Post­greSQL.

Post­greSQL will not be able to connect to the server when the con­nec­tion is blocked by a firewall. Note that all Cloud Servers are affected by the default Firewall Policy which is con­trolled from the Cloud Panel.

Cloud Backup powered by Acronis
Mitigate downtime with total workload pro­tec­tion
  • Automatic backup & easy recovery
  • Intuitive sched­ul­ing and man­age­ment
  • AI-based threat pro­tec­tion
Go to Main Menu