Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Created July 3, 2022 14:13
Show Gist options
  • Save vrdhn/e15e82417d5f938c90df57c44bd8f7c5 to your computer and use it in GitHub Desktop.
Save vrdhn/e15e82417d5f938c90df57c44bd8f7c5 to your computer and use it in GitHub Desktop.
Disposable postgres installtions for testing .. don't bother with docker etc.
#!/bin/bash
PGDATADIR=pgdata
mkdir -p $PGDATADIR
usage () {
echo "** Disposable postgres installtions for testing **"
echo ""
echo "$0 <cmd> [ args .... ] "
echo " =cmd= =args...="
echo " list"
echo " init Name Port"
echo " up Name Port"
echo " down Name Port"
echo " clean Name Port"
echo " logs Name Port"
echo " uri Name Port"
}
fn_check()
{
name=$1
port=$2
if [ -z "$name" -o -z "$port" ] ; then
echo "Provide NAME and PORT" >&2
exit 1;
fi
}
fn_pg_uri()
{
name=$1
port=$2
if [ -z "$name" -o -z "$port" ] ; then
echo "** Provide NAME and PORT" >&2
exit 1;
fi
echo "postgresql://$name:$name@localhost:$port/$name"
}
fn_pg_dir()
{
name=$1
port=$2
if [ -z "$name" -o -z "$port" ] ; then
echo "Provide NAME and PORT" >&2
exit 1;
fi
echo "$PGDATADIR/$name:$port"
}
fn_pg_list()
{
for dir in $PGDATADIR/*
do
bn=$(basename $dir)
if [ "x$bn" = "x*" ] ; then
echo "Nothing found."
else
name=${bn%:*}
port=${bn##*:}
fn_pg_status $name $port
fi
done
}
fn_pg_status()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port )
uri=$(fn_pg_uri $name $port )
pg_ctl -D $dir status > /dev/null
status=$?
case "$3" in
"" )
case $status in
0) echo "$name:$port : Server running, URI: $(fn_pg_uri $name $port)";;
3) echo "$name:$port : Server NOT running";;
4) echo "$name:$port : Invalid data directory";;
*) echo "$name:$port : unknown status $?";;
esac
;;
running )
if [ $status != 0 ]
then
echo "** Expected server to be running!";
exit 1;
fi;;
stopped )
if [ $status != 3 ]
then
echo "** Expected server to be stopped!";
exit 1;
fi;;
missing )
if [ $status != 4 ]
then
echo "** Expected server to be uninitialized!";
exit 1;
fi;;
* ) echo "Unknown $3" > 2 ; exit 1 ;;
esac
}
fn_pg_init()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
uri=$(fn_pg_uri $name $port)
root=postgres
root_uri=$(fn_pg_uri "$root" "$2")
if fn_pg_status $name $port missing
then
TZ=Etc/UTC initdb --auth-host password -D $dir --encoding UTF8 --locale=C -U $root --pwfile=<(echo $root)
fn_pg_up $name $port
psql $root_uri -c "CREATE ROLE $name LOGIN PASSWORD '$name';"
psql $root_uri -c "CREATE DATABASE $name OWNER $name;"
fn_pg_down $name $port
fi
}
fn_pg_up ()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
if fn_pg_status $name $port stopped
then
pg_ctl -D $dir -o '-h localhost' -o -i -o '-k ""' -o '-d 1' -o "-p $port" -l $dir/pg.log start
fi
}
fn_pg_down ()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
if fn_pg_status $name $port running
then
pg_ctl -D $dir stop
fi
}
fn_pg_clean ()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
if fn_pg_status $name $port stopped
then
rm -fr $dir
fi
}
fn_pg_psql ()
{
echo "$@"
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
uri=$(fn_pg_uri $name $port)
if fn_pg_status $name $port running
then
psql $uri "${@:2}"
fi
}
fn_pg_log ()
{
name=$1
port=$2
fn_check $name $port
dir=$(fn_pg_dir $name $port)
exec tail -F $dir/pg.log
}
case "$1" in
ls | list ) fn_pg_list ;;
init ) fn_pg_init "$2" "$3" ;;
up ) fn_pg_up "$2" "$3" ;;
down ) fn_pg_down "$2" "$3" ;;
uri ) fn_pg_uri "$2" "$3" ;;
psql ) fn_pg_psql "$2" "$3" "${@:3}";;
clean ) fn_pg_clean "$2" "$3" ;;
log ) fn_pg_log "$2" "$3" ;;
* ) usage;exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment