Postgres Port 5432 – How to Fix Listening but Unreachable Issue

postgresqltcpUbuntu

I have an Ubuntu VM, hosted with Microsoft Azure. It was completely blank, and then I installed Postgres using sudo apt-get install postgresql.

The version I installed is Ubuntu 10.10-0ubuntu0.18.04.1.

I configured Postgres in order to accept remote connections, by following this tutorial:

  • I edited my postgresql.conf file by setting listen_addresses = '*'.
  • I edited my pg_hba.conf file by adding the two following lines:

    host all all 0.0.0.0/0 md5

    host all all ::/0 md5

And I restarted Postgres, and even the whole server for good measure.

When I execute netstat -tln, I obtain the following result:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.53:53           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
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN

My iptables are totally empty, I did not do anything with it after creating the VM.

From another client, I tried to execute sudo -u postgres psql -h [host] -U postgres, and the request times out.

When I execute telnet [host] 5432 from another client, nothing happens until it times out. However, executing telnet [host] 22 works fine, and executing telnet [host] 80 gets me a connexion refused, which I assume means that my host is at least listening on these ports.

What could I be missing here?

Best Answer

Try switching as postgres user and enter psql on the machine where postgres is installed. This will make sure the installation is correct or wrong.

su - postgres
psql

Check the firewall in Azure console, probably you will have to enable port 5432.

More information about how to open ports on an Azure VM can be found here.