Index

Reminder for later, how to setup an SSH tunnel to a remote PostgreSQL database.

Create the script postgres_tunnel.sh:

#!/bin/sh

: ${REMOTEHOST:=example.com}
: ${LOCALPORT:=12345}

# Assuming postgres listens on localhost:5432
ssh "$REMOTEHOST" -L "$LOCALPORT":localhost:5432 -N

Execute it, and connects to the remote database like this:

$ psql 'postgres://username:password@localhost:12345/mydb'