4
votes

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:*
1
Please check the logs. The PostgreSQL server obviously (netstat output) is not running.Ancoron
I use command "sudo service postgresql status" ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Sun 2019-03-24 21:01:12 MSK; 6min ago Process: 24974 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 24974 (code=exited, status=0/SUCCESS) Mar 24 21:01:12 cs451508 systemd[1]: Starting PostgreSQL RDBMS... Mar 24 21:01:12 cs451508 systemd[1]: Started PostgreSQL RDBMS.Андрей Писарев
Localhost should be open to itself. Are you trying to jump between servers on that port? Or are you connecting on same server to the database?user9105725
On the same serverАндрей Писарев
It seems that server is not running. Below is PostgreSQL log: FATAL: pg_hba.conf rejects connection for host "109.252.90.59" LOG: received fast shutdown request LOG: aborting any active transactions LOG: background worker "logical replication launcher" (PID 421) exited with exit code 1 LOG: shutting downАндрей Писарев

1 Answers

2
votes

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.