First Choice: an SSH Tunnel
PostgreSQL listens on 127.0.0.1 by default, and that should stay so. Applications on the same server connect locally; see what is localhost. To connect from your own computer, forward a local port over SSH:
ssh -L 5433:127.0.0.1:5432 user@your_server_ipWhile that session is open, a client on your computer connects to the tunnel:
psql -h 127.0.0.1 -p 5433 -U app_user appThe traffic is encrypted by SSH, the login is checked by SSH first, and no database port is open to anyone. Graphical database tools offer the same tunnel as an option in their connection settings. The SSH port forwarding guide explains the syntax.
When Another Server Must Connect
An application server that runs elsewhere needs a real network path. Keep it as narrow as possible:
- Private address: if both servers share a private network, listen on and connect through that address only.
- One source: allow the application server's address in the firewall and in
pg_, nothing else.hba.conf - TLS: require encryption with
hostssllines. - Never the whole internet: no
0.0.0.0/0rule, and never thetrustmethod.
The examples use 10.0.0.1 for the database server and 10.0.0.2 for the application server. Take a snapshot or a dump before you edit configuration files.
Step 1: Set listen_addresses
Open postgresql.conf: under /etc/postgresql/VERSION/main/ on Ubuntu and Debian, under /var/lib/pgsql/data/ on AlmaLinux and Rocky Linux. Set:
listen_addresses = 'localhost,10.0.0.1'Local applications keep using localhost, and the private address is added. Avoid '*', which listens on every interface, the public one included.
Step 2: Add a Line to pg_hba.conf
In the same folder, pg_ decides who may connect, from where, and how. PostgreSQL uses the first line that matches, so add yours above any broader host lines:
# TYPE DATABASE USER ADDRESS METHOD
hostssl app app_user 10.0.0.2/32 scram-sha-256This allows one role, into one database, from one address, over TLS, with a password. Check that the role's password was set while password_ was scram-sha-256, the default since PostgreSQL 14; SHOW password_ tells you. If in doubt, set it again with \password app_.
Step 3: Turn On TLS
On Ubuntu and Debian, a new cluster usually starts with ssl = on and the system's self-signed certificate; SHOW ssl; in psql confirms it. On AlmaLinux and Rocky Linux, ssl is off by default: set ssl = on and point ssl_ and ssl_ at a certificate and a key owned by the postgres user, the key with mode 600. A self-signed certificate encrypts the connection; to verify the server as well, use a certificate the client trusts and connect with sslmode=verify-full.
Step 4: Restart or Reload
| Change | Needs |
|---|---|
listen_ | Restart |
pg_ | Reload |
ssl and certificates | Reload |
sudo systemctl restart postgresql
sudo systemctl reload postgresqlA restart drops open connections, so schedule it. SELECT pg_ in psql also reloads. Before you reload, SELECT * FROM pg_ shows how PostgreSQL reads the current file, and a filled error column marks a line it cannot use.
Step 5: Allow One Address in the Firewall
On Ubuntu and Debian with ufw:
sudo ufw allow from 10.0.0.2 to any port 5432 proto tcpOn AlmaLinux and Rocky Linux with firewalld:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.2/32" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reloadTest from the application server with psql "host=10.0.0.1 dbname=app user=app_.
Troubleshooting
- Connection refused: PostgreSQL does not listen on that address. Check
sudo ss -tlnp | grep 5432and whether you restarted. - Timed out: a firewall drops the packets, on the server or in your provider's control panel.
- No pg_
hba.conf entry: the address, database, role, or TLS setting does not match any line. The server log names the rejected address. - Password authentication failed: wrong password, or a password stored in an older format.
Creating the role itself is covered in how to create a user in PostgreSQL.
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