Roles and privileges
- Version
- 19
- Review
- Draft
Summary#
Roles represent users and groups. GRANT gives privileges on objects or membership in another role, and REVOKE takes a grant away.
Syntax#
CREATE ROLE group_role NOLOGIN;
GRANT group_role TO member_role [WITH INHERIT TRUE];
GRANT USAGE ON SCHEMA schema_name TO group_role;
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO group_role;
ALTER DEFAULT PRIVILEGES FOR ROLE creator_role IN SCHEMA schema_name
GRANT SELECT ON TABLES TO group_role;
REVOKE SELECT ON table_name FROM group_role;
Example#
The output below is from PostgreSQL 19.0, produced with psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -f example.sql. Use a superuser on a disposable PostgreSQL cluster because the example creates cluster-wide roles and switches roles. The final ROLLBACK removes both roles and the schema.
BEGIN;
CREATE ROLE devlookup_reader NOLOGIN;
CREATE ROLE devlookup_app NOLOGIN;
GRANT devlookup_reader TO devlookup_app WITH INHERIT TRUE;
CREATE SCHEMA devlookup_grants;
CREATE TABLE devlookup_grants.reports (
report_id integer PRIMARY KEY,
title text NOT NULL
);
INSERT INTO devlookup_grants.reports VALUES (1, 'weekly');
-- Both the schema and the table need a grant.
GRANT USAGE ON SCHEMA devlookup_grants TO devlookup_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA devlookup_grants TO devlookup_reader;
-- Cover tables that the current role creates later in this schema.
ALTER DEFAULT PRIVILEGES IN SCHEMA devlookup_grants
GRANT SELECT ON TABLES TO devlookup_reader;
CREATE TABLE devlookup_grants.drafts (draft_id integer PRIMARY KEY);
SELECT t.table_name,
has_table_privilege('devlookup_app', t.table_name, 'SELECT') AS can_select,
has_table_privilege('devlookup_app', t.table_name, 'INSERT') AS can_insert
FROM (VALUES ('devlookup_grants.drafts'),
('devlookup_grants.reports')) AS t(table_name)
ORDER BY t.table_name;
-- Run a query with the application role's privileges.
SET LOCAL ROLE devlookup_app;
SELECT current_user, title FROM devlookup_grants.reports ORDER BY report_id;
RESET ROLE;
REVOKE SELECT ON devlookup_grants.reports FROM devlookup_reader;
SELECT has_table_privilege('devlookup_app', 'devlookup_grants.reports', 'SELECT')
AS can_select_after_revoke;
ROLLBACK;
Expected output#
table_name|can_select|can_insert
devlookup_grants.drafts|t|f
devlookup_grants.reports|t|f
(2 rows)
current_user|title
devlookup_app|weekly
(1 row)
can_select_after_revoke
f
(1 row)
Common mistakes#
ERROR: permission denied for schema NAME often means that the role lacks USAGE on the schema, which it needs in addition to privileges on the objects inside. Creating objects there needs CREATE on the schema.
GRANT ... ON ALL TABLES IN SCHEMA covers existing tables only. Default privileges cover later objects only when the role named in FOR ROLE, or the current role if omitted, creates them. A REVOKE from one role also leaves access obtained through PUBLIC or another membership.
Version applicability#
ALTER DEFAULT PRIVILEGES requires PostgreSQL 9.0. PostgreSQL 15 stopped granting CREATE on the public schema to everyone in new clusters and newly created databases; upgrades and dump restores keep the existing permissions. The example's WITH INHERIT TRUE requires PostgreSQL 16. Earlier versions use the member role's INHERIT attribute.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0