Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Created June 21, 2021 12:54
Show Gist options
  • Save AlexeyRaga/69f1831eabf88461c4a336718ebb53f2 to your computer and use it in GitHub Desktop.
Save AlexeyRaga/69f1831eabf88461c4a336718ebb53f2 to your computer and use it in GitHub Desktop.
Postgres in Nix shell
with import <nixpkgs> {};
let
pg_root = builtins.toString ./. + "/.postgres";
pg_user = "postgres";
pg_db = "service";
pg_bind = "127.0.0.1";
svc_pg_user = "dev";
svc_pg_password = "changeme";
in mkShell {
name = "fsharp-stub-service";
buildInputs = [
postgresql
];
shellHook = ''
export PGDATA=${pg_root}/data
export PGHOST=${pg_root}/postgres
export PGLOG=$PGHOST/pg.log
function cleanup_pg {
echo "Stopping DB..."
pg_ctl stop
echo "Destroying DB..."
rm -rf "${pg_root}"
}
trap cleanup_pg EXIT
if [ ! -d ${pg_root} ]; then
mkdir -p $PGHOST
mkdir -p $PGDATA
echo "Initialising DB..."
initdb $PGDATA --auth=trust --auth-host=trust > "${pg_root}/db-init.log" 2>&1
echo "Starting server..."
pg_ctl start -l $PGLOG -o '-c listen_addresses= -c unix_socket_directories=$PGHOST'
createuser -s ${svc_pg_user}
createdb ${pg_db} -O ${svc_pg_user}
fi
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment