Infrastructure at your Service

This script provides functionality to setup passwordless authentication in PostgreSQL for defined Server Names.
This is very helpful by using replication via repmgr and is not Linux Distribution specific, it was used till now with SLES 12, SLES 15 and RHEL 8 Clones.

As all of my scripts it provides help how the usage is with -h parameter:

$ [[[email protected] ~]$ sh pgpass.sh -h
$ [[[email protected] ~]$ Usage:
$ [[[email protected] ~]$ pgpass.sh [OPTION]
$ [[[email protected] ~]$ 
$ [[[email protected] ~]$ Options:
$ [[[email protected] ~]$          -p                     server where the primary host is running on (required)
$ [[[email protected] ~]$          -s                     server where the secondary host is running on (required)
$ [[[email protected] ~]$          -h                     prints this help

The parameters -p and -s should be server DNS Names without domain, the domain will be specified within the script.

The script itself:

!/bin/sh

########################################
#  pgpass setup script                 #
#  Author: Karsten Lenz / 2020.07.13   #
########################################

progName=$(basename $0)
domain=put your domain here
postgresHome=/var/lib/pgsql
pgpass=$postgresHome/.pgpass
password=put your password here

function printHelp() {
  printf "Usage:\n"
  printf "${progName} [OPTION]\n\n"
  printf "Options:\n"
  printf "\t -p \t\t\tserver where the primary host is running on (required)\n"
  printf "\t -s \t\t\tserver where the secondary host is running on (required)\n"
  printf "\t -h \t\t\t\tprints this help\n"
}

while getopts p:s:h option 2>/dev/null
do
  case "${option}"
  in
  p) primServer=${OPTARG};;
  s) secdServer=${OPTARG};;
  h) printHelp; exit 2;;
  *) printf "Unsupported option or parameter value missing '$*'\n";
     printf "Run ${progName} -h to print help\n"; exit 1;;
  esac
done

############ Log function ############

logFile=/tmp/pgpass_install.log

function log() {
  echo "$(date +%Y.%m.%d-%H:%M:%S) [$$]$*" | tee -a $logFile
}

if [ -f $logFile ]; then
  continue
else
  touch $logFile
  chmod -R 774 $logFile
  sleep 2
fi

#clean .pgpass
rm -f $pgpass

#set values in .pgpass
log "INFO: #host:port:database:user:password in $pgpass"
echo "#host:port:database:user:password" | tee -a $pgpass
log "INFO: Setting localhost in $pgass"
echo "localhost:5432:*:repmgr:$password" | tee -a $pgpass
log "INFO: Setting 127.0.0.1 in $pgpass"
echo "127.0.0.1:5432:*:repmgr:$password" | tee -a $pgpass
log "INFO: Setting Primary $primServer in $pgpass"
echo "$primServer.$domain:5432:*:repmgr:$password" | tee -a $pgpass
log "INFO: Setting Primary $secdServer in $pgpass"
echo "$secdServer.$domain:5432:*:repmgr:$password" | tee -a $pgpass

#set .pgpass 0600
chmod 0600 $pgpass

The script is used in a customer build cloud solution as part of recurrent setup step of replication using repmgr.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Karsten Lenz
Karsten Lenz

Consultant