1

I know other people have posted similar questions but none of the solutions applied here. I have installed Postgresql-9.6 under CentOS 7 on my virtual private server, and I am trying to connect to the server locally by doing:

psql -U postgres -h [ip_address]

However, I got message like:

could not connect to server: Connection refused Is the server running on 
[ip_address] and accepting TCP/IP connections on port 5432?. 

As suggested by previous posts, I went to postgresql.conf and set:

listen_addresses = '*'      
port = 5432     

also, I edited pg_hba.conf as follows:

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::/0                    md5

By restart the postgresql server, and doing:

netstat -nlt

I got:

Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:6969            0.0.0.0:*               LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 :::5432                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN 

It shows port 5432 is listening. However, I still got same error message when trying to connect to the database locally.

  • Did you try with -h 127.0.0.1? With other user than postgres? – ypercubeᵀᴹ May 20 '18 at 1:36
  • I also tried telnet the server at port 5432 but still, no success. Can anyone tell why from the configuration I have posted? – yimcai May 20 '18 at 4:25
  • 2
    no, could it be a firewall rule blocking it? – Jasen May 20 '18 at 5:08
  • Can you try sudo -u postgres psql (without nothing else) and add the error you'll get? – ypercubeᵀᴹ May 20 '18 at 9:20
  • It turned out to be firewall blocking the port 5432. link – yimcai May 24 '18 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.