A PostgreSQL listener on every network interface can turn a database meant for application hosts into a port that scanners, unmanaged clients, or mistaken firewall rules can reach. Restricting remote access starts at the listener layer, then continues in host-based authentication when a non-loopback interface still needs to serve trusted clients.
PostgreSQL uses listen_addresses in postgresql.conf to decide which TCP/IP addresses the server binds. The default value localhost accepts only loopback TCP connections, while * binds all available IPv4 and IPv6 interfaces. pg_hba.conf is the next gate after a connection reaches the server; it matches connection type, database, user, client address, and authentication method in order.
Changing listen_addresses requires a restart because it applies only when the server starts. Plan a restart window, keep a rollback path for the previous configuration, and query the running server for its config paths instead of assuming a distro layout. Remote clients such as application servers, replicas, backups, or monitoring agents will lose connectivity unless the new bind address and authentication rules still include them.
Related: How to configure pg_hba.conf in PostgreSQL \\
Related: How to enable SSL for PostgreSQL connections \\
Tool: Port Exposure Summary Checker
$ sudo -u postgres psql -Atc "SHOW config_file;" /etc/postgresql/18/main/postgresql.conf
$ sudo -u postgres psql -Atc "SHOW listen_addresses;" *
localhost means loopback-only TCP listening. * means every available IPv4 and IPv6 interface can accept connection attempts before pg_hba.conf authentication is checked.
$ sudo ss -lnt 'sport = :5432' State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* LISTEN 0 200 [::]:5432 [::]:*
0.0.0.0:5432 and [::]:5432 show listeners on all IPv4 and IPv6 interfaces.
$ sudo cp /etc/postgresql/18/main/postgresql.conf /etc/postgresql/18/main/postgresql.conf.before-access-change
Replace the path with the value returned by SHOW config_file.
$ sudoedit /etc/postgresql/18/main/postgresql.conf
listen_addresses = 'localhost'
Use a specific private IP address or a comma-separated list when trusted remote clients still need direct access, and avoid * unless every interface must accept connection attempts.
A wrong bind address can leave PostgreSQL unreachable for clients that depend on TCP connections.
$ sudo systemctl restart postgresql
A restart disconnects active sessions and aborts running transactions.
$ sudo -u postgres psql -Atc "SHOW listen_addresses;" localhost
$ sudo ss -lnt 'sport = :5432' State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* LISTEN 0 200 [::1]:5432 [::]:*
Loopback-only listeners appear as 127.0.0.1:5432 and, when IPv6 loopback is enabled, [::1]:5432.
$ sudo -u postgres psql -Atc "SHOW hba_file;" /etc/postgresql/18/main/pg_hba.conf
Loopback-only listen_addresses blocks remote TCP connections, but permissive host entries matter again when binding to a non-loopback interface.