Roles and Privileges
Role and privilege commands are used to create roles, grant permissions to users or roles, and view or revoke existing grants.
In This Chapter
Common Operations
Create a Role and Grant Privileges
-- Create a role
CREATE ROLE IF NOT EXISTS data_analyst;
-- Grant table privileges to the role
GRANT SELECT ON TABLE public.orders TO ROLE data_analyst;
GRANT SELECT ON TABLE public.customers TO ROLE data_analyst;
-- Grant privileges on all tables in a schema to the role
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ROLE data_analyst;
Grant a Role to a User
-- Assign a role to a user
GRANT ROLE data_analyst TO USER alice;
-- Grant privileges directly to a user (not recommended; prefer role-based management)
GRANT SELECT ON TABLE public.orders TO USER bob;
View Privileges
-- View privileges held by a user
SHOW GRANTS TO USER alice;
-- View privileges held by a role
SHOW GRANTS TO ROLE data_analyst;
-- List all roles
SHOW ROLES;
Revoke Privileges
-- Revoke a role from a user
REVOKE ROLE data_analyst FROM USER alice;
-- Revoke a table privilege from a role
REVOKE SELECT ON TABLE public.orders FROM ROLE data_analyst;