How to make permissions to a user on all new table

2019-07-18 14:49发布

问题:

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