nix: improve prymn_db script, adding a 'shell' command

This commit is contained in:
Nikos Papadakis 2023-12-14 14:45:22 +02:00
parent 818b20f775
commit 62c40358a2
Signed by untrusted user who does not match committer: nikos
GPG key ID: 78871F9905ADFF02

View file

@ -15,19 +15,33 @@
export PGHOST=/tmp
export DB_LOG=$PROJECT_ROOT_DIR/.db/log
if [ ! -d "$PGDATA" ]; then
initdb "$PGDATA" --auth=trust
fi
start_db() {
if [ ! -d "$PGDATA" ]; then
initdb "$PGDATA" --auth=trust
fi
if ! pg_ctl status >/dev/null; then
pg_ctl start -l "$DB_LOG" -o "-c unix_socket_directories=$PGHOST"
echo "starting your dev database..."
fi
if ! pg_ctl status >/dev/null; then
pg_ctl start -l "$DB_LOG" -o "-c unix_socket_directories=$PGHOST"
echo "starting your dev database..."
fi
user_exists=$(psql --csv -t -d postgres -c "SELECT count(*) FROM pg_user WHERE usename='postgres'")
if [ "$user_exists" != "1" ]; then
createuser -s -h "$PGHOST" postgres
fi
user_exists=$(psql --csv -t -d postgres -c "SELECT count(*) FROM pg_user WHERE usename='postgres'")
if [ "$user_exists" != "1" ]; then
createuser -s -h "$PGHOST" postgres
fi
}
command="''${1-default}"
case $command in
shell)
psql -U postgres
exit 0
;;
start | default | *)
start_db
exit 0
;;
esac
'';
};
}