2013-02-25 21:46:36 +01:00
|
|
|
#/bin/sh -xe
|
|
|
|
|
2013-05-22 20:11:47 +02:00
|
|
|
ROOT_POSTGRES="sudo -u postgres psql"
|
|
|
|
DEFAULT_DB=""
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
|
|
ROOT_POSTGRES="psql"
|
|
|
|
DEFAULT_DB="postgres"
|
|
|
|
fi
|
|
|
|
|
2013-05-08 05:30:42 +02:00
|
|
|
if [[ $# == 0 ]]; then
|
2013-10-26 02:23:40 +02:00
|
|
|
USERNAME=zulip
|
2013-11-12 15:26:06 +01:00
|
|
|
PASSWORD=$($(dirname $0)/../../bin/get-django-setting LOCAL_DATABASE_PASSWORD)
|
2013-10-26 02:23:40 +02:00
|
|
|
DBNAME=zulip
|
2013-05-08 05:30:42 +02:00
|
|
|
SEARCH_PATH="$USERNAME",public
|
|
|
|
elif [[ $# == 4 ]]; then
|
|
|
|
USERNAME=$1
|
|
|
|
PASSWORD=$2
|
|
|
|
DBNAME=$3
|
|
|
|
SEARCH_PATH=$4
|
|
|
|
else
|
|
|
|
echo "Usage Instructions"
|
2013-10-26 02:23:40 +02:00
|
|
|
echo "Run with either no arguments (sets up devel db for local deploy--zulip with user zulip)"
|
2013-05-08 05:30:42 +02:00
|
|
|
echo "or specify <db-username> <password> <db-name> <user-schema-search-path>"
|
|
|
|
exit
|
|
|
|
fi
|
2013-02-25 21:46:36 +01:00
|
|
|
|
2013-05-22 20:11:47 +02:00
|
|
|
|
|
|
|
$ROOT_POSTGRES $DEFAULT_DB << EOF
|
2013-05-08 05:30:42 +02:00
|
|
|
CREATE USER $USERNAME WITH PASSWORD '$PASSWORD';
|
|
|
|
ALTER USER $USERNAME CREATEDB;
|
|
|
|
ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;
|
|
|
|
EOF
|
|
|
|
|
|
|
|
umask go-rw
|
|
|
|
echo "*:*:*:$USERNAME:$PASSWORD" >> ~/.pgpass
|
|
|
|
chmod go-rw ~/.pgpass
|
|
|
|
|
|
|
|
psql -h localhost postgres $USERNAME <<EOF
|
2013-05-22 20:11:47 +02:00
|
|
|
DROP DATABASE IF EXISTS $DBNAME;
|
2013-05-08 05:30:42 +02:00
|
|
|
CREATE DATABASE $DBNAME;
|
|
|
|
EOF
|
|
|
|
|
2013-05-22 20:11:47 +02:00
|
|
|
DEFAULT_DB=$DBNAME
|
|
|
|
|
|
|
|
$ROOT_POSTGRES $DEFAULT_DB << EOF
|
2013-02-25 21:46:36 +01:00
|
|
|
DROP SCHEMA public CASCADE;
|
|
|
|
EOF
|
|
|
|
|
|
|
|
echo "Database created"
|
|
|
|
|