diff --git a/tools/postgres-init-db b/tools/postgres-init-db index 341e229d58..c960c7807b 100755 --- a/tools/postgres-init-db +++ b/tools/postgres-init-db @@ -7,6 +7,8 @@ if [ "$(uname)" = "Darwin" ]; then DEFAULT_DB="postgres" fi +VAGRANTUSERNAME=$(whoami) + if [[ $# == 0 ]]; then USERNAME=zulip PASSWORD=$($(dirname $0)/../bin/get-django-setting LOCAL_DATABASE_PASSWORD) @@ -30,6 +32,19 @@ $ROOT_POSTGRES $DEFAULT_DB << EOF CREATE USER $USERNAME WITH PASSWORD '$PASSWORD'; ALTER USER $USERNAME CREATEDB; ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH; + +DO \$\$ + BEGIN + IF NOT EXISTS ( + SELECT * FROM pg_catalog.pg_user WHERE usename -- [sic] + = '$VAGRANTUSERNAME') THEN + + CREATE USER $VAGRANTUSERNAME; + END IF; + END +\$\$ ; +GRANT $USERNAME TO $VAGRANTUSERNAME; +ALTER ROLE $VAGRANTUSERNAME SET search_path TO $SEARCH_PATH; EOF umask go-rw