Connect to Postgres Through an SSH Tunnel
A production Postgres server is usually firewalled to a private network. The exact ssh command, the ~/.ssh/config block, and what to point your client at.
A production Postgres instance is rarely reachable directly from a laptop, and that’s usually by design: the database sits in a private subnet, and only a bastion (jump) host has a public address. To run a query against it, you forward a local port through that bastion to the database’s real host and port, then point your Postgres client at your own machine instead of the database.
The command
ssh -N -L 15432:db-internal-host:5432 ec2-user@bastion.example.com -i ~/.ssh/id_ed25519
-N— don’t open a remote shell; the connection exists only to carry the forwarded port.-L <local-port>:<db-host>:<db-port>— forwardlocalhost:<local-port>on your machine to<db-host>:<db-port>as resolved from the bastion, not from your machine.<db-host>is usually a private hostname or IP your laptop can’t reach on its own.ec2-user@bastion.example.com— the bastion’s user and address.-i ~/.ssh/id_ed25519— the private key to authenticate with the bastion.
Add -J other-bastion before the destination if you have to hop through a second jump host first, and -f to background the tunnel once it’s authenticated.
A persistent block in ~/.ssh/config
Typing the full command every time gets old fast. An entry in ~/.ssh/config turns it into ssh pg-tunnel:
Host pg-tunnel
HostName bastion.example.com
User ec2-user
IdentityFile ~/.ssh/id_ed25519
LocalForward 15432 db-internal-host:5432
ServerAliveInterval 30
ServerAliveInterval keeps the connection from being dropped by an idle-timeout firewall between you and the bastion — without it, a tunnel left open overnight can go stale silently.
What to put in the client
Once the tunnel is up, the database “host” from your Postgres client’s point of view is your own machine, not the real server:
host: 127.0.0.1
port: 15432
Use 127.0.0.1 rather than localhost — on some systems localhost resolves to the IPv6 loopback (::1) first, and ssh -L by default only binds the IPv4 loopback address unless told otherwise with -b.
In Tablyne
Open the tunnel first (or keep one running via the ~/.ssh/config alias above), then connect Tablyne the same way you would to any local Postgres: host 127.0.0.1, the forwarded local port, and the database’s real credentials. Tablyne doesn’t need to know a tunnel is involved — from its side, it’s just a Postgres server on localhost. The SSH tunnel command builder generates the exact ssh command and ~/.ssh/config block above from your own bastion and database details.
Frequently asked questions
Do I need to change sslmode when connecting through a tunnel?
No — SSH port forwarding and Postgres's own TLS are independent layers. The SSH tunnel encrypts the hop between your machine and the bastion; whether Postgres itself requires TLS on top of that is still controlled by the server's pg_hba.conf (a hostssl vs host entry) and has nothing to do with the tunnel. Set sslmode to whatever the server's pg_hba.conf actually requires for that connection, not based on the tunnel being present.
Why does the client have to connect to 127.0.0.1 and not the tunnel's remote host name?
ssh -L binds the forwarded port on your local machine's loopback interface by default. The bastion forwards whatever arrives on that local port to the database host and port as seen from the bastion's side — your Postgres client never resolves or contacts the real database hostname directly, so it has to be told to connect to localhost.
What if the local port is already taken?
Pick a different local port in the -L flag (for example -L 15432:dbhost:5432) and point the client at that port instead — the local and remote ports don't have to match.