How to publish and connect to Postgres without exposing it
Published on 2026-09-12 by frfrand
The four ways to access your database, ranked from worst to best, and why opening port 5432 to the Internet is almost always the wrong answer.

Short answer: Do not open port 5432 to the Internet. To connect yourself, use an SSH tunnel; for an application on another server to reach the database, use a permanent tunnel or a private network; and if there is no other choice but to expose it, limit by IP and require TLS. Then check four things: where it listens (listen_addresses), who gets in (pg_hba.conf with scram-sha-256), which user the app connects with, and whether your backups restore.
The time comes when you need to connect a database client to your Postgres in production, or when an application on another server needs to query it. There are four ways, and the difference between them is enormous.
Option 1: opening the port to the Internet
It is the fastest and almost always the worst.
You modify the configuration so it listens on all interfaces, open port 5432 in the firewall, and that's it.
What happens next: within minutes your server appears on automatic scanner lists. From there on, the only thing protecting your data is the database password and that no Postgres version has an unpatched vulnerability. Forever.
There are databases exposed on the Internet with real customer data. Practically none of them were opened on purpose: they were left that way after a test.
Option 2: opening the port but limiting by origin
Better. If only the office connects, you allow only that address in the firewall.
The problem is practical: home addresses change, people work from other places, and in the end someone expands the rule "for a while". If you choose this path, make sure the connection also goes encrypted with TLS: the password and data should not cross the Internet in clear text.
Option 3: the SSH tunnel
This is the one that solves the "I need to connect" case cleanly:
ssh -L 5432:localhost:5432 prod
This makes port 5432 on the server appear as local on your machine. Your client connects to localhost, traffic travels encrypted inside SSH, and the database has no port open to the world.
Access is tied to your SSH key, which is much stronger than a database password. And if the connection drops when changing networks, autossh reconnects it automatically.
Option 4: private network between servers
For the "my application is on another server" case, the tunnel also applies, but permanent: a tunnel between the two VPS, initiated from the application side.
From the app's point of view, the database is on localhost. From the Internet, the database port does not exist: all the data server accepts is SSH with an authorized key, through which the tunnel travels. And if the tunnel drops, whoever initiated it brings it back up.
It's what ArduMaker sets up when you connect two VPS with a tunnel: the application VPS (the initiator) runs autossh with a local forwarding to the other one (the receiver), which only accepts a dedicated tunnel user, without terminal and only for forwarding. The database is not published on the Internet.
The four things worth checking
Where it listens. The listen_addresses parameter. If it says localhost, it only accepts connections from the same machine. That should be the value unless you have a concrete reason.
Who can connect. The pg_hba.conf file defines, line by line, which user can enter which database from which origin and with which method. Two warnings: the method must be scram-sha-256, not md5 (old) nor trust (which means "without password"), and rules are evaluated in order, so the first match wins.
With which user your application connects. Not with the superuser. Create a user with permissions only on what it needs. The day a SQL injection appears, the difference between reading a table and deleting the database is exactly that detail.
If your backups restore. Having backups is not enough: you must have tested at least one full restore. The first time something is always missing.


Pre-opening checklist for PostgreSQL
On an ArduMaker VPS you can run PostgreSQL for your own application, but it should not rely on an open port to the world. Check: the address the engine listens on, pg_hba.conf rules, authorized users, firewall, encryption in transit when applicable, and a restorable backup.
For remote administration, an SSH tunnel can expose localhost:5432 only on your computer, provided you have the appropriate SSH permissions. On ArduMaker, SSH access to a VPS is opened for a limited time and closes automatically when it expires, unless you choose to leave it unlimited. If the API runs on the same VPS, it can normally connect via a local socket or an internal interface without exposing the engine.
If you install PostgreSQL as an ArduMaker app, several of these boxes come checked: the database is accessible only from within the VPS, with its own user without superuser privileges and a hostssl … scram-sha-256 line in pg_hba.conf. Opening it to the Internet is optional and is done via IP allowlist, with TLS (it is option 2, applied in the host firewall). The details are in the PostgreSQL documentation and in the SSH tunnels documentation.
Frequently asked questions
Is it dangerous to open port 5432? Yes, almost always. Automatic scanners find it in minutes and from that moment on the only defense is the password and having the engine patched. If necessary, limit by IP and require TLS.
How do I connect to Postgres in production from my computer? With an SSH tunnel (ssh -L 5432:localhost:5432 server) and your client pointing to localhost. If you already have a local Postgres on port 5432, use another local port, for example -L 15432:localhost:5432.
md5 or scram-sha-256 in pg_hba.conf? scram-sha-256. md5 is the old and weaker method, and trust means entering without a password.
What user should my application use? Its own user, owner only of what it needs, never the superuser postgres.
Related: Linux Firewall · PostgreSQL explained.