The Post­gr­eSQL 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

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 centres
  • 24/7 premium support with a personal con­sult­ant

‘Could not connect to server: No such file or directory’

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

First, use the systemctl status post­gr­esql command to check Post­gr­eSQL’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­gr­eSQL with the systemctl restart post­gr­esql command. If the status is shown as inactive, start Post­gr­eSQL 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­gr­esql/9.6/main. The version number 9.6 may differ depending on your in­stall­a­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­gr­eSQL as shown above.

‘Could not connect to server: Con­nec­tion refused’

Another variant of the Post­gr­eSQL error is ‘Could not connect to server: Con­nec­tion refused’.

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

If this does not fix the problem, the most likely cause of this error is that Post­gr­eSQL is not con­figured 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­bu­tion:

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

Check the listen_address con­fig­ur­a­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­ur­a­tion is left blank or set to localhost, Post­gr­eSQL will not allow external TCP/IP con­nec­tions. This also cor­res­ponds to the default setting of Post­gr­eSQL.

Post­gr­eSQL 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 and easy recovery
  • Intuitive schedul­ing and man­age­ment
  • AI-based threat pro­tec­tion
Go to Main Menu