Postgres remote connection fails, 'psql: error: connection to server at {server}, port 5432 failed: Connection timed out'

Viewed 50

I have a postgres database currently working on my PC. I am deploying a flask app which uses said database onto a linux server, and need to remotely connect to my database from the linux machine. The command I am using on the linux machine to do this is

psql -h 12.345.678.901 -p 5432 -U postgres

where 12.345.678.901 is my local PC ip address. When I do this, I get the error

psql: error: connection to server at "12.345.678.901", port 5432 failed: Connection timed out
    Is the server running on that host and accepting TCP/IP connections?

I would like to emphasize that the connection is not being 'refused', it is just timing out (unlike many of the questions related to this topic). I'm not sure if this helps identify the underlying issue or not. I understand that this is an extremely common issue, but no solutions have worked for me. Among these solutions are updating the pg_hba.conf, postgresql.conf, firewall configuration, and many others. I have done this. My pg_hba.conf file looks like this

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
host    all             all             ::0/0                   md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

host all all 0.0.0.0/0 md5

and my postgresql.conf looks like this

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                # comma-separated list of addresses;
                                # defaults to 'localhost'; use '*' for all
                                # (change requires restart)
port = 5432                     # (change requires restart)
max_connections = 100           # (change requires restart)

These files are located in C:\Program Files\PostgreSQL\14\data. I have manually checked that these changes are saved and implemented with the psql shell. I also restarted postgres after all changes to these files.

Other fixes I have implemented:

  • Set firewall rules on local PC to open port 5432 to inbound and outbound TCP/IP connections with Windows Defender Firewall

  • Set remote linux PC firewall to allow connections through port 5432 with the lines

    'sudo ufw allow 5432/tcp' & 'sudo ufw allow postgres/tcp'

  • Tried both local PC IPv4 address and default gateway address (I am not sure which one to use to be honest)

  • Set a rule for my physical router to allow connections to port 5432

I cannot figure this out to save my life. Any help would be greatly so utterly appreciated.

0 Answers
Related