PortGate

Resources · Port reference · port 5432

Port 5432 (PostgreSQL): what it is and how to secure it

PostgreSQL on port 5432 should listen on localhost. A public Postgres with a weak password is a common way in for miners.

Check command

sudo ss -tlnp | grep 5432

Copied.

Port 5432 is PostgreSQL. Keep it on localhost or a private network, and require scram-sha-256 passwords. Bots guess Postgres passwords constantly, and a superuser login can run shell commands on the server.

What port 5432 is for

PostgreSQL accepts client connections on 5432 from your app, psql, pgAdmin and tools such as DBeaver. Two settings decide who can connect: listen_addresses in postgresql.conf (which addresses it listens on) and pg_hba.conf (who may log in, from where, and how). Distribution packages default to localhost.

Why a public 5432 is dangerous

  • Password guessing against postgres and other common users never stops.
  • A trust line in pg_hba.conf lets anyone in without a password.
  • A superuser can run COPY ... FROM PROGRAM, which executes a shell command as the postgres user. Cryptomining campaigns use this to install their miner.

Check yours

sudo ss -tlnp | grep 5432
sudo -u postgres psql -c "SHOW listen_addresses;"
sudo grep -vE "^\s*(#|$)" /etc/postgresql/*/main/pg_hba.conf

listen_addresses of localhost, and pg_hba.conf lines limited to 127.0.0.1/32, ::1/128 or private ranges with scram-sha-256, are what you want. 0.0.0.0/0, all with trust, or md5 from anywhere need fixing. From another machine:

nc -zv YOUR_SERVER_IP 5432

Fix it

In postgresql.conf (on Debian and Ubuntu, /etc/postgresql/<version>/main/postgresql.conf):

listen_addresses = 'localhost'
password_encryption = scram-sha-256

In pg_hba.conf, allow only local and private sources:

local   all   all                     peer
host    all   all   127.0.0.1/32      scram-sha-256
host    all   all   ::1/128           scram-sha-256
sudo systemctl restart postgresql

Users created with old md5 hashes need their password set again after switching to scram-sha-256. Give the app a user that owns only its own database, never a superuser.

For Docker, never set POSTGRES_HOST_AUTH_METHOD=trust on a server, and publish on loopback:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    ports:
      - "127.0.0.1:5432:5432"

If you need remote access

Use an SSH tunnel, ssh -L 5432:127.0.0.1:5432 you@YOUR_SERVER_IP, or pgAdmin's built-in SSH tunnel. For an app on another server, listen on the private address and add one pg_hba.conf line for that server's IP only.

Common mistakes

  • listen_addresses = '*' with host all all 0.0.0.0/0 md5. That is the whole internet with a password prompt.
  • Running the app as postgres. If the app is hacked, the attacker becomes a database superuser.
  • Docker publishing 5432. UFW does not filter Docker-published ports.

FAQ

I see unknown processes run by the postgres user. Treat it as a compromise. Close the port, rotate every password, and follow the cryptominer guide to find how it persists.

Is changing the port enough? No. Scanners find Postgres on any port.

How do I get warned? PortGate's Server Guard alerts when 5432 listens on a public address, including through Docker.