The Default PostgreSQL Port
Port 5432 is the port PostgreSQL listens on unless configured otherwise, and the port clients such as psql try first. A fresh installation from the distribution's packages listens on localhost only, so the port is not reachable from outside until you change that. For a list of other database and service ports, see common port numbers.
Check What Listens on 5432
sudo ss -ltnp 'sport = :5432'A default setup shows 127.0.0.1:5432 and often [::1]:5432, owned by a postgres process. Ask the server whether it accepts connections:
pg_isready -h 127.0.0.1 -p 5432Find the Configuration Files
The paths differ between distributions and versions, so ask PostgreSQL itself:
sudo -u postgres psql -c 'SHOW config_file;'
sudo -u postgres psql -c 'SHOW hba_file;'On Debian and Ubuntu they live under /etc/postgresql/, in a folder named after the version. On AlmaLinux and Rocky Linux they are in the data directory under /var/lib/pgsql/.
Change the Port
In postgresql.conf, set the port and restart:
port = 5433sudo systemctl restart postgresqlWith packages from the PostgreSQL project on AlmaLinux and Rocky Linux, the service name carries the version, for example postgresql-17. SELinux on those systems also has to allow the new port (semanage comes with the policycoreutils-python-utils package):
sudo semanage port -a -t postgresql_port_t -p tcp 5433On Debian and Ubuntu, a second cluster on the same server takes 5433 automatically. Clients name the port with -p:
psql -h 127.0.0.1 -p 5433 -U app_user -d appdbBackground on SELinux labels is in what is SELinux.
Open the Port Safely
Three changes work together, and all three are needed. First, in postgresql.conf, listen on the server's address as well:
listen_addresses = 'localhost,203.0.113.20'Second, in pg_, allow one database, one user, and one client address, with TLS and a password:
hostssl appdb app_user 203.0.113.10/32 scram-sha-256A hostssl line works only when ssl = on is set in postgresql.conf with a certificate; otherwise PostgreSQL ignores the line and logs a warning.
Third, open the port in the firewall for that client only. On Ubuntu and Debian:
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcpOn AlmaLinux and Rocky Linux:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10/32" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reloadRestart PostgreSQL after changing listen_; a reload is enough for pg_. The full procedure is in PostgreSQL remote access.
Or Use an SSH Tunnel
If only you or one application server needs access, leave PostgreSQL on localhost and open nothing. Forward a local port over SSH instead:
ssh -N -L 5433:127.0.0.1:5432 user@your_server_ip
psql -h 127.0.0.1 -p 5433 -U app_user -d appdbRun the first command on your computer and leave it open; the second, in another terminal, connects through the tunnel. Details are in SSH tunneling.
Never Open 5432 to the Whole Internet
- Automated scans: bots look for open database ports and try common user names and passwords around the clock.
- A broad pg_
hba.conf line: 0.0.0.0/0with a password is not a safe substitute for a firewall rule. - Docker:
-p 5432:5432publishes the port past ufw; bind it to 127.0.0.1 instead, as shown in Docker port mapping.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations