12

As discussed in the question at GRANT SELECT to all tables in postgresql, as of PG 9.0 you can mass-grant privileges on all existing tables to user u, using a command like:

GRANT ALL ON ALL TABLES IN SCHEMA public TO u;

Logged in as u, you can now do this to pre-existing table a:

SELECT * FROM a;

But if you now create table b and do:

SELECT * FROM b;

You get:

ERROR: permission denied for relation b
SQL state: 42501

This can be remedied by re-executing

GRANT ALL ON ALL TABLES IN SCHEMA public TO u;

But it's a problem to have to remember to do this after each time you create a table.

Is there a way to get PostgreSQL to apply these global grants automatically to newly created tables?

~ Thanks in advance ~ Ken

Ken
  • 123
  • 1
  • 1
  • 4

1 Answers1

17

A posible solution is to alter default privileges for u user:

Eg:

alter default privileges in schema public grant all on tables to u;
alter default privileges in schema public grant all on sequences to u;

Description

ALTER DEFAULT PRIVILEGES allows you to set the privileges that will be applied to objects created in the future. (It does not affect privileges assigned to already-existing objects.) Currently, only the privileges for tables (including views), sequences, and functions can be altered.

You can change default privileges only for objects that will be created by yourself or by roles that you are a member of. The privileges can be set globally (i.e., for all objects created in the current database), or just for objects created in specified schemas. Default privileges that are specified per-schema are added to whatever the global default privileges are for the particular object type.

As explained under GRANT, the default privileges for any object type normally grant all grantable permissions to the object owner, and may grant some privileges to PUBLIC as well. However, this behavior can be changed by altering the global default privileges with ALTER DEFAULT PRIVILEGES.

see: ALTER DEFAULT PRIVILEGES

Federico Sierra
  • 3,499
  • 1
  • 18
  • 24