zero-to-axum/scripts/start_dev_db.sh
2025-05-22 11:57:37 -05:00

50 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
SUPERUSER_NAME="postgres"
SUPERUSER_PASS="nOMvDptXFk"
USER_NAME="ztoa"
USER_PASS="0zpVXAVK20"
APP_DB_NAME="ztoa"
CONTAINER_NAME="postgres"
DATABASE_URL="postgres://ztoa:0zpVXAVK20@localhost:5432/ztoa"
# Launch postgres
podman run \
--replace \
--env POSTGRES_USER=${SUPERUSER_NAME} \
--env POSTGRES_PASSWORD=${SUPERUSER_PASS} \
--health-cmd="pg_isready -U ${SUPERUSER_NAME} || exit 1" \
--health-interval=1s \
--health-timeout=5s \
--health-retries=5 \
--publish 5432:5432 \
--detach \
--name "${CONTAINER_NAME}" \
postgres -N 1000
# Wait for Postgres to be ready
until [ \
"$(podman inspect -f "{{.State.Health.Status}}" ${CONTAINER_NAME})" == \
"healthy" \
]; do
>&2 echo "Postgres is still unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up and running"
# Create the application user
CREATE_QUERY="CREATE USER ${USER_NAME} WITH PASSWORD '${USER_PASS}';"
podman exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER_NAME}" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${USER_NAME} CREATEDB;"
podman exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER_NAME}" -c "${GRANT_QUERY}"
sqlx database create
sqlx migrate run