55 lines
1.8 KiB
Bash
Executable File
55 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Start / stop the development Postgres.
|
|
#
|
|
# Uses the docker compose plugin when it is available (that is the case on the
|
|
# server); when it is not, it brings up the same container with plain docker run.
|
|
#
|
|
# ./scripts/db.sh up | down | psql | logs | reset
|
|
set -euo pipefail
|
|
|
|
NAME=szabadpalya-db
|
|
IMAGE=postgres:17-alpine
|
|
PORT=5433
|
|
USER=szabadpalya
|
|
PASS=szabadpalya
|
|
DB=szabadpalya
|
|
VOLUME=szabadpalya_pgdata
|
|
|
|
has_compose() { docker compose version >/dev/null 2>&1; }
|
|
|
|
up() {
|
|
if has_compose; then
|
|
docker compose up -d db
|
|
else
|
|
if docker ps -a --format '{{.Names}}' | grep -qx "$NAME"; then
|
|
docker start "$NAME" >/dev/null
|
|
else
|
|
docker volume create "$VOLUME" >/dev/null
|
|
docker run -d --name "$NAME" --restart unless-stopped \
|
|
-e POSTGRES_USER="$USER" -e POSTGRES_PASSWORD="$PASS" -e POSTGRES_DB="$DB" \
|
|
-p "$PORT:5432" -v "$VOLUME:/var/lib/postgresql/data" \
|
|
--health-cmd "pg_isready -U $USER -d $DB" --health-interval 5s \
|
|
"$IMAGE" >/dev/null
|
|
fi
|
|
fi
|
|
printf 'waiting for the database'
|
|
for _ in $(seq 1 40); do
|
|
if docker exec "$NAME" pg_isready -U "$USER" -d "$DB" >/dev/null 2>&1; then
|
|
echo " -> ready (localhost:$PORT)"; return 0
|
|
fi
|
|
printf '.'; sleep 1
|
|
done
|
|
echo " -> did not start in time"; docker logs --tail 20 "$NAME"; return 1
|
|
}
|
|
|
|
case "${1:-up}" in
|
|
up) up ;;
|
|
down) has_compose && docker compose stop db || docker stop "$NAME" >/dev/null; echo "stopped" ;;
|
|
psql) docker exec -it "$NAME" psql -U "$USER" -d "$DB" ;;
|
|
logs) docker logs -f "$NAME" ;;
|
|
reset) docker rm -f "$NAME" >/dev/null 2>&1 || true
|
|
docker volume rm "$VOLUME" >/dev/null 2>&1 || true
|
|
echo "removed — run again: ./scripts/db.sh up" ;;
|
|
*) echo "usage: $0 {up|down|psql|logs|reset}"; exit 2 ;;
|
|
esac
|