Grant permissions to user for any new tables created in postgresql

后端 未结 3 876
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 17:28

Currently I am using this to grant permissions:

grant select on all tables in schema public to ;

alter default privileges in schema public          


        
3条回答
  •  感动是毒
    2020-12-09 18:07

    I was looking for same thing, I found other way to solve this. Based on postgresql documentation we can create event trigger, so when new table is created, grant query will execute automatically. So no matter who created new table, other user allowed to use it.

    CREATE OR REPLACE FUNCTION auto_grant_func()
    RETURNS event_trigger AS $$
    BEGIN
        grant all on all tables in schema public to ;
        grant all on all sequences in schema public to ;
        grant select on all tables in schema public to ;
        grant select on all sequences in schema public to ;
    END;
    $$ LANGUAGE plpgsql;
    
    CREATE EVENT TRIGGER auto_grant_trigger
        ON ddl_command_end
        WHEN TAG IN ('CREATE TABLE', 'CREATE TABLE AS')
    EXECUTE PROCEDURE auto_grant_func();
    

提交回复
热议问题