Add script to create PostgreSQL user/database.

This commit is contained in:
Viyurz 2024-03-30 17:23:15 +01:00
parent 14b484dd37
commit b27a553dfb
Signed by: Viyurz
SSH key fingerprint: SHA256:IskOHTmhHSJIvAt04N6aaxd5SZCVWW1Guf9tEcxIMj8

24
psql-create-db-user.sh Executable file
View file

@ -0,0 +1,24 @@
#!/bin/bash
read -rp "Database: " database
read -rp "User: " user
read -srp "Password: " password
echo ""
read -rp "Create database $database & user $user? [Y/n]: " answer
if ! [[ $answer =~ ^(Y|y)?$ ]]; then
exit 1
fi
stmts=(
"CREATE USER $user PASSWORD '$password'"
"CREATE DATABASE $database WITH OWNER = $user"
"GRANT ALL ON DATABASE $database TO $user"
)
for stmt in "${stmts[@]}"; do
docker exec postgres psql -c "$stmt"
done