Skip to content
AdminKit

Is port 5432 (PostgreSQL) exposed to the internet, and is pg_hba.conf too loose?

Port 5432 is PostgreSQL's IANA-registered default port. PostgreSQL's defaults are actually safe — it listens on localhost only — but listen_addresses and pg_hba.conf get changed together often enough that weakly-authenticated, internet-reachable instances keep turning up. AdminKit's port-check tool automates nine exposure checks; 5432 isn't on that list, so here's how to check it yourself.

By AdminKit team, Netbase JSC · Updated

Key takeaways

  • PostgreSQL starts closed: listen_addresses defaults to localhost, so a fresh install takes no network connections from other machines.
  • Exposure needs two changes: listen_addresses widened to a public interface, and a pg_hba.conf rule that matches internet addresses. The second file decides how bad it is.
  • A trust rule for remote hosts means no password at all. A password rule means the postgres superuser can be brute-forced, and a superuser can run operating-system commands through COPY ... FROM PROGRAM.
  • Test from outside with psql, not only nc: the server’s reply tells you which pg_hba.conf rule your address hit.
  • Fix it in layers: bind to a private address, narrow pg_hba.conf to known hosts with scram-sha-256 over TLS, firewall 5432, and keep superuser logins off the network.

Why does PostgreSQL start out closed?

IANA registers port 5432 for the PostgreSQL wire protocol. PostgreSQL’s own connections and authentication documentation confirms that listen_addresses defaults to localhost — a fresh installation simply doesn’t accept network connections from anywhere else until an administrator deliberately widens it, typically to * or a specific public interface, to allow remote application servers to connect. That change alone doesn’t make a database insecure; PostgreSQL still requires a second file, pg_hba.conf, to authorize who can authenticate and how.

The two settings answer different questions, and both have to be wrong at the same time for a database to be open to the internet:

LayerSettingDefaultWhat it decides
Networklisten_addresses in postgresql.conflocalhostWhich interfaces the server accepts TCP connections on at all
Networkport5432The TCP port, shared by every address it listens on
Client authenticationpg_hba.conf recordsSet by the installer or imageWhich client address, database and user may connect, and with which method
TransportssloffWhether the server offers TLS to clients that ask for it

The last row matters more than it looks. With ssl off, every byte of the session, including query results, crosses the network in clear text, and the password method sends the password itself in clear text.

What does pg_hba.conf actually decide?

PostgreSQL’s own pg_hba.conf documentation describes it as a list of rules evaluated top to bottom, with the first matching rule deciding whether a connection is accepted and which authentication method it must use. There is no fall-through: if the first matching record leads to a failed login, PostgreSQL does not try the next one. That makes the order of lines a security setting in its own right.

The risk shows up when listen_addresses is widened to accept connections from anywhere, and a pg_hba.conf rule is also set broadly — for example allowing all hosts with the trust method. The trust authentication documentation is blunt about what that means: PostgreSQL assumes anyone who can connect is authorized as whatever database user name they specify, superuser names included. That combination, often introduced temporarily during development or initial setup and then never tightened before the server goes into production, is what turns a reachable port 5432 into an open door rather than just an open port.

The method column is where most of the difference between “reachable” and “compromised” lives:

MethodWhat the client must proveRisk if the rule matches internet addresses
trustNothingCritical: any user name, including postgres, with no password
passwordThe password, sent in clear textHigh: guessable, and readable on the wire without TLS
md5A hashed password exchangeHigh: guessable; PostgreSQL has deprecated MD5 passwords
scram-sha-256A challenge-response proof of the passwordStill open to password guessing from anywhere
certA client TLS certificateLow, if the CA and the certificate list are under your control
rejectNothing; the connection is refusedLow: the listener still answers, but no login is possible

The password authentication documentation states that support for MD5-encrypted passwords is deprecated and is due to be dropped in a later version, so a server that still uses md5 rules has a migration to plan regardless of exposure.

What can someone do with a reachable port 5432?

Even with a real password method configured, an internet-facing PostgreSQL port is routinely targeted for credential brute-forcing and password spraying against default accounts like postgres. The account name is predictable because installers and the official container image create it, so an attacker only has to guess the password.

