Skip to content

Instantly share code, notes, and snippets.

@nilsmeyer
Created January 21, 2020 16:16
Show Gist options
  • Save nilsmeyer/ee3902fe2662cb828b8b1015b8bfc96f to your computer and use it in GitHub Desktop.
Save nilsmeyer/ee3902fe2662cb828b8b1015b8bfc96f to your computer and use it in GitHub Desktop.
#!/bin/bash
umask 077
if [ "$1" == "" ]
then
echo "Usage: pg_connect SSH_HOST psql_options"
fi
ssh_host=$1
shift
sessiondir=~/.pgsession/${ssh_host}
mkdir -p ${sessiondir}
ssh -o StreamLocalBindUnlink=yes -o ControlMaster=auto -o ControlPath=${sessiondir}/.ssh-control -O check -M -f -N -T -L ${sessiondir}/.s.PGSQL.5432:/var/run/postgresql/.s.PGSQL.5432 ${ssh_host}
psql -h ${sessiondir} $*
netstat -nx | grep "${sessiondir}/.ssh-control" || ssh -o ControlPath=${sessiondir}/.ssh-control -O exit ${ssh_host}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment