How to make permissions to a user on all new tables in Postgres?

好久不见. 提交于 2019-12-24 00:45:01

问题


Supposing, I have two users in Postgres 9.6 named user1 and user2. I want to make permissions to user2 on any new table created by user1 without extra explicit grant commands for the created table. In other words, I want to make the following commands work correctly:

  1. Giving permissions to user2 (I don't know what should be here!)
  2. CREATE TABLE t1 (id int) (under user1)
  3. INSERT INTO t1 VALUES (5) (under user2)

Please, notice, I am not supposed to make any permissions between step 2 and 3.

On step 3 I always have the error:

permission denied for relation t1

I have tried:

GRANT ALL PRIVILEGES ON DATABASE test TO user2;

and

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user2;

without any success.

I also tried:

GRANT user1 TO user2

and it helped. But I can't consider it as the solution because user1 may have too high permissions (for example, it can be postgres) that I don't want to share with user2.


回答1:


You can do this by creating default privileges:

ALTER DEFAULT PRIVILEGES
FOR USER user1
GRANT ALL ON TABLES TO user2


来源:https://stackoverflow.com/questions/51169896/how-to-make-permissions-to-a-user-on-all-new-tables-in-postgres

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