@@ -250,13 +250,13 @@ ${opt.schema !== 'public' ? `\
250250DO
251251$$
252252BEGIN
253+ -- Roles are shared across databases, so we have remove previously configured privileges.
254+ -- This might of course interfere with other programs running on the DBMS!
255+ -- todo: find a cleaner solution
253256 IF EXISTS (
254257 SELECT FROM pg_catalog.pg_roles
255258 WHERE rolname = 'web_anon'
256259 ) THEN
257- -- Roles are shared across databases, so we have remove previously configured privileges.
258- -- This might of course interfere with other programs running on the DBMS!
259- -- todo: find a cleaner solution
260260 RAISE WARNING 'Role web_anon already exists. Reassigning owned DB objects to current_user().';
261261 REASSIGN OWNED BY web_anon TO SESSION_USER;
262262 -- REVOKE ALL PRIVILEGES ON DATABASE current_database() FROM web_anon;
@@ -271,6 +271,24 @@ BEGIN
271271 RAISE NOTICE 'Role web_anon was just created by a concurrent transaction.';
272272 END;
273273 END IF;
274+ IF EXISTS (
275+ SELECT FROM pg_catalog.pg_roles
276+ WHERE rolname = 'postgrest'
277+ ) THEN
278+ RAISE WARNING 'Role postgrest already exists. Reassigning owned DB objects to current_user().';
279+ REASSIGN OWNED BY postgrest TO SESSION_USER;
280+ -- REVOKE ALL PRIVILEGES ON DATABASE current_database() FROM postgrest;
281+ -- REVOKE ALL PRIVILEGES ON SCHEMA "${ opt . schema } " FROM postgrest;
282+ -- REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA "${ opt . schema } " FROM postgrest;
283+ -- REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA "${ opt . schema } " FROM postgrest;
284+ ELSE
285+ BEGIN
286+ CREATE ROLE postgrest LOGIN NOINHERIT NOCREATEDB NOCREATEROLE NOSUPERUSER PASSWORD '${ postgrestPassword } ';
287+ EXCEPTION
288+ WHEN duplicate_object THEN
289+ RAISE NOTICE 'Role postgrest was just created by a concurrent transaction.';
290+ END;
291+ END IF;
274292END
275293$$;
276294
@@ -283,7 +301,6 @@ GRANT SELECT ON ALL TABLES IN SCHEMA "${opt.schema}" TO web_anon;
283301GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA "${ opt . schema } " TO web_anon;
284302GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "${ opt . schema } " TO web_anon;
285303
286- CREATE ROLE postgrest LOGIN NOINHERIT NOCREATEDB NOCREATEROLE NOSUPERUSER PASSWORD '${ postgrestPassword } ';
287304GRANT web_anon TO postgrest;
288305
289306COMMENT ON SCHEMA "${ opt . schema } " IS
0 commit comments