Example usage: implementing inherited privileges
The following steps show how user Joe enables inheritance of privileges on a given schema so other users can access tables in that schema.
The following steps show how user Joe
enables inheritance of privileges on a given schema so other users can access tables in that schema.
-
Joe
creates schemaschema1
, and creates tabletable1
in it:=>\c - Joe You are now connected as user Joe => CREATE SCHEMA schema1; CRDEATE SCHEMA => CREATE TABLE schema1.table1 (id int); CREATE TABLE
-
Joe
grants USAGE and CREATE privileges onschema1
toMyra
:=> GRANT USAGE, CREATE ON SCHEMA schema1 to Myra; GRANT PRIVILEGE
-
Myra
queriesschema1.table1
, but the query fails:=>\c - Myra You are now connected as user Myra => SELECT * FROM schema1.table1; ERROR 4367: Permission denied for relation table1
-
Joe
grantsMyra
SELECT ON SCHEMA
privileges onschema1
:=>\c - Joe You are now connected as user Joe => GRANT SELECT ON SCHEMA schema1 to Myra; GRANT PRIVILEGE
-
Joe
usesALTER TABLE
to include SCHEMA privileges fortable1
:=> ALTER TABLE schema1.table1 INCLUDE SCHEMA PRIVILEGES; ALTER TABLE
-
Myra
's query now succeeds:=>\c - Myra You are now connected as user Myra => SELECT * FROM schema1.table1; id --- (0 rows)
-
Joe
modifiesschema1
to include privileges so all tables created inschema1
inherit schema privileges:=>\c - Joe You are now connected as user Joe => ALTER SCHEMA schema1 DEFAULT INCLUDE PRIVILEGES; ALTER SCHEMA => CREATE TABLE schema1.table2 (id int); CREATE TABLE
-
With inherited privileges enabled,
Myra
can querytable2
withoutJoe
having to explicitly grant privileges on the table:=>\c - Myra You are now connected as user Myra => SELECT * FROM schema1.table2; id --- (0 rows)