postgresql-init-db: Support arbitrary database user and dbname.

Co-authored-by: Adam Birds <adam.birds@adbwebdesigns.co.uk>
This commit is contained in:
Alex Vandiver 2021-05-24 20:45:24 -07:00 committed by Tim Abbott
parent 54c222d3f8
commit 1d59330cbc
2 changed files with 27 additions and 12 deletions

View File

@ -1,16 +1,17 @@
\connect postgres \connect postgres
DROP DATABASE IF EXISTS zulip; DROP DATABASE IF EXISTS :"dbname";
DO $$BEGIN SELECT format($$BEGIN
CREATE USER zulip; CREATE USER %I;
EXCEPTION WHEN duplicate_object THEN EXCEPTION WHEN duplicate_object THEN
RAISE NOTICE 'zulip user already exists'; RAISE NOTICE 'user already exists';
END$$; END$$, :'dbuser') AS code \gset
ALTER ROLE zulip SET search_path TO zulip,public; DO :'code';
CREATE DATABASE zulip ALTER ROLE :"dbuser" SET search_path TO :"dbname",public;
OWNER=zulip CREATE DATABASE :"dbname"
OWNER=:dbuser
ENCODING=UTF8 ENCODING=UTF8
LC_COLLATE='C.UTF-8' LC_COLLATE='C.UTF-8'
LC_CTYPE='C.UTF-8' LC_CTYPE='C.UTF-8'
TEMPLATE=template0; TEMPLATE=template0;
\connect zulip \connect :"dbname"
CREATE SCHEMA zulip AUTHORIZATION zulip; CREATE SCHEMA zulip AUTHORIZATION :"dbuser";

View File

@ -10,11 +10,15 @@ set -x
# What user should we use for connecting to the database # What user should we use for connecting to the database
POSTGRES_USER="${POSTGRES_USER:-postgres}" POSTGRES_USER="${POSTGRES_USER:-postgres}"
# What database name and username to use when connecting to the database
DATABASE_NAME=$(crudini --get /etc/zulip/zulip.conf postgresql database_name 2>/dev/null || echo zulip)
DATABASE_USER=$(crudini --get /etc/zulip/zulip.conf postgresql database_user 2>/dev/null || echo zulip)
# This psql command may fail because the Zulip database doesnt exist, # This psql command may fail because the Zulip database doesnt exist,
# hence the &&. # hence the &&.
if records="$( if records="$(
cd / # Make sure the current working directory is readable by postgres cd / # Make sure the current working directory is readable by postgres
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip" su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM $DATABASE_NAME.zerver_message;' $DATABASE_USER"
)" && [ "$records" -gt 200 ]; then )" && [ "$records" -gt 200 ]; then
set +x set +x
echo "WARNING: This will delete your Zulip database which currently contains $records messages." echo "WARNING: This will delete your Zulip database which currently contains $records messages."
@ -38,9 +42,19 @@ su -s /usr/bin/env - -- "$POSTGRES_USER" \
( (
cd / # Make sure the current working directory is readable by postgres cd / # Make sure the current working directory is readable by postgres
su "$POSTGRES_USER" -c 'psql -v ON_ERROR_STOP=1 -e' su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -v dbname=$DATABASE_NAME -v dbuser=$DATABASE_USER -e"
) <"$(dirname "$0")/create-db.sql" ) <"$(dirname "$0")/create-db.sql"
# Set a postgres password if the postgres username is not "zulip".
# When the username is zulip, we rely on running as the zulip system
# user for authentication via postgres' peer authentication.
if [ "$DATABASE_USER" != "zulip" ]; then
PASSWORD=$(crudini --get /etc/zulip/zulip-secrets.conf secrets postgres_password)
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -e postgres" <<EOF
ALTER ROLE $DATABASE_USER PASSWORD '$PASSWORD';
EOF
fi
# Clear memcached to avoid contamination from previous database state # Clear memcached to avoid contamination from previous database state
"$(dirname "$0")/flush-memcached" "$(dirname "$0")/flush-memcached"