24

I'm trying to connect to a remote psql database. Before I added the pg_hba.conf entry with the client's IP address, I was getting an error message :

xdev@xdevbox:~$ psql -U postgres testdb -h 10.1.1.47
psql: FATAL:  no pg_hba.conf entry for host "10.201.50.71", user "postgres", database "testdb", SSL off

I added the client's IP with trust settings. I also changed the listen address in postgres.conf on the server to listen to "*". Then I restarted the database server using /etc/init.d/postgresql restart command.

Now when I try to connect, I get the following error message:

psql: could not connect to server: Connection refused
    Is the server running on host "10.1.1.47" and accepting
    TCP/IP connections on port 5432?

in postgresql.conf, the port is set to 5432. I'm not sure what else to check.

Thanks

dot
  • 781
  • 5
  • 11
  • 22

2 Answers2

35

You have to configure the following two files

pg_hba.conf

host all all 0.0.0.0/0 md5

postgresql.conf

listen_addresses='*'

You have to check if the port 5432 is open: http://www.yougetsignal.com/tools/open-ports/

If it's not then add a rule to your iptables:

iptables -A INPUT -s 0/0 -p tcp --dport 5432 -j ACCEPT

0/0: If you want anybody to access it. You can change it to a specific IP address or range of IP addresses.

András Váczi
  • 31,778
  • 13
  • 102
  • 151
Danish Khakwani
  • 476
  • 5
  • 7
2

I do not agree to use

host all all 0.0.0.0/0 md5

if your database is exposed to the internet. In my project postrgres is in the cloud and it is used from the backend. I need to connect directly to the database only for maintenance. I connect to the database from a public IP, say 1.2.3.4. In this case the correct entry is:

host mybackend myuser 1.2.3.4/0 md5

Please note the "/0" after the IP address. Without "/0" you will receive the "Connection refused error".

It is always better to restrict the access to the database specifying the database name, the database user and the ip addresses in the white list. Never use "ALL" unless you are in a developing environment.

Bye!