39 lines
1.4 KiB
SQL
39 lines
1.4 KiB
SQL
-- Creates the optional PostgreSQL metrics login on first volume
|
|
-- initialization. The image entrypoint runs *.sql files with psql
|
|
-- (ON_ERROR_STOP=1, as POSTGRES_USER in POSTGRES_DB), independent of file
|
|
-- mode, unlike *.sh files, which it executes or sources depending on the
|
|
-- executable bit reported by the bind mount. \getenv needs psql 15 or newer.
|
|
--
|
|
-- Both credentials empty or unset: nothing happens. Exactly one set: the
|
|
-- initialization fails. Both set: the role is created if missing and gets
|
|
-- pg_monitor.
|
|
|
|
\getenv exporter_user POSTGRES_EXPORTER_USER
|
|
\getenv exporter_password POSTGRES_EXPORTER_PASSWORD
|
|
\if :{?exporter_user}
|
|
\else
|
|
\set exporter_user ''
|
|
\endif
|
|
\if :{?exporter_password}
|
|
\else
|
|
\set exporter_password ''
|
|
\endif
|
|
|
|
SELECT :'exporter_user' <> '' AS has_exporter_user,
|
|
:'exporter_password' <> '' AS has_exporter_password
|
|
\gset
|
|
|
|
\if :has_exporter_user
|
|
\if :has_exporter_password
|
|
SELECT format('CREATE ROLE %I LOGIN PASSWORD %L', :'exporter_user', :'exporter_password')
|
|
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'exporter_user')
|
|
\gexec
|
|
|
|
GRANT pg_monitor TO :"exporter_user";
|
|
\else
|
|
DO $$ BEGIN RAISE EXCEPTION 'POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set'; END $$;
|
|
\endif
|
|
\elif :has_exporter_password
|
|
DO $$ BEGIN RAISE EXCEPTION 'POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set'; END $$;
|
|
\endif
|