Shared server guidance · Question & answer

How do I access a server’s PostgreSQL database through a local SSH tunnel?

An SSH local forward can carry database traffic to a loopback listener on the same server. Bind the local tunnel to 127.0.0.1 so other computers cannot use your forwarded port. SSH access and database authentication are separate checks; a tunnel does not grant database privileges.

IntermediateWindows PowerShell with OpenSSH and a PostgreSQL clientAn SSH-accessible server with a local PostgreSQL TCP listener
Go to the answer

Before you start

  • A verified SSH host key and authorised SSH account.
  • Permission for SSH forwarding.
  • The correct database account, port and database name.

Instructions

1. Open the forward in one local terminal

Replace the example address and account. Verify the server fingerprint through a trusted source on the first connection. Leave this terminal open while using the database.

Local Windows PowerShell
ssh -N -o ExitOnForwardFailure=yes -L 127.0.0.1:15432:127.0.0.1:5432 adminuser@203.0.113.10

Binds local TCP 15432 and forwards to TCP 5432 on the SSH server’s loopback interface. -N runs no remote shell command.

2. Connect from a second local terminal

Use the local forwarded port, not the public server address. Enter the database password only at the prompt.

Local Windows PowerShell
psql -X -h 127.0.0.1 -p 15432 -U appuser -d appdb -W -v ON_ERROR_STOP=1 -c "SELECT current_database(), current_user;"

Verifies an authenticated database query through the tunnel from Windows.

3. Close the forward when finished

Exit database clients, then press Ctrl+C in the SSH tunnel terminal. If the database is on a different machine from the SSH endpoint, this example does not encrypt that additional network hop; use the approved private-network and database TLS design.

Check the result

  • The query reaches the intended database through local port 15432 and public database exposure is unnecessary.

Troubleshooting

SSH stays open but database connection fails
ExitOnForwardFailure confirms local forward setup, not downstream database readiness. Check the remote listener and database authentication separately.

Official references

Use the documentation for your installed version when a flag, service name or platform feature differs.