This is documented, not hypothetical. In December 2020, Palo Alto Networks’ Unit 42 described PGMiner, a cryptomining botnet that picked public address ranges at random, connected to PostgreSQL as postgres, and brute-forced the password from a built-in list of common ones. Once in, it did not need a software bug. It used COPY ... FROM PROGRAM, a normal feature that runs a shell command on the database host and reads its output into a table.

PostgreSQL’s COPY documentation limits that feature, and its file-reading and file-writing variants, to superusers and to roles granted pg_read_server_files, pg_write_server_files or pg_execute_server_program. That is the real line of defence: a guessed password for an ordinary application role reads that role’s data, while a guessed superuser password hands over the operating-system account PostgreSQL runs as. Unit 42’s own advice was to remove pg_execute_server_program from untrusted users.

How does a PostgreSQL server end up reachable?

Most exposures trace back to one of a few routine setups rather than a deliberate decision:

  1. Tutorial settings copied into production. Many remote-access walkthroughs set listen_addresses = '*' and add a host all all 0.0.0.0/0 line to get an application working, then never return to narrow them.
  2. Container images with a published port. The official postgres image writes a pg_hba.conf line for all databases, all users and all addresses, using scram-sha-256 by default on PostgreSQL 14 and later. Setting POSTGRES_HOST_AUTH_METHOD=trust turns that line into password-free access, which the image’s own documentation recommends against. Publishing the container with -p 5432:5432 then exposes it on every host interface.
  3. A host firewall that never sees the traffic. Docker’s documentation on packet filtering and firewalls explains that traffic to published container ports is diverted before it reaches the chains ufw manages, so a ufw deny 5432 rule may not protect a containerised database.
  4. Cloud network rules opened for one client. A security group that allows 5432 from 0.0.0.0/0 “just for now” is a common way the network layer gets widened without anyone touching PostgreSQL itself.

How do you check your own exposure?

AdminKit’s open ports check checks a fixed nine-port list, and port 5432 isn’t on it — confirm your public IP with what is my IP, then test the port manually from a machine outside your network with nc -vz -w 3 203.0.113.5 5432. A connection that succeeds means listen_addresses is reachable from the internet; “connection refused” or a timeout means it’s scoped the way you expect, rather than relying on memory of a configuration change made months earlier.

AdminKit’s check scans only the public address of the browser that starts it, so it describes the network you are sitting on, not a cloud server elsewhere. If PostgreSQL runs on an office server behind your router, an open 80, 443 or 8080 in that result is a sign that port forwards exist and are worth reviewing; reading an open-ports result covers what each state means.

nc only proves that something accepted a TCP connection. The PostgreSQL client tells you which pg_hba.conf rule your address reached. Run it from outside, against your own address, with a user name that does not exist:

$ psql "host=203.0.113.5 port=5432 user=probe dbname=probe connect_timeout=5"

Example replies (illustrative, using documentation addresses; exact wording varies by PostgreSQL version):

ReplyWhat it means
Connection refused or a timeoutNothing reachable on 5432 from where you tested
FATAL: no pg_hba.conf entry for host "198.51.100.7", user "probe", ...The server listens publicly, but no rule matches your address. Close the port anyway; the listener is still exposed.
Password for user probe:A rule matches internet addresses with a password method. Anyone can try passwords.
A probe=> prompt, or an error that the role does not exist with no password askedA trust rule matched. Treat it as critical and close the port first.

How do you confirm the listener on your own host?

From the database server itself, three commands show what the running instance is really using, which can differ from what you remember putting in the files:

$ ss -ltnp | grep 5432
LISTEN 0  200  0.0.0.0:5432  0.0.0.0:*  users:(("postgres",pid=812,fd=6))

postgres=# SHOW listen_addresses;
postgres=# SELECT line_number, type, address, auth_method FROM pg_hba_file_rules;

The ss output above is an example. 0.0.0.0:5432 or [::]:5432 means every interface; 127.0.0.1:5432 or a private address means the network layer is already narrow. The pg_hba_file_rules view lists each non-comment line of pg_hba.conf with its parsed values, in order, which is the quickest way to spot a trust or 0.0.0.0/0 line near the top.

