PostgreSQL: Could not connect to server: TCP/IP connections on port 5432

Андрей Писарев picture Андрей Писарев · Mar 24, 2019 · Viewed 24.8k times · Source

I have an error with PostgreSQL:

SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

The following command doesn't show 5432 port is open:

sudo netstat -plunt |grep postgres

I guess that the problem is 5432 port so as I don't see it at netstat.
How to open port for PostgreSQL?

  1. The postgresql.conf and pg_hba.conf files are okay (see below).
  2. postgres server is running
  3. My postgresql.conf file:
listen_addresses = '*'
port = 5432
  1. My pg_hba.conf file:
local   all             postgres                                md5
local   all             all                                     md5
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
  1. I ran the command: $ netstat -atu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:6379          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:https           0.0.0.0:*               LISTEN
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11807 ESTABLISHED
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11258 ESTABLISHED
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11797 ESTABLISHED
tcp6       0      0 localhost:6379          [::]:*                  LISTEN
tcp6       0      0 [::]:http               [::]:*                  LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 [::]:https              [::]:*                  LISTEN
udp        0      0 localhost:domain        0.0.0.0:*
udp        0      0 30secondboomer.c:bootpc 0.0.0.0:*

Answer

MwamiTovi picture MwamiTovi · Nov 25, 2019

Got a similar error while settings up PostgreSQL to work with a Python/Django project.

  1. Try to stop and restart the server again.
  2. If indeed the challenge is port 5432 as you suspect, you can try to start postgres on another port say 5433 and see what happens.
  3. For those, whose database server is not running at all, this solution worked for me, after reading the official PostgreSQL-9.6.16 documentation.