Skip to content
Log In Sign Up info@muvcloud.com

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_ip

While that session is open, a client on your computer connects to the tunnel:

psql -h 127.0.0.1 -p 5433 -U app_user app

The 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_hba.conf, nothing else.
  • TLS: require encryption with hostssl lines.
  • Never the whole internet: no 0.0.0.0/0 rule, and never the trust method.

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_hba.conf 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-256

This allows one role, into one database, from one address, over TLS, with a password. Check that the role's password was set while password_encryption was scram-sha-256, the default since PostgreSQL 14; SHOW password_encryption; tells you. If in doubt, set it again with \password app_user.

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_cert_file and ssl_key_file 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

ChangeNeeds
listen_addressesRestart
pg_hba.confReload
ssl and certificatesReload
sudo systemctl restart postgresql
sudo systemctl reload postgresql

A restart drops open connections, so schedule it. SELECT pg_reload_conf(); in psql also reloads. Before you reload, SELECT * FROM pg_hba_file_rules; 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 tcp

On 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 --reload

Test from the application server with psql "host=10.0.0.1 dbname=app user=app_user sslmode=require".

Troubleshooting

  • Connection refused: PostgreSQL does not listen on that address. Check sudo ss -tlnp | grep 5432 and 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.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

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.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations