The idea here is you set yourself up with an entry in your ~/.ssh/config
with a LocalForward to the Postgres port on the host you want to connect to. (The SSH connection itself might be to the same host or a remote bastion. LocalForward is a honey badger.) Then, create or add to your ~/.pg_service.conf
file with a service name that matches the host name from your SSH config. See examples below, and just edit the included dummy values to suit your environment. Note that matching the host names between the two config files is the key to making this work nicely.
(Which port you choose to forward is arbitrary, but it's a good idea NOT to use the default Postgres port of 5432, to avoid any potential conflict with any Postgres service you might have running locally.)
Once you have that configured, the teeny but mighty bash script below will create an [auto-closing SSH tunnel](https://www.g-loaded.e