How do you fix an exposed PostgreSQL?

  • Set listen_addresses to a specific private network interface rather than a wildcard, so the database process itself never binds to a publicly routable address unless that’s genuinely required.
  • Replace any trust or unrestricted pg_hba.conf rule with scram-sha-256 authentication, scoped to the specific source IP ranges that need access — not 0.0.0.0/0.
  • Use hostssl records instead of host for remote clients, and turn ssl on, so passwords and query results don’t cross the network in clear text.
  • Add a firewall rule restricting port 5432 to the application servers and administrators that actually need it, as defense in depth beyond pg_hba.conf alone.
  • Keep superuser logins local, and grant pg_execute_server_program and the server-file roles only to roles that truly need them.
  • If remote administrative access is genuinely needed, prefer connecting over a VPN or SSH tunnel rather than exposing 5432 directly to the internet.

A narrow pg_hba.conf for one application subnet, with an explicit reject at the end, looks like this:

# TYPE    DATABASE  USER      ADDRESS        METHOD
local     all       postgres                 peer
hostssl   appdb     app_user  10.0.2.0/24    scram-sha-256
host      all       all       0.0.0.0/0      reject
host      all       all       ::/0           reject

Because the first match wins, the reject lines catch every address the earlier lines did not name, and the postgres superuser can log in only through the local socket.

Other database ports share the same shape of risk once their defaults get changed — see is port 27017 (MongoDB) exposed for the same pattern on the MongoDB wire protocol, and open Redis and other unauthenticated databases for the full outside-in walkthrough across data stores.

Which servers may you test with these checks?

Every check this page links to is for assets you own or are explicitly authorized to test. Never scan, probe, or attempt to connect to a network or device you don’t control — AdminKit’s active checks only ever run against the IP making the request or an asset you’ve verified ownership of. See our Disclaimer and Acceptable Use Policy for the full terms.

PostgreSQL’s wire protocol is one entry in AdminKit’s broader machine protocols cluster, which groups the network services and device-management protocols that most often turn up misconfigured and internet-reachable.

This guide is general information, not a security audit of your specific network — see our disclaimer for what AdminKit's guidance does and does not commit to.

Frequently asked questions

Does AdminKit's port-check tool scan port 5432?

No. The tool checks a fixed list of nine specific TCP ports (23, 554, 1883, 8883, 502, 7547, 80, 443, 8080) chosen for how often they turn up misconfigured on home and small-office devices — see the port-check tool page for the current list. 5432 isn't on that list, so confirm your own exposure with the manual check below.

Is PostgreSQL insecure by default, like some other databases?

No — PostgreSQL's own documentation confirms listen_addresses defaults to localhost, meaning a fresh install only accepts connections from the same machine until an administrator deliberately changes it. The exposure covered here comes from that default being changed, not from PostgreSQL shipping open.

What's the difference between listen_addresses and pg_hba.conf?

listen_addresses controls which network interfaces PostgreSQL's server process binds to and listens on at all; pg_hba.conf then controls who is allowed to authenticate once a connection reaches the server, using rules matched top to bottom with the first match deciding the outcome. Both have to be configured correctly — loosening one without the other either blocks legitimate access or leaves a wide-open door.

What does a dangerous pg_hba.conf rule look like?

A line such as allowing all hosts (0.0.0.0/0) to connect using the 'trust' authentication method, which accepts any connection with no password check at all — a setting sometimes used temporarily during development and then never removed before the server becomes reachable from outside.

What's the fastest fix if I find my own instance's port 5432 open to the internet?

Set listen_addresses to a specific private interface rather than a wildcard, replace any 'trust' or overly broad pg_hba.conf rule with scram-sha-256 authentication scoped to known source IPs, and add a firewall rule so port 5432 is reachable only from the application servers and admins that actually need it.

Want AdminKit to watch this for you?

Free checks tell you the state today. Protective DNS and monitoring tell you when it changes.

Huy Nguyen (David), Founder and CEO of Netbase JSC

Talk to an expert

Huy Nguyen (David), Founder & CEO, Netbase JSC

Ask whether this fix applies to your setup, or get a second pair of eyes on your DNS and mail records.