Skip to content

Instantly share code, notes, and snippets.

@michellescripts
Created November 4, 2020 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michellescripts/e7fed0b3664d3716cc9946a23b3a7870 to your computer and use it in GitHub Desktop.
Save michellescripts/e7fed0b3664d3716cc9946a23b3a7870 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
database=${database:-some_database}
user=${user:-$database}
password=${password:-password}
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
v="${1/--/}"
declare $v="$2"
# echo $1 $2 // Optional to see the parameter:value result
fi
shift
done
cat > /tmp/create_db.sql <<EOL
CREATE ROLE "$user";
ALTER ROLE "$user" with password '${password}';
ALTER ROLE "$user" with login;
CREATE DATABASE "$database";
GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$user";
EOL
docker create \
--name a-postgres \
--env POSTGRES_PASSWORD=password \
--rm \
-p 5432:5432 \
postgres:10
docker cp /tmp/create_db.sql a-postgres:/docker-entrypoint-initdb.d/
docker start a-postgres -a
---
Run as:
give-me-postgres --database <DB NAME> --user <USER NAME> --password <PASSWORD>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment