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:

$ [[root@rocky ~]$ sh pgpass.sh -h
$ [[root@rocky ~]$ Usage:
$ [[root@rocky ~]$ pgpass.sh [OPTION]
$ [[root@rocky ~]$ 
$ [[root@rocky ~]$ Options:
$ [[root@rocky ~]$          -p                     server where the primary host is running on (required)
$ [[root@rocky ~]$          -s                     server where the secondary host is running on (required)
$ [[root@rocky ~]$          -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]nn"
  printf "Options:n"
  printf "t -p tttserver where the primary host is running on (required)n"
  printf "t -s tttserver where the secondary host is running on (required)n"
  printf "t -h ttttprints this helpn"
}

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 helpn"; 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.