PostgreSQL Permissions – Grant Permissions to Non-Existing Tables

permissionspostgresql

I would like to have a dedicated user in the schema, that can have access to tables/objects by pattern like foo* (starts with foo).

He should have permissions to create new tables like foo_table1 and perform insert/update on existing tables.

It is possible with MySQL, but not sure about PostgreSQL (easily, without stored procedures or other magic).

Is there a way to achieve this?

Best Answer

Without claiming to address your 'by pattern' requirement, granting privileges on not-yet-extant objects is addressed by Alter Default Privileges

Create schema foo;

Grant Usage On Schema foo to foouser;

Alter Default Privileges In Schema foo 
  GRANT SELECT, INSERT, UPDATE, DELETE, Truncate ON TABLES To foouser;

Alter Default Privileges In Schema foo 
  Grant EXECUTE on FUNCTIONS To foouser;

Or simply

Alter Default Privileges In Schema foo GRANT All Privileges To foouser;

As @a_horse_with_no_name observes, the schema is the usual way to group objects for privileges. Using schemas for security is much less error prone than using naming patterns, so I think we should recommend you go this way anyway!

foouser only needs to use a dot in their statements:

create table foo.mine ( id int)
select * from foo.mine

PS Why are name-patterns an error-prone way of doing security?

Because you never know whether a year later you're going to

create table food_for_top_secret_party

which matches pattern foo*, and so can be not only read, but also accidentally dropped, by foouser.