How to fix PostgreSQL server doesn’t listen error
While trying to connect to Postgress you may face this error – Server doesn’t listen.
Error:
This is not an error exactly but an exception that occurs when we are running two or more PostgreSQL servers on a single OS.
The screenshot below is showing that there are three different PostgreSQL servers 9.4, 9.5 and 9.6 with port numbers 5432, 5433 and 5434 respectively.
So, the issue is that when you try to connect db of PostgreSQL 9.6 server it shows SERVER DOESN’T LISTEN error because the PostgreSQL server has been configured on 5434 port number. And our TCP connection is unable to find 5432 port number.
Method to solve this issue:
1. Remove old PostgreSQL servers.
Note: Take the database backup first, So you can restore the db again.
PostgreSQL comes with two useful utilities called pg_dump and pg_restore to backup databases. You can pick following commands for dump and restore the single-single databases.
:~$ pg_dump -U postgresql dbname > dbname.sql :~$ pg_restore -U postgresql -d dbname < /path/dbname.sql
Now remove OLD postgreSQL server(s).
In my case I don’t need PostgreSQl 9.4 and 9.5 servers, So I removed them by following commands
:~$ sudo apt-get --purge remove postgresql-client-9.4 postgresql-9.4 :~$ sudo apt-get --purge remove postgresql-client-9.5 postgresql-9.5
Above command(s) will remove PostgreSQL 9.4 and 9.5 servers along with its configurations files.
2. Check postgresql.conf files for current PostgreSQL server.
/etc/postgresql/9.6/main/postgresql.conf
Open this conf file with VI editor or any other editor you are familiar with and goto the line number 63 and replace the port number from 5434 to default PostgreSQL port number 5432. Check Screenshot below :
Now restart the PostgreSQL service by following commands.
:~$ sudo /etc/init.d/postgresql restart
Now you will be able to connect to server.