Skip to content

Instantly share code, notes, and snippets.

@samsch
Created August 16, 2019 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsch/689ad0832d81bcb6c424fa02733481d7 to your computer and use it in GitHub Desktop.
Save samsch/689ad0832d81bcb6c424fa02733481d7 to your computer and use it in GitHub Desktop.
Create local Postgres database + user + password
#!/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
@samsch
Copy link
Author

samsch commented Aug 16, 2019

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.

@samsch
Copy link
Author

samsch commented Aug 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment