Following on from this post, you probably have multiple versions of PostgreSQL installed on your Mac. In that post, I added an example function to help you manage all these concurrent installs. Today, I'm back with a full-fledged shell script to help manage all this. Without further ado, the script:
wantedver=$1 norestart=
is the version requested installed?
brew ls --version postgresql-${wantedver} &>/dev/null if [[ $? -eq 0 ]] ; then # yes, carry on : else # nope, so install it echo -n "Installing PostgreSQL ${wantedver}... " brew install postgresql-${wanted_ver} &>/tmp/brew.out if [[ $? -eq 0 ]] ; then echo "done" else echo "FAILED!" cat /tmp/brew.out exit fi
is postgresql is running?
for i in /usr/local/var/postgres/* do checkver=$(basename ${i}) isrunning=$(ps -few | egrep -- "[p]ostgres.-D.${check_ver}")
if [[ -z ${isrunning} ]] ; then # nope, carry on : else # it is. is it the requested version? if [[ "${wantedver}" = "${checkver}" ]] ; then # yup, carry on norestart=t else # nope, so kill it echo -n "Stopping PostgreSQL ${checkver}... " /usr/local/opt/postgresql-${checkver}/bin/pgctl -D /usr/local/var/postgres/${checkver} stop -w -mf &>/tmp/stop.out if [[ $? -eq 0 ]] ; then echo "done!" else echo "FAILED!" cat /tmp/stop.out fi fi fi done
what version is active?
active_ver=$(/usr/bin/stat -f %Y /usr/local/bin/psql | cut -d\/ -f3 | cut -d- -f2)
is the active version the requested version?
if [[ "${activever}" = "${wantedver}" ]] ; then # yup, carry on : else # nope, so deactivate it echo -n "Deactivating PostgreSQL ${activever}... " brew unlink --force --overwrite postgresql-${activever} &>/dev/null echo "done!" # and activate the correct version echo -n "Activating PostgreSQL ${wantedver}... " brew link --force --overwrite postgresql-${wantedver} &>/dev/null echo "done!" fi
point to the correct data dir
export PGDATA=/usr/local/var/postgres/${wanted_ver}
should we be starting a cluster?
if [[ "${norestart}" = "t" ]] ; then # nope, carry on : else # yup. has the cluster been initialized? if [[ ! -d ${PGDATA} ]] ; then # nope, so let's do that echo -n "Initializing PostgreSQL ${wantedver} cluster... " mkdir ${PGDATA} initdb -k ${PGDATA} &>/dev/null || initdb ${PGDATA} &>/dev/null echo "done!" else # yup, carry on : fi # start the cluster echo -n "Starting PostgreSQL ${wantedver}... " pgctl -D ${PGDATA} start &>/tmp/postmaster.out if [[ $? -eq 0 ]] ; then echo "done!" else echo "FAILED!" cat /tmp/postmaster.out fi ret=1 while [[ ${ret} -eq 1 ]] do # wait for the cluster to be available before exiting pg_isready -q ret=$? done fi
But what does it do? It's pretty simple actually. When you call this script, you tell it what version of PostgreSQL you want:
doug@Douglass-MacBook-Pro ~ » pg 9.6
and then the script does the following:
- checks if the requested version is installed, and installs it if not
- checks if another version of PostgreSQL is running, and stops it
- checks if another version is linked as the active version, and unlinks it
- links the requested version as the active version
- sets PGDATA to point to the requested version's data cluster
- does an
initdb
for the requested version if needed - starts the requested version's cluster
I'll be the first to admit that the script could use additional work, but it's functional enough to start using today. As I continue to improve the script, I'll update the gist with those changes, so check back every so often.
Enjoy.