diff options
Diffstat (limited to 'etc/pgctl')
-rwxr-xr-x | etc/pgctl | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/etc/pgctl b/etc/pgctl new file mode 100755 index 0000000..26ad888 --- /dev/null +++ b/etc/pgctl @@ -0,0 +1,143 @@ +#!/bin/sh +# file : etc/pgctl +# copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file +# +# Designed to simplify controlling brep PostgreSQL server instance. + +. `dirname $0`/config + +cmd="$1" + +# Print usage description and exit. +# +case $cmd in + init|start|stop|status|connect) ;; + *) + echo "Usage: pgctl (init|start|stop|status|connect)" + exit 1 + ;; +esac + +ERROR=0 + +# Initilization includes creating PostgreSQL DB cluster, creating and +# populating brep DB. +# +if test "$cmd" = "init"; then + if test -d "$PG_DATA_DIR"; then + echo "PostgreSQL DB cluster directory $PG_DATA_DIR already exist" + else + echo "creating PostgreSQL DB cluster ..." + pg_ctl initdb -D "$PG_DATA_DIR" + ERROR=$? + + if test $ERROR -eq 0; then + echo "cluster created" + else + echo "cluster creating failed" + exit $ERROR; + fi + fi +fi + +case $cmd in + start|init) + # Start DB server if not running yet. + # + pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1 + + if test $? -eq 0; then + echo "PostgreSQL server already running" + else + if test ! -d "$PG_DATA_DIR"; then + echo "PostgreSQL DB cluster not exist; run '$0 init' first" + exit 1 + fi + + echo "PostgreSQL server starting ..." + + mkdir -p `dirname $PG_OUT_FILE` + mkdir -p "$PG_LOG_DIR" + mkdir -p "$PG_SOCKET_DIR" + + pg_ctl start -D "$PG_DATA_DIR" -w -o \ + "-c port=$PG_PORT -c unix_socket_directories=$PG_SOCKET_DIR \ + -c logging_collector=on -c log_directory=$PG_LOG_DIR" \ + 1>"$PG_OUT_FILE" 2>&1 + + ERROR=$? + + if test $ERROR -eq 0; then + echo "server started" + else + cat "$PG_OUT_FILE" 1>&2 + echo "server starting failed" + exit $ERROR + fi + fi + ;; + + status) + echo "checking PostgreSQL server status ..." + pg_ctl status -D "$PG_DATA_DIR" + ;; + + stop) + pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1 + + if test $? -eq 0; then + echo "PostgreSQL server stopping ..." + pg_ctl stop -D "$PG_DATA_DIR" + ERROR=$? + + if test $ERROR -eq 0; then + : # pg_ctl prints "server stopped" on success + else + echo "server stopping failed" + exit $ERROR + fi + else + echo "PostgreSQL server not running" + fi + ;; + + connect) + echo "connecting to PostgreSQL server ..." + psql --host=$PG_SOCKET_DIR --port=$PG_PORT brep + ;; +esac + +if test "$cmd" = "init"; then + # Create brep DB if not exist. + # + psql --host=$PG_SOCKET_DIR --port=$PG_PORT -c "" brep \ + 1>>"$PG_OUT_FILE" 2>&1 + + ERROR=$? + + if test $ERROR -eq 0; then + echo "brep DB already exist" + else + if test $ERROR -eq 2; then + echo "creating brep DB ..." + createdb --host=$PG_SOCKET_DIR --port=$PG_PORT brep + ERROR=$? + + if test $ERROR -eq 0; then + echo "brep DB created" + else + echo "brep DB creating failed" + exit $ERROR; + fi + else + echo "brep DB existence checking failed" + exit $ERROR; + fi + fi + + # Populate brep DB if required. + # psql --host=$PG_SOCKET_DIR --port=$PG_PORT -c "select count(1) ..." brep \ + # 1>>"$PG_OUT_FILE" 2>&1 + # ... +fi |