0

When my postgres function is tries to create a new sequence, it returns a permission denied error. It looks like the only way to make it work is to give Create permission on schema using the below statement.

GRANT CREATE ON SCHEMA public to "myuser"

But, this will allow the user to create any other type of object as well, including tables. How can we control this? I want my user to be able to create a sequence, but not tables.

Note: Create is not a valid grant on Sequences.

Brendan McCaffrey
3,4542 gold badges8 silver badges29 bronze badges
asked Mar 11, 2022 at 11:37

3 Answers 3

1

From this answer, an example of how to do this with an event trigger is

CREATE OR REPLACE FUNCTION check_ddl_event_user()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
BEGIN
 IF (SELECT current_user) = 'USER_TO_BLOCK' AND tg_tag NOT = 'CREATE SEQUENCE' THEN
 RAISE EXCEPTION 'USER_TO_BLOCK tried to access a forbidden resource, the action was %', tg_tag;
 END IF;
END;
$$;
CREATE EVENT TRIGGER track_ddl_event
 ON ddl_command_start
 EXECUTE FUNCTION check_ddl_event_user();

Note that the trigger used is ddl_command_start which fires on a lot of events. The full list can be found here

answered Mar 17, 2023 at 16:01
0

There is no way to do this using permissions. The only option would be to create an event trigger that throws an exception if an undesirable object is created.

answered Mar 11, 2022 at 11:59
0

Make the fuction have security definer then the actions taken by the fuction will be applied with the permissions of the creator of the function.

https://www.postgresql.org/docs/current/sql-createfunction.html

answered Aug 3, 2023 at 3:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.