Postgres: granting access to a role/user for future tables created by a different role/user

我只是一个虾纸丫 提交于 2021-02-17 06:13:20

问题


I'm building a spring boot application. Flyway database migrations are executed at the application startup.

I decided to use two different roles: role__app (read/write rights on tables, sequences in app schema) and role__migration (advanced rights in app/migration schemas).

Flyway migrations are executed under role__migration so it becomes the owner of the created objects. I thought that the following statements would help:

ALTER DEFAULT PRIVILEGES IN SCHEMA app GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO role__app;
ALTER DEFAULT PRIVILEGES IN SCHEMA app GRANT USAGE ON SEQUENCES TO role__app;

But when the new tables are added to the app schema the user__app (belongs to the role__app) doesn't have access to the tables.

Is it possible to maintain such a flow (with app, migrattion users/roles) by Postgres or by any other means?


As a side note I should mention that I run the following statements on the target database:

REVOKE CREATE ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON DATABASE myDb FROM PUBLIC;

Update 1

I added the FOR ROLE clause, yet I'm still getting the permission denied message for a created table (app.property) in app schema for user user__app. The owner of the table is user__mig.

Update 2

After logging in as postgres user in dbeaver we can see that user__mig has all necessary permissions ticked whereas the user__app has no permissions on the app.property table at all:

enter image description here


Here is a gist to reproduce the problem: https://gist.github.com/happygrizzly/849a6a791f028ba5b191f73180ae35d1


回答1:


You should write

ALTER DEFAULT PRIVILEGES FOR USER role__migration ...

If you omit the FOR USER clause, the privileges are only granted on objects created by the user who ran ALTER DEFAULT PRIVILEGES.

With the above statement, the privileges are granted when role__migration creates an object. That does not extend to members of the role role__migration.



来源:https://stackoverflow.com/questions/58219201/postgres-granting-access-to-a-role-user-for-future-tables-created-by-a-differen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!