Skip to content

Instantly share code, notes, and snippets.

@rshk
Last active December 15, 2021 12:41
Show Gist options
  • Save rshk/8a0eb186d6cfcf18f9a886ecd1609d33 to your computer and use it in GitHub Desktop.
Save rshk/8a0eb186d6cfcf18f9a886ecd1609d33 to your computer and use it in GitHub Desktop.
#!/bin/bash
CONTAINER_NAME=my-postgres-database
VOLUME_NAME="${CONTAINER_NAME}-data"
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_IMAGE=postgres:14
case "$1" in
start)
docker run --rm --name "$CONTAINER_NAME" \
-e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
-d -p 5432:5432 \
--mount type=volume,src="$VOLUME_NAME",target=/var/lib/postgresql/data \
"$POSTGRES_IMAGE"
;;
stop)
docker kill "$CONTAINER_NAME"
;;
connect)
docker exec -it "$CONTAINER_NAME" psql -U postgres
;;
cleanup)
docker volume rm "$VOLUME_NAME"
;;
*)
cat <<EOF
Usage: db.sh <command>
Commands:
start
Start docker container in the background
stop
Stop docker container
connect
Connect to running database server
cleanup
Delete docker volume attached to the container
EOF
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment