Skip to content
VDS · Databases

Remote database access: how not to expose it to the whole internet

Two ways to connect to a database on VDS from your work computer: SSH tunnel and an open port with restrictions. What to choose and how to check.

Updated August 23, 2026

A database listening on 0.0.0.0 with the password root survives only until the first scan — and the entire address range is scanned continuously. At the same time, connecting to the database from a laptop with a client is convenient and often necessary. There are two ways, and only one of them has no downsides.

The database stays bound to 127.0.0.1, and nothing but SSH is exposed to the outside. On your own computer:

ssh -L 5433:127.0.0.1:5432 root@YOUR_ADDRESS -N
ssh -L 3307:127.0.0.1:3306 root@YOUR_ADDRESS -N

Now connect to 127.0.0.1:5433 in DBeaver, TablePlus, or psql. From the database's point of view, this looks like a local connection.

Most clients can set up the tunnel themselves: the connection settings have an “SSH” tab. Specify the machine address from the “Overview” of the control panel /hosting/vds/m/<id> and your key.

The advantage of this method: you don't need to change anything in the database or firewall configuration.

Method 2: an open port with restrictions

This is a last resort — for when an external service with a known static address connects. Every step below is mandatory:

ufw allow from 203.0.113.10 to any port 5432 proto tcp

For PostgreSQL: listen_addresses = '*' and the line hostssl all all 203.0.113.10/32 scram-sha-256 in pg_hba.conf. For MySQL: bind-address = 0.0.0.0 and a user bound to a specific address:

CREATE USER 'app'@'203.0.113.10' IDENTIFIED BY 'long-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'203.0.113.10';

Without TLS, such a connection sends data in plain text. ufw allow 5432 without specifying a source is exactly what “opening it to the whole internet” means.

Verification

From another machine:

nmap -Pn -p 3306,5432,6379,27017 YOUR_ADDRESS

The expected response is filtered or closed on all ports. Locally, check what is listening and where:

ss -tulpn | grep -E '3306|5432|6379'

Common mistakes

SymptomCause
The client won't connect through the tunnelThe local port is taken — pick another one
Host ... is not allowed to connectThe MySQL user is bound to localhost
no pg_hba.conf entry for hostThere is no rule for your address
The port is open but nothing respondsThe database only listens on 127.0.0.1
It worked, then stoppedYour provider gives you a dynamic address — one more argument for the tunnel

The machines have no IPv6, and no second address either. Write all rules for the single public IPv4 address from the “Overview” — there will be no confusion with two stacks.