Skip to content

Instantly share code, notes, and snippets.

@wo0dyn
Created November 2, 2012 11:21
Show Gist options
  • Save wo0dyn/4000325 to your computer and use it in GitHub Desktop.
Save wo0dyn/4000325 to your computer and use it in GitHub Desktop.

Create user and database for PostgreSQL

Just a simple script to create a user and its database easily.

Requirements

Tested w/ PostgreSQL 8.4 on Mint 11 (±Ubuntu). Be careful to use md5 in your pg_hba.conf.

Installation

sudo su postgres
mkdir ~/bin
wget <URL of the following script>

Usage

sudo su postgres 
sh ~/bin/create-user-and-database.sh

The script will prompt for username, password and database (using username as a default value). That's it!

#!/bin/sh
read -p "Enter username: " username
stty -echo
read -p "Enter password: " password
stty echo
echo
read -p "Enter database name (${username}): " database
database=${database:-$username}
createuser -SRD $username
createdb $database
echo -n "Setting user's password: "
stty -echo
echo "alter user ${username} with encrypted password '${password}';" | psql
stty echo
echo -n "Grant all privileges on database ${database}: "
echo "grant all privileges on database ${database} to ${username};" | psql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment