Files
App_PostgreSQL_DB_bash_creator/postgresql_datauser.sh

43 lines
1.1 KiB
Bash
Raw Permalink Normal View History

# postgresql_datauser.sh
# ----------------------
#
# Very basic script to create a database on mysql whit its own user
# and password to be used by an application like a website,
# cloud, etc.
# As of the date below it has only been tested on mariaDB sql server.
#
# by Jony Silva, with AI help
# Wed 31 Dec 06:30:54 UTC 2025
# Anti-copyright and/or BeerWare.
#!/usr/bin/env bash
message1="Creating database, user and password for new application."
message2="Give me DataBase user:"
message3="Give me DataBase user password:"
message4="Give me DataBase name:"
message5="Give me host (press enter for default \"localhost\"):"
echo $message1
echo $message4
read newDb
echo $message2
read newUser
echo $message3
read newDbPassword
echo $message5
read host
if [ -z "$host" ]
then host=localhost
fi
# PostgreSQL commands
sudo -u postgres psql -p 5432 << EOF # Change port accordingly
CREATE DATABASE ${newDb};
CREATE USER ${newUser} WITH PASSWORD '${newDbPassword}';
GRANT ALL PRIVILEGES ON DATABASE ${newDb} TO ${newUser};
\c ${newDb}
GRANT ALL ON SCHEMA public TO ${newUser};
EOF
echo "Database '${newDb}' and user '${newUser}' created successfully!"