Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql (remote) connection refused

My error:

# psql -U postgres -h 10.230.5.51
psql: could not connect to server: Connection refused
        Is the server running on host "10.230.5.51" and accepting
        TCP/IP connections on port 5432?

in postgresql.conf:

listen_addresses = '*'
port = 5432                             # (change requires restart)

I have added the client server in pg_hba.conf (username/database replaced by X)

host    X          X          10.230.5.21             md5
host    X          X          10.230.5.22             md5

Before I added those in the pg_hba.conf, it gave me this error:

# psql -U postgres -h 10.230.5.51
psql: FATAL:  no pg_hba.conf entry for host "10.230.5.22", user "X", database "X", SSL on
FATAL:  no pg_hba.conf entry for host "10.230.5.22", user "X", database "X", SSL off

So I assume the first steps I have taken are correct?

The last step I feel like I may be missing, could be iptables. they look like this:

-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 ! -i lo -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 10.230.4.0/22 -j LOCAL
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8010 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10443 -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p gre -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-port-unreachable
-A LOCAL -p udp -m state --state NEW -m udp --dport 53 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT
-A LOCAL -p udp -m state --state NEW -m udp --dport 123 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 3888 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 7000 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 7001 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 9042 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 9160 -j ACCEPT

Is the iptables ok? I can see that port 5432 is on the list. Is the connection being refused by another rule before that? iptable commands are weird.

General information:

Both computers are running linux. I did not originally setup the database. The database is running fine, but only locally, even though this happened when running the command on the server with postgres database:

# psql -U postgres -h localhost
psql: 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?
like image 728
Nixxon Avatar asked Sep 05 '25 19:09

Nixxon


2 Answers

I faced the same problem in PostgreSQL 9.6 and I resolved in the following way.

  1. sudo nano /etc/postgresql/9.6/main/postgresql.conf

  2. put '*' instead of localhost on the following line and removed comment sign(#)

    listen_addresses = '*'
    
  3. Add the following line in pg_hba.conf

    #TYPE DATABASE USER CIDR-ADDRESS METHOD

    host  all  all 0.0.0.0/0 md5
    
  4. Restart the service:
    (Ubuntu) sudo service postgresql restart

like image 159
Shree Prakash Avatar answered Sep 09 '25 00:09

Shree Prakash


Do following

Update : /var/lib/pgsql/<version>/data/postgresql.conf

change : #listen_addresses = 'localhost' to listen_addresses = '*'

restart service

like image 32
Riddhi Gohil Avatar answered Sep 09 '25 01:09

Riddhi Gohil