Skip to content

Instantly share code, notes, and snippets.

@esbullington
Last active September 15, 2015 14:13
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 esbullington/548f3b7e51273f61be43 to your computer and use it in GitHub Desktop.
Save esbullington/548f3b7e51273f61be43 to your computer and use it in GitHub Desktop.
Bash script for easy bootstrapping a PostgreSQL database
#!/bin/bash
make_database () {
sudo -u postgres psql -d template1 -U postgres -c "CREATE USER $db_user WITH PASSWORD '$db_password';";
sudo -u postgres psql -d template1 -U postgres -c "CREATE DATABASE $db_name;";
sudo -u postgres psql -d template1 -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE $db_name to $db_user;";
sudo service postgresql restart;
}
echo "Please enter a database user: "
read -e db_user
echo "You entered: $db_user"
echo "Please enter a database name: "
read -e db_name
echo "You entered: $db_name"
echo "Please enter a database password: "
read -e -s db_password
echo "You entered: $db_password"
while true; do
echo "You selected database $db_name owned by user $db_user and your selected password."
read -p "Do you wish to configure this database? (y/n) " yn
case $yn in
[Yy]* ) echo "configuring...."; make_database; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment