I've a VM on which I installed postgres, now I'm trying to connect the this PG from outside, I created a firewall rule that opens the 5432 port to any source IP like below

enter image description here

My instance has the rule

enter image description here

But when I try to check if the port is open it fails for me

$ nc -zv public-ip 5432
nc: connectx to public-ip port 5432 (tcp) failed: Connection refused
$ nc -zv public-ip 22  
Connection to public-ip port 22 [tcp/ssh] succeeded!
$ psql -h public-ip -p 5432 --username=myuser --dbname=mydb --password 
Password: 
psql: error: connection to server at "public-ip", port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?

I tried restaring the VM but that didn't help. what am I missing?


Solution 1:

Connection refused means you can initiate a TCP connection but no process is listening on the port, so the connection attempt is refused. This means the firewall is probably not the problem. A firewall problem usually results in a Timeout error.

Edit the postgresql.conf configuration file:

listen_addresses = '*'

18.3.1. Connection Settings