Skip to content

Instantly share code, notes, and snippets.

@nmattia
Created March 12, 2018 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmattia/5e32d5cc945122492932d9eee601dc61 to your computer and use it in GitHub Desktop.
Save nmattia/5e32d5cc945122492932d9eee601dc61 to your computer and use it in GitHub Desktop.
postgresql in a shell
#!/usr/env/bin bash
# vim: set ft=bash
ROOT=$PWD
read -r -d '' ADJECTIVES <<'EOF'
agreeable
brave
calm
delightful
eager
faithful
gentle
happy
EOF
read -r -d '' NOUNS <<'EOF'
child
day
life
man
people
thing
time
way
woman
world
year
EOF
read -r -d '' NATS <<'EOF'
american
australian
british
chinese
french
german
indian
italian
japanese
russian
EOF
start_pg()
{
adj=$(echo "$ADJECTIVES" | shuf | head -n 1)
noun=$(echo "$NOUNS" | shuf | head -n 1)
nat=$(echo "$NATS" | shuf | head -n 1)
name="$adj"-"$nat"-"$noun"
FOO_PGDATA="$ROOT/.pg/$name"
echo "Creating $FOO_PGDATA directory"
mkdir -p $FOO_PGDATA
chmod 0700 -R $FOO_PGDATA
echo "Starting postgres..."
PGDATA=$FOO_PGDATA initdb
echo "PG_HBA:"
cat $FOO_PGDATA/pg_hba.conf
# TODO(1): write hba_conf here
# TODO(2): accept:
# $name NAME
# $user USERNAME PASSWORD
# $port PORT
# TODO(3): export env vars with PGPORT and all
PGDATA=$FOO_PGDATA postgres > $FOO_PGDATA/pglog 2>&1 &
echo "Postgres started, are logs written to $FOO_PGDATA/pglog."
FOO_PID=$!
TRAPS="name=$name FOO_PGDATA=$FOO_PGDATA FOO_PID=$FOO_PID stop_pg; $TRAPS"
echo "Done."
trap "$TRAPS" EXIT
}
stop_pg()
{
echo stopping $name
echo killing $FOO_PID
kill $FOO_PID
wait $FOO_PID
echo removing $FOO_PGDATA
rm -rf $FOO_PGDATA
}
let
pkgs = import <nixpkgs> {};
in pkgs.mkShell
{
shellHook = builtins.readFile ./pg-nix;
buildInputs = [ pkgs.postgresql ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment