Skip to content

Instantly share code, notes, and snippets.

@PatrickRoumanoff
Last active April 28, 2023 20:56
Show Gist options
  • Save PatrickRoumanoff/d2dfa2d02ae2d79d98994c11aa670efc to your computer and use it in GitHub Desktop.
Save PatrickRoumanoff/d2dfa2d02ae2d79d98994c11aa670efc to your computer and use it in GitHub Desktop.
Shell script to reset a postgres DB running in a docker container with no exposed network port
#!/bin/bash
# To create the seed run
# docker exec -t <dockerid> pg_dump -U postgres <dbname> -fc > seed.dump
set -euo pipefail
IFS=$'\n\t'
if [ $# -eq 0 ]; then
echo "dockerId must be provided"
echo "$0 <dockerId> [filename.dump:-seed.dump] [db_name:-DB_NAME:–postgres]"
exit -1
fi
DOCKER_ID="${1}"
SEED="${2:-seed.dump}"
DB_NAME="${3:-${DB_NAME:=postgres}}"
echo "#######################"
echo "# ${0} "
echo "# DOCKER_ID = ${DOCKER_ID} "
echo "# SEED = ${SEED} "
echo "# DB_NAME = ${DB_NAME} "
echo "#######################"
docker exec $DOCKER_ID dropdb -U postgres "${DB_NAME}" || true
docker exec $DOCKER_ID createdb -U postgres "${DB_NAME}"
docker exec -i $DOCKER_ID pg_restore -U postgres -d "${DB_NAME}" < "$SEED"
echo "# ${0} done"
echo "#######################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment