Created
August 16, 2019 19:19
-
-
Save samsch/689ad0832d81bcb6c424fa02733481d7 to your computer and use it in GitHub Desktop.
Create local Postgres database + user + password
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
#!/bin/bash | |
name=$1 | |
if [[ -n "$name" ]]; then | |
if [[ $name =~ ^[a-z_]+$ ]]; then | |
sudo -u postgres createdb $name | |
sudo -u postgres createuser $name | |
sudo -u postgres psql -c "alter user $name with encrypted password '$name';" | |
sudo -u postgres psql -c "grant all privileges on database $name to $name;" | |
else | |
echo "Name must be snake case and only letters" | |
fi | |
else | |
echo "Missing db/user name argument" | |
fi |
This it the reference I went from creating this: https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THIS IS FOR LOCAL DEVELOPMENT USE ONLY! If your database is network accessible, you should use a real password.
Save file, set executable, run
./pg_add_db.sh <name>
.It creates a database named
<name>
, a user named<name>
with password<name>
with full permissions to that database.