Open remote connections for PostgreSQL on Linux - Davz33/tutorials GitHub Wiki

Open the desired port (default for postgres is TCP 5432)

  • for firewall-d based systems via:
    firewall-cmd --zone=public --add-port=5432/tcp --permanent
    firewall-cmd --reload

  • for ufw based systems via:
    ufw allow 5432

  • for all other cases:
    iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

Apply the changes via firewall-cmd --reload or ufw reload.

Check that postgres is listening and which port is it listening on (default is TCP 5432)

netstat -tulpn | grep LISTEN

A postgres record showing 127.0.0.1 means only localhost connections are allowed .

Look for the location of your postgres config file named postgres.conf:
sudo -u postgres psql -c 'SHOW config_file';
(without postgresql client installed, the command above will fail: normally, you'll find your .conf under /etc/postgresql/<vers>/main/

Edit postgres.conf:

listen_addresses = '*'
port = 5432

Edit pg_hba.conf

It is located in the same directory as postgres.conf .
Add host dbname username 0.0.0.0/0 scram-sha-256 and host dbname username ::/0 scram-sha-256 to the end of the file .
You can replace dbname and username with all to allow any user access to all remote DBs. For a newly created user to operate on a certain DB, you still need to grant writing / reading permissions manually via ALTER ROLE statements .

If you're going to connect to PostgreSQL from a static IP, you can put that in place of 0.0.0.0 and remove the line with ::/0 .

Restart postgres

systemctl restart postgresql.service

⚠️ **GitHub.com Fallback** ⚠️