Tutorial
Connect to PostgreSQL through an SSH tunnel
Run one ssh command to open a port on your computer that leads to the database, then point psql or any client at 127.0.0.1 and that port. Server-side details (sslmode, pg_hba.conf) are covered further down for when something is refused.
- Level
- Beginner
- Reading time
- 10 min
- Applies to
- Windows, macOS, Linux, OpenSSH command line
- Transputty steps checked against
- Transputty 0.1 (development build, not released)
Before you start
- An SSH login on a host that can reach the database, for example deploy@bastion.example.com
- The database address as that host sees it, for example db.internal:5432 (or 127.0.0.1:5432 if PostgreSQL runs on the SSH host)
- A PostgreSQL user name and database, for example app_ro and app
- psql or another PostgreSQL client on your computer
Short answer
To connect to PostgreSQL through an SSH tunnel, run ssh -N -L 5433:db.internal:5432 deploy@bastion.example.com and leave it running. Then connect your client to host 127.0.0.1, port 5433, with the usual database, user and password: psql "host=127.0.0.1 port=5433 dbname=app user=app_ro". The SSH host must be able to reach db.internal:5432.
The last section shows the same tunnel in Transputty, an SSH tunnel manager for Windows 11 that is in development. There is no public build yet; see the release status page. Everything before that section works today with plain ssh.
Never made a tunnel before? Do your first SSH tunnel on Windows first; it takes about 7 minutes and explains what the command does.
If someone told you to "use a tunnel", ask them for four things: the SSH host name, your SSH user name (with its password or key), the database's address and port as seen from that SSH host, and your database user name and database name. The SSH login and the database login are separate.
What does the path look like?
your computer bastion.example.com db.internal
psql -> 127.0.0.1:5433 ==== SSH (encrypted, port 22) ====> sshd ---> 5432 PostgreSQL
(ssh listens here) (plain TCP on the private network)
bastion.example.com stands for the SSH host. A bastion (or jump host) is the one server you are allowed to SSH into, which can in turn reach the private machines. Two things follow from the picture. The destination db.internal:5432 is resolved and dialled by the SSH host, not by your computer. And PostgreSQL sees a connection from the SSH host.
The command
When PostgreSQL is on a different machine from the SSH host:
ssh -N -L 5433:db.internal:5432 deploy@bastion.example.com
When PostgreSQL runs on the SSH host itself:
ssh -N -L 5433:127.0.0.1:5432 deploy@db.example.com
In that second form 127.0.0.1 is the SSH host's own loopback address, the one a computer uses to reach itself. It is safer than writing localhost, which the server may resolve to the IPv6 address ::1 while PostgreSQL listens on IPv4 only.
-L 5433:db.internal:5432 means "listen on local port 5433 and forward each connection to db.internal port 5432 as seen from the server". -N means no remote shell, so after you log in the command prints nothing and waits. That silence is the tunnel running. Press Ctrl+C to stop it. The local port forwarding tutorial explains each part of -L in more depth.
Why 5433 and not 5432?
The local port is your choice. If PostgreSQL is also installed on your computer, it already holds 5432. Depending on your system, ssh then either fails to open the port or, on Windows, can end up listening next to the local server so that it is unclear which one your client reaches. Using 5433 removes the ambiguity. If 5433 is taken as well, pick any free port; Local port already in use shows how to find out what holds one.
Connect with psql
In a second terminal:
psql "host=127.0.0.1 port=5433 dbname=app user=app_ro"
The same thing as a libpq URI, which is the form most drivers and ORMs accept:
postgresql://app_ro@127.0.0.1:5433/app
Database, user and password are the real server's. Only host and port change. Prefer 127.0.0.1 over localhost: with -L 5433:… ssh listens on both 127.0.0.1 and ::1, but tools that listen on IPv4 only (Transputty among them) leave ::1 closed, and a client that tries only the IPv6 address will be refused.
How do I know it worked?
psql asks for the database password (not the SSH one) and then shows its prompt:
app=>
Ask the server where it thinks you are connecting from:
SELECT inet_client_addr(), current_database();
The first column is the SSH host's address on the private network, or 127.0.0.1 when PostgreSQL runs on the SSH host. It is never your own PC's network address, because the SSH host made the connection. To rule out a PostgreSQL installed on your own PC answering instead, press Ctrl+C in the ssh window and run psql again: it must now fail with Connection refused.
Without psql, Test-NetConnection 127.0.0.1 -Port 5433 in PowerShell reporting TcpTestSucceeded : True shows that the tunnel is listening. It does not show that the database answered.
GUI clients
Any graphical client takes the same four values: Host 127.0.0.1, Port 5433, Database app, User app_ro. Leave the client's own SSH or proxy options off, or you will tunnel twice.
Many clients, including SQLPro Studio, DBeaver, DataGrip and pgAdmin, have a built-in SSH tunnel option, and for one client and one database it is the simpler choice. An external tunnel is still preferable when several tools should share one connection (a client, a migration script and a BI tool on the same port), when the tunnel should outlive client restarts, or when the route needs jump hosts that the client's dialog cannot express.
Why does PostgreSQL need a tunnel?
A sensibly deployed PostgreSQL server is not reachable from the internet. Three settings usually stand in the way, and a tunnel works with all of them instead of weakening them:
listen_addressesdefaults tolocalhost, so a fresh install accepts TCP connections only from its own machine.pg_hba.conflists which client addresses may log in to which databases as which users. Production files rarely include office or home addresses.- Firewalls and security groups normally allow port 5432 only from application servers.
An SSH tunnel sidesteps the network part: your client connects to a port on your own computer, and the SSH host opens the real connection from inside the private network. This is SSH local port forwarding, and nothing on the database server has to change if the SSH host is already an allowed client.
If the server refuses: TLS and pg_hba.conf
The rest of this page is for when the tunnel is up and PostgreSQL still says no, or when you must verify the server's certificate.
TLS through the tunnel: sslmode and the certificate name
libpq's default sslmode=prefer encrypts when the server offers TLS and verifies nothing, so it works through a tunnel unchanged. Verification is where the tunnel shows.
With sslmode=verify-full, libpq compares the certificate with the host you asked for. Through a tunnel that host is 127.0.0.1, and the check fails:
psql: error: connection to server at "127.0.0.1", port 5433 failed: server certificate for "db.internal" does not match host name "127.0.0.1"
There are two correct ways to handle it. The first keeps full verification by giving both the real name and the tunnel address: host is used for the certificate check, hostaddr for the actual connection.
psql "host=db.internal hostaddr=127.0.0.1 port=5433 dbname=app user=app_ro sslmode=verify-full sslrootcert=ca.crt"
The second is sslmode=verify-ca, which checks that the certificate chains to your CA but ignores the name.
Do not drop to sslmode=disable out of frustration when the database is on a different machine from the SSH host. SSH protects only the first leg, and sslmode=prefer or require without verification does not stop someone on that private network from impersonating the database.
What pg_hba.conf sees
pg_hba.conf matches the address the connection arrives from, and that is the SSH host:
| Layout | Address PostgreSQL sees | Typical matching line |
|---|---|---|
Database on the SSH host, forward to 127.0.0.1:5432 |
127.0.0.1 |
host all all 127.0.0.1/32 scram-sha-256 |
Database on db.internal, SSH on a bastion |
The bastion's private address | host app app_ro 10.0.1.5/32 scram-sha-256 |
If no line matches, the server answers with FATAL: no pg_hba.conf entry for host "10.0.1.5", user "app_ro", database "app", no encryption (or SSL encryption). The address in that message tells you exactly which line is missing. A local line (Unix socket) never applies to a tunnel, because the SSH host connects over TCP.
The same tunnel in Transputty
Transputty keeps this forward as a saved connection and holds the local port open across reconnects. The steps below follow version 0.1, which is a development build.
- Press Ctrl+N or click + in the toolbar. The "New connection" dialog opens with the PostgreSQL preset already applied under Start from a preset: name
postgres-tunneland one "Local (-L)" rule from127.0.0.1:5432tolocalhost:5432. If port 5432 is busy on your PC, the rule gets the next free port instead and a note says so, for example "5432 is in use by postgres.exe (PID 4121), so Transputty picked 5433." - Under SSH server enter Host
bastion.example.com, Port22and Userdeploy. Choose a method under Authentication. - Under Forwarding rules set the Port next to Listen address to
5433, to match the rest of this tutorial. Set Destination host todb.internal(or127.0.0.1if PostgreSQL runs on the SSH server) and check the destination Port is5432. Leave Listen address as127.0.0.1. - Click Save & connect. On the first connection, compare the host key fingerprint and choose Trust and connect. If you chose Password, Transputty then asks for it ("Password for deploy@bastion.example.com").
- Point your client at
127.0.0.1and port5433. Ctrl+C on the row copies the endpoint.
The preset only fills in ports. Transputty does not speak the PostgreSQL protocol, so sslmode, users and pg_hba.conf behave exactly as described above. For background on what the app does when the link drops, see automatic reconnection.
Troubleshooting
| Message | Likely cause | Next step |
|---|---|---|
Connection refused and Is the server running on that host and accepting TCP/IP connections? |
Nothing is listening on the local port: the tunnel is not running, or the client uses a different port | SSH tunnel connection refused |
server closed the connection unexpectedly |
The tunnel accepted the connection but the SSH host could not open the destination (wrong name or port, PostgreSQL not listening on that interface, or forwarding disabled). Look at the ssh window for a channel N: open failed line |
SSH tunnel connection refused |
FATAL: password authentication failed for user "app_ro" |
The tunnel works and PostgreSQL answered. The user name, password or pg_hba.conf method is wrong |
Check the user on the server; this is not a tunnel problem |
FATAL: no pg_hba.conf entry for host … |
The SSH host's address is not allowed in pg_hba.conf |
Add a line for the address in the message |
| The client hangs, then times out | The SSH host cannot route to the database (firewall drop), or the SSH session has died silently | Test from the SSH host; see SSH tunnel keeps disconnecting |
bind [127.0.0.1]:5433: Address already in use in the ssh window (Windows' built-in ssh says Permission denied) |
Another program on your computer holds the local port | Local port already in use |
Common questions
What host and port do I give my PostgreSQL client when using an SSH tunnel?
Use host 127.0.0.1 and the local port you chose in the ssh -L command, for example 5433. The database name, user and password stay exactly what the real server expects. The tunnel only changes where the client connects, not who it logs in as.
Why use local port 5433 instead of 5432 for the tunnel?
If PostgreSQL is installed on your own computer it already listens on 5432, so the tunnel cannot bind that port, or worse, your client quietly talks to the local server. A different local port such as 5433 avoids both problems. The remote side of the forward still uses 5432.
Does sslmode=verify-full work through an SSH tunnel?
Not with host=127.0.0.1, because the server certificate names the real host and libpq compares it with the host you typed. Use host=db.internal together with hostaddr=127.0.0.1 so libpq connects to the tunnel but checks the certificate against db.internal, or use sslmode=verify-ca, which checks the certificate chain without the name.
Which address does pg_hba.conf see for a tunnelled connection?
PostgreSQL sees the connection coming from the SSH host, never from your computer. If the database runs on the SSH host and the forward targets localhost, the client address is 127.0.0.1 or ::1. If the database is on another machine, the client address is the bastion's address on that network.
Do I still need SSL to PostgreSQL if the connection goes through SSH?
SSH encrypts the path from your computer to the SSH host only. When PostgreSQL runs on a different machine from the SSH host, the last hop between them is an ordinary PostgreSQL connection, so keep sslmode=require or stricter. When the database is on the SSH host itself, that hop never leaves the machine.
How this tutorial was checked
2026-11-26: The ssh and psql steps were run on Windows 11 with the built-in client (OpenSSH_for_Windows 9.5p2) against a real OpenSSH server (OpenSSH for Windows 10.4p1) and a throwaway PostgreSQL 18.4 with TLS and scram-sha-256, all on one PC. Run through the tunnel: both connection-string forms, a login as app_ro, inet_client_addr(), a forward to localhost failing where 127.0.0.1 worked, sslmode=verify-full failing by host name, verify-ca and the host plus hostaddr form succeeding, a wrong password, a closed destination and AllowTcpForwarding no. The error messages quoted come from that run, with two exceptions: the test certificate carried two names, so libpq added "(and 1 other name)" to the verify-full message, and the pg_hba.conf message is PostgreSQL's standard wording and was not reproduced. Not tested: a Linux SSH server, a separate bastion or database machine, and the Test-NetConnection check on this page. The Transputty steps were written from the 0.1 source code and were not clicked through in the app.
Sources and further reading
- ssh(1) manual page, OpenBSD (the -L and -N options)
- PostgreSQL documentation: Secure TCP/IP Connections with SSH Tunnels
- PostgreSQL documentation: libpq connection strings (host, hostaddr, sslmode)
- PostgreSQL documentation: libpq SSL support and certificate verification
- PostgreSQL documentation: the pg_hba.conf file
- PostgreSQL documentation: connection settings (listen_addresses)
Transputty keeps SSH tunnels running on Windows
Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.