Skip to content

Instantly share code, notes, and snippets.

@roguh
Created April 10, 2026 03:39
Show Gist options
  • Select an option

  • Save roguh/d30008d042138e27e1ab7ed534ba0d33 to your computer and use it in GitHub Desktop.

Select an option

Save roguh/d30008d042138e27e1ab7ed534ba0d33 to your computer and use it in GitHub Desktop.
psql-pass.sh: A script to connect to postgres using credentials stored in Linux's passwordstore.
#!/bin/sh
# pgsql - connect to PostgreSQL using credentials from pass(1)
set -eu
if [ $# -lt 1 ]; then
echo "Usage: pgsql <pass-prefix> [extra psql args...]" >&2
echo "Reads from these secrets: <pass-prefix>/{host,port,user,dbname,password}" >&2
echo "https://www.passwordstore.org/" >&2
exit 1
fi
PASSROOT="$1"; shift
PGPASSWORD="$(pass show "$PASSROOT/password" | head -n1)" \
exec psql \
-h "$(pass show "$PASSROOT/host" | head -n1)" \
-p "$(pass show "$PASSROOT/port" | head -n1)" \
-U "$(pass show "$PASSROOT/user" | head -n1)" \
-d "$(pass show "$PASSROOT/dbname" | head -n1)" \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment