Grant privileges on future tables in PostgreSQL?

SqlDatabasePostgresql

Sql Problem Overview


I am running PostgreSQL 9.3.1. I have test database and backup user which is used to backup the database. I have no problems with granting privileges to all current tables, but I have to grant privileges each time the new table is added to schema.

createdb test

psql test
test=# create table foo();
CREATE TABLE
test=# grant all on all tables in schema public to backup;
GRANT
test=# create table bar();
CREATE TABLE

psql -U backup test
test=> select * from foo;
test=> select * from bar;
ERROR:  permission denied for relation bar

Is it possible to grant access to tables which will be created in future without making user owner of the table?

Sql Solutions


Solution 1 - Sql

It looks like the solution is to alter default privileges for backup user:

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

From the comment by Matt Schaffer:

> As caveat, the default only applies to the user that executed the > alter statement. This confused me since I was driving most of my > permissions statements from the postgres user but creating tables from > an app user. In short, you might need something like this depending on > your setup:

ALTER DEFAULT PRIVILEGES FOR USER webapp IN SCHEMA public GRANT SELECT ON SEQUENCES TO backup;
ALTER DEFAULT PRIVILEGES FOR USER webapp IN SCHEMA public GRANT SELECT ON TABLES TO backup;

Solution 2 - Sql

I am trying to create a role, grant connect access to the role and then alter default privileges to keep access for future objects. However, it seems that the below command doesn't work at role level. alter default privileges in schema public grant all on tables to backup;

I followed the below documentation but seems that there are two command do not work for roles. DOC: https://aws.amazon.com/blogs/database/managing-postgresql-users-and-roles/ First command: GRANT CONNECT ON DATABASE mydatabase TO readonly; Second command: GRANT USAGE ON SCHEMA myschema TO readonly; (For ROLES usually it needs TO ROLE, I also tried TO ROLE but still doesn't work.

Solution 3 - Sql

If you want the backup user to have access to the future tables of userN, you must run the code below under each userN who creates new tables, because ALTER DEFAULT PRIVILEGES... works only for objects by that user under whom you run ALTER DEFAULT PRIVILEGES...

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO backup;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrey ChernihView Question on Stackoverflow
Solution 1 - SqlAndrey ChernihView Answer on Stackoverflow
Solution 2 - Sqlarman nemat pasandView Answer on Stackoverflow
Solution 3 - SqlDenis DavydovView Answer on Stackoverflow