Secure PostgreSQL: Bind 5432 to Localhost on Linux
Stop exposing PostgreSQL to the internet. Bind port 5432 to localhost, lock pg_hba.conf with scram-sha-256, verify with ss, and know when remote Fixwebnode support in Australia is the safer path.
This guide is for Australian small-business owners, site operators, and sysadmins who need PostgreSQL listening only on localhost—not open to automated bots on the public internet. By the end you will set listen_addresses = 'localhost', restrict pg_hba.conf with scram-sha-256, restart the service cleanly, and confirm with ss -tlnp | grep 5432 that the database binds strictly to 127.0.0.1:5432.
Leaving listen_addresses = '*' in postgresql.conf is one of the most common misconfigurations on Linux VPS and cloud hosts. Firewalls help, but hardened PostgreSQL configuration should always be your first defence. If you want hands-on remote help rather than DIY, Fixwebnode Linux server support works directly with Australian servers over secure remote sessions—not a freelance marketplace.
Why binding PostgreSQL to localhost matters
Port 5432 is scanned constantly. When PostgreSQL accepts connections on every interface, weak passwords, default roles, or outdated minor versions become an open door. Binding to localhost forces app-to-database traffic through the same host (or a controlled tunnel/SSH path), shrinking the attack surface before ufw or cloud security groups even enter the picture.
On Ubuntu and Debian systems common in Australia, the main files are usually under /etc/postgresql/<version>/main/. Always back up configs before editing, reload or restart deliberately, and verify the listen socket—not just the service status.
How do I stop PostgreSQL exposing port 5432 on a Linux server in Australia?
Set listen_addresses = 'localhost' (or 127.0.0.1) in postgresql.conf, restrict client rules in pg_hba.conf to trusted hosts with scram-sha-256, restart PostgreSQL, then confirm with ss -tlnp | grep 5432 that only 127.0.0.1:5432 (and optionally ::1) is listening. If the app runs on another host, keep a narrow host rule for that private IP only—never 0.0.0.0/0.
| Symptom | Quick fix | When to call Fixwebnode |
|---|---|---|
ss shows 0.0.0.0:5432 or *:5432 | Set listen_addresses = 'localhost' and restart | Config path unclear, multiple clusters, or production lock risk |
| App cannot connect after lockdown | Align pg_hba.conf + app host/socket settings | Remote app server, SSL, or replication involved |
| Auth failures / method rejected | Use scram-sha-256 and reset role passwords | Legacy clients, mixed auth, or unknown password stores |
Common issues when securing PostgreSQL on Linux
These problems show up repeatedly on Australian VPS, dedicated, and cloud Linux hosts. Each has a different root cause.
1. Port 5432 still listens on all interfaces
Symptoms: ss -tlnp | grep 5432 shows 0.0.0.0:5432 or *:5432. External scanners can reach the port even if you “thought” the firewall was enough.
2. Application breaks after binding to localhost
Symptoms: Web app or API returns database connection refused / timeout. The app was connecting to a public or private NIC address that PostgreSQL no longer accepts.
3. pg_hba.conf still allows overly broad trust or md5 from anywhere
Symptoms: Logs show connections from unexpected IPs, or auth still uses weak md5/trust lines with 0.0.0.0/0 or ::/0.
4. Restart succeeds but the wrong cluster or config file was edited
Symptoms: You changed a file under /etc/postgresql/, service restarts cleanly, yet listen address never changes—common with multiple major versions installed side by side.
Issue 1 — Bind listen_addresses to localhost (primary lockdown)
This is the core fix: stop PostgreSQL advertising 5432 on every interface.
Step 1 — Find the active config and version
sudo -u postgres psql -c "SHOW config_file;"
sudo -u postgres psql -c "SHOW listen_addresses;"
psql --version
ls /etc/postgresql/Note the path returned by SHOW config_file. Edit that file, not a stale copy from another major version.
Step 2 — Back up and set listen_addresses
sudo cp -a /etc/postgresql/$(sudo -u postgres psql -tA -c "SHOW server_version_num;" | cut -c1-2)/main/postgresql.conf /root/postgresql.conf.bak.$(date +%F) 2>/dev/null || true
# Prefer the exact path from SHOW config_file:
sudo cp -a "$(sudo -u postgres psql -tA -c 'SHOW config_file')" "$(sudo -u postgres psql -tA -c 'SHOW config_file').bak.$(date +%F)"
sudo sed -n 's/^#\?listen_addresses.*/listen_addresses = '\''localhost'\''/p' "$(sudo -u postgres psql -tA -c 'SHOW config_file')"
sudo nano "$(sudo -u postgres psql -tA -c 'SHOW config_file')"In the editor, ensure a single active line:
listen_addresses = 'localhost'Use '127.0.0.1' if you want IPv4-only. Avoid '*' and bare empty values on internet-facing hosts.
Step 3 — Restart PostgreSQL
sudo systemctl restart postgresql
# or, for a specific cluster (example 16 main):
sudo systemctl restart postgresql@16-main
sudo systemctl status postgresql --no-pagerStep 4 — Verify the socket
ss -tlnp | grep 5432
sudo -u postgres psql -c "SHOW listen_addresses;"You want 127.0.0.1:5432 (and possibly [::1]:5432), not 0.0.0.0:5432. If the wrong address remains, you edited the wrong cluster file—repeat Step 1.
When to call Fixwebnode: multiple clusters, custom data directories, or a live storefront where a bad restart means downtime. Remote specialists can identify the active unit and apply the change with a rollback plan.
Issue 2 — Restore app connectivity after localhost binding
Binding to localhost is correct for same-host apps (PHP-FPM, Node, Docker on host network with local socket/TCP). Remote app tiers need a deliberate path—not opening * again.
Step 1 — Confirm how the app connects
grep -R --line-number -E 'DATABASE_URL|DB_HOST|5432' /var/www /etc/nginx /etc/*/env 2>/dev/null | head -n 50If DB_HOST is a public IP or cloud private IP of the DB server, change it to 127.0.0.1 or localhost when the app runs on the same machine. Prefer Unix socket where supported.
Step 2 — Same-host TCP test
psql -h 127.0.0.1 -p 5432 -U your_app_user -d your_db -c 'SELECT 1;'
psql -h localhost -p 5432 -U your_app_user -d your_db -c 'SELECT 1;'Step 3 — If the app is on another server
Do not set listen_addresses = '*' casually. Prefer SSH tunnel, WireGuard/private VPC, or bind PostgreSQL to a private interface only, then allow that single app IP in pg_hba.conf. Example private bind (only if required):
listen_addresses = 'localhost,10.0.0.5'Replace 10.0.0.5 with your real private NIC address. Keep public interfaces out.
Step 4 — Application service reload
sudo systemctl reload php8.3-fpm 2>/dev/null || sudo systemctl reload php-fpm 2>/dev/null
sudo systemctl reload nginx 2>/dev/null || sudo systemctl reload apache2 2>/dev/null
# containerised apps: recreate with DB_HOST=127.0.0.1 or host.docker.internal as appropriateWhen to call Fixwebnode: split app/DB tiers, Docker/Kubernetes networking, or connection pools that still point at the old public endpoint.
Issue 3 — Harden pg_hba.conf with scram-sha-256
Listen address controls where the port opens; pg_hba.conf controls who may authenticate and how.
Step 1 — Locate and back up HBA
sudo -u postgres psql -c "SHOW hba_file;"
sudo cp -a "$(sudo -u postgres psql -tA -c 'SHOW hba_file')" "$(sudo -u postgres psql -tA -c 'SHOW hba_file').bak.$(date +%F)"Step 2 — Replace broad rules
Edit the HBA file and remove or comment lines that look like:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
host all all 0.0.0.0/0 trustFor a same-host app, prefer local socket + tight host rules:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
# Optional: single private app server only
# host your_db your_app_user 10.0.1.20/32 scram-sha-256Step 3 — Ensure password encryption defaults to SCRAM
sudo -u postgres psql -c "SHOW password_encryption;"
# In postgresql.conf if needed:
# password_encryption = scram-sha-256Step 4 — Reset app role password so SCRAM secrets are stored
sudo -u postgres psql -c "ALTER USER your_app_user WITH PASSWORD 'replace_with_long_random_secret';"Update the application secret store to match. Old md5 verifiers will not satisfy scram-sha-256 HBA lines.
Step 5 — Reload HBA without full outage when possible
sudo systemctl reload postgresql
# or
sudo -u postgres psql -c "SELECT pg_reload_conf();"
sudo tail -n 50 /var/log/postgresql/postgresql-*-main.logTest auth from localhost. Reject unexpected remote clients.
When to call Fixwebnode: legacy BI tools that only speak md5, replication slots, or unclear which roles production still uses.
Issue 4 — Wrong cluster or unit still serving the open port
Step 1 — List clusters and sockets
pg_lsclusters
ss -tlnp | grep 5432
systemctl list-units 'postgresql*' --allStep 2 — Map PID to config
sudo ss -tlnp | grep 5432
# note postgres PID, then:
ps aux | grep -E 'postgres|postmaster' | grep -v grep
sudo ls -l /proc/<PID>/cwd 2>/dev/null
sudo tr '\0' '\n' < /proc/<PID>/cmdlineStep 3 — Fix the active cluster only
sudo nano /etc/postgresql/<version>/main/postgresql.conf
sudo systemctl restart postgresql@<version>-main
pg_lsclusters
ss -tlnp | grep 5432Disable unused clusters if they were installed by accident and must not listen.
When to call Fixwebnode: leftover packages from major-version upgrades, Patroni/HA wrappers, or non-standard data directories.
Optional host firewall reinforcement (after config hardening)
Configuration first; firewall second. On Ubuntu:
sudo ufw status verbose
sudo ufw deny 5432/tcp
sudo ufw reload
sudo ss -tlnp | grep 5432
# External check from another host should fail; local 127.0.0.1 should still workCloud security groups should also drop inbound 5432 from the public internet. Do not rely on firewall alone while listen_addresses = '*' remains.
Quick Linux cheat sheet (copy sequence)
# 1) Inspect
sudo -u postgres psql -c "SHOW config_file;"
sudo -u postgres psql -c "SHOW listen_addresses;"
ss -tlnp | grep 5432
# 2) Lock listen address
sudo sed -i "s/^#\?listen_addresses\s*=.*/listen_addresses = 'localhost'/" "$(sudo -u postgres psql -tA -c 'SHOW config_file')"
# 3) Harden HBA (edit carefully — example local-only SCRAM)
# host all all 127.0.0.1/32 scram-sha-256
# host all all ::1/128 scram-sha-256
# 4) Restart & verify
sudo systemctl restart postgresql
ss -tlnp | grep 5432
sudo -u postgres psql -c "SHOW listen_addresses;"When DIY is enough vs when to book Fixwebnode
DIY is enough when you have SSH access, a single PostgreSQL cluster, the app on the same host, a recent backup, and a maintenance window. Follow the numbered steps, verify with ss, and keep the config backups you created.
Book Fixwebnode when production checkout depends on the database, you see multiple clusters, replication or connection poolers (PgBouncer) sit in front, the app tier is remote, or a prior change already caused partial outage. Fixwebnode is a direct specialist provider offering remote Linux server support for Australian businesses—including Adelaide and wider service coverage listed on the service areas page. You work with the specialist team straight through—not bidding boards or marketplace middle layers.
Talk to Fixwebnode about securing your PostgreSQL host
If port 5432 is still visible on a public interface, treat it as urgent hardening—not a cosmetic tweak. A short remote session can lock listen_addresses, tighten pg_hba.conf to scram-sha-256, restart safely, and prove with ss -tlnp | grep 5432 that only 127.0.0.1:5432 remains open.
Start a conversation or book remote help via Fixwebnode Ubuntu Linux server support in Australia. Bring SSH access details you are comfortable sharing over your preferred secure channel, note your PostgreSQL major version, and mention whether the app shares the DB host. We will keep the scope on this lockdown so your database stops answering the open internet.