Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Postgres sandbox control script sort of.
#!/bin/bash
db="${PWD}/data"
log_file="${db}/postgres.log"
port=9999
#------------------------------------------------------------------------------
function is_alive() {
result=$(pg_ctl status -p ${port} -D ${db} | grep "server is running")
if [ "${result}" != "" ]; then
echo true
else
echo false
fi
}
#------------------------------------------------------------------------------
function status() {
live=$(is_alive)
if ${live}; then
echo "Postgres is running:"
echo " db: ${db}"
echo " log: ${log_file}"
echo " port: ${port}"
else
echo "Postges is NOT running (on port ${port})."
fi
}
#------------------------------------------------------------------------------
function start() {
echo -n "Starting ... "
if [ ! -e "${db}" ]; then
echo "alas!"
echo " - No db found at '${db}'."
echo " - Try '${0} setup'."
exit 1
fi
pg_ctl -o "-p ${port}" -l ${log_file} -D ${db} start > /dev/null
sleep 2
echo "done."
cat <<EOF
- data directory: ${db}
- log file: ${log_file}
EOF
}
#------------------------------------------------------------------------------
function stop() {
echo -n "Stopping ... "
if [ ! -e "${db}" ]; then
echo ""
echo " - No db found at '${db}' so doing nothing."
exit 1
fi
pg_ctl -o "-p ${port}" -D ${db} stop &> /dev/null
sleep 2
echo "done."
}
#------------------------------------------------------------------------------
function clean() {
echo -n "Cleaning ... "
if [ -e "${db}" ]; then
rm -rf ${db}
fi
echo "done."
}
#------------------------------------------------------------------------------
function setup() {
echo "Setting up..."
if [ -e "${db}" ]; then
echo " - Sandbox db still exists, clean before running setup."
exit 1
fi
echo " - Initializing DB in ${db}."
initdb ${db}
echo -n " - starting postgres process ... "
start > /dev/null
sleep 3
echo "done."
echo -n "Role: "
psql template1 --port ${port} <<-EOF
CREATE ROLE datomic LOGIN PASSWORD 'datomic';
EOF
echo -n "Database: "
psql template1 --port ${port} <<-EOF
CREATE DATABASE notes
WITH OWNER = datomic
TEMPLATE template0
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
CONNECTION LIMIT = -1;
EOF
echo -n "Table: "
psql notes --port ${port} <<-EOF
CREATE TABLE datomic_kvs (
id text NOT NULL,
rev integer,
map text,
val bytea,
CONSTRAINT pk_id PRIMARY KEY (id ))
WITH (
OIDS=FALSE
);
ALTER TABLE datomic_kvs OWNER TO datomic;
GRANT ALL ON TABLE datomic_kvs TO datomic;
GRANT ALL ON TABLE datomic_kvs TO public;
EOF
}
#------------------------------------------------------------------------------
function usage() {
sed -e 's/^[ ]\{6\}//g' <<-EOF
usage: ${0} options
This script (barely) automates setting up a postgres sandbox for datomic.
COMMANDS:
start Start postgres.
stop Stop postgres.
status Is postgres running?
setup Set up datomic specific artifacts in postgres.
clean Remove the postgres sandbox.
EOF
}
#------------------------------------------------------------------------------
cmd="${1}"
live=$(is_alive)
case $1 in
start)
if ${live}; then
echo "Postgres already started."
else
start
fi
;;
stop)
if ${live}; then
stop
else
echo "Postgres is already stopped."
fi
;;
clean)
if ${live}; then
echo "Postgres is still running. Please stop before cleaning."
else
clean
fi
;;
setup)
if ${live}; then
echo "Postgre is still running. Stop, then clean, before setup."
else
setup
fi
;;
status)
status
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.