Created
November 4, 2020 20:30
-
-
Save michellescripts/e7fed0b3664d3716cc9946a23b3a7870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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