PostgreSQL Permissions – Replacing Function Body by Users from Same Group

functionspermissionspostgresqlreplace

In the documentation of CREATE FUNCTION is stated that ''You must own the function to replace it (this includes being a member of the owning role).''

Two questions:

  1. I don't understand what exactly the ''this includes being a member of the owning role'' part means.

  2. Is it possible to have multiple users (non superusers) that can modify the body of the same set of functions? I have a user which creates the functions, tables etc (Liquibase user) and I want to add a debug user which should have the permission to modify functions created by the first user in order to test some assumptions or hotfixes?

Best Answer

Consider this example setup:

CREATE ROLE function_creator;

CREATE USER radu;
CREATE USER ion;

GRANT function_creator TO radu, ion; -- this is 'being a member of the owning role'

GRANT ALL ON SCHEMA here_live_the_functions TO function_creator;

Then, when you create functions:

SET ROLE TO function_creator;

CREATE OR REPLACE FUNCTION ...;

After all this, you and ion will be able to change (redefine) the function, which is owned by function_creator.

If you need the debug user, you can add one more level:

CREATE ROLE debugger;

GRANT function_creator TO debugger;

-- and instead of granting function_creator to the actual users, do 
GRANT debugger TO radu, ion;