zulip/scripts/setup/create-db.sql
Alex Vandiver 3ee7690b0b migrations: Fix default search_path.
PostgreSQL's `search_path` sets the order in which schemas are
searched, and the first one is the one in which unqualified objects
are created[^1].

Lower in the file, we create an explicit `zulip` schema -- so we
should not use the database name in the `search_path`.  Switch to the
correct constant `zulip`.

[^1]: https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH
2025-12-16 13:03:27 -08:00

23 lines
683 B
SQL

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