Skip to content

Instantly share code, notes, and snippets.

@aks
Last active October 25, 2018 22:49
Show Gist options
  • Select an option

  • Save aks/567fc8fe5e2b86f32d0b4813fc5a1001 to your computer and use it in GitHub Desktop.

Select an option

Save aks/567fc8fe5e2b86f32d0b4813fc5a1001 to your computer and use it in GitHub Desktop.
Script to setup `~/.pgpass` from `~/.pgpass.copy` or `~postgres/.pgpass`
#!/bin/bash
# setup-pgpass
# execute (or source) this script to ensure that ~/.pgpass has the most recent credentials
#
# if ~/.pgpass.copy exists, it is used as a source; otherwise, compare against
# ~postgres/.pgpass, and create ~/.pgpass and ~/.pgpass.copy from
# ~postgres/.pgpass
MY_PGPASS=~/.pgpass
PG_PGPASS=~postgres/.pgpass
pgpass_too_small() {
(( `wc -l <$MY_PGPASS` <= 1 ))
}
pgpass_too_old() {
[[ $MY_PGPASS -ot $PG_PGPASS ]]
}
pgpass_non_existant() {
[[ ! -f $MY_PGPASS ]]
}
pgpass_copy_newer() {
[[ $MY_PGPASS -ot $MY_PGPASS.copy ]]
}
talk() { echo 1>&2 "$*" ; }
if pgpass_copy_newer ; then
cp $MY_PGPASS.copy $MY_PGPASS
talk "$MY_PGPASS updated from copy"
elif pgpass_non_existant || pgpass_too_old || pgpass_too_small ; then
sudo cp $PG_PGPASS $MY_PGPASS
sudo chown $USER $MY_PGPASS
sudo chmod 600 $MY_PGPASS
cp $MY_PGPASS $MY_PGPASS.copy
talk "$MY_PGPASS initialized"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment