Users, Roles & Privilege Management (GRANT, REVOKE)
Manage least-privilege access with roles, GRANT, and REVOKE.
In plain words
Roles are groups of permissions. Give a group only what it needs, then put people or apps in the group.
Think of it like…
Office key cards: rather than programming every door for every employee, you create groups (finance, engineering, visitors), give each group access to certain doors, and put people in groups.
Where you’ve seen it
A startup's database has three roles: app_rw (read and write data, no DDL) for the API, migrator (owns the schema, runs migrations), and analyst_ro (SELECT only, for the BI tool). When an analyst's password leaks, the attacker can read data but can't change or delete anything.
Words you’ll hear
- Role
- A named set of privileges; with LOGIN it acts as a user.
- GRANT / REVOKE
- Give or remove privileges on objects, or membership in roles.
- Default privileges
- ALTER DEFAULT PRIVILEGES: privileges automatically applied to tables created in the future.
- PUBLIC
- A pseudo-role meaning everyone; revoke what it doesn't need.
Visual diagram
Theory & example queries
Roles, membership and grants
Create group roles without LOGIN for each kind of access, grant them privileges, and grant the group to login roles: CREATE ROLE analyst_ro; GRANT analyst_ro TO asha;. Members inherit the group's privileges. Revoking membership removes all of them at once, which keeps access reviews manageable.
Access needs every level: CONNECT on the database, USAGE on the schema, then the table privilege. GRANT SELECT ON ALL TABLES IN SCHEMA app TO analyst_ro covers existing tables only; add ALTER DEFAULT PRIVILEGES IN SCHEMA app GRANT SELECT ON TABLES TO analyst_ro (run as the role that creates tables) so new tables are covered too. You can also grant on specific columns: GRANT SELECT (id, name, city) ON customers TO support.
CREATE ROLE analyst_ro;GRANT CONNECT ON DATABASE shop TO analyst_ro;GRANT USAGE ON SCHEMA app TO analyst_ro;GRANTSELECT ON ALL TABLES IN SCHEMA app TO analyst_ro;ALTER DEFAULT PRIVILEGES FOR ROLE migrator IN SCHEMA appGRANTSELECT ON TABLES TO analyst_ro;CREATE ROLE asha LOGIN PASSWORD '…' IN ROLE analyst_ro;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative role setup status
Note: PostgreSQL-style command tags. The password value is intentionally omitted, and existing database/schema names must be available.
Key takeaway: Grant to group roles at every level, and set default privileges for future tables.
Least privilege in practice
Separate duties: a migration role owns the schema and runs DDL; the application role gets only the DML it needs on its tables; read-only roles serve analytics and support. The application should never connect as a superuser or as the table owner, because owners can drop their tables regardless of grants.
Clean up defaults: in PostgreSQL before version 15, every role could create objects in the public schema; REVOKE CREATE ON SCHEMA public FROM PUBLIC closes that. For per-row restrictions, such as a tenant seeing only its own rows, add row-level security policies on top of grants. In MySQL, users are 'name'@'host' accounts, and roles exist from version 8.0.
CREATE ROLE app_rw;GRANT USAGE ON SCHEMA app TO app_rw;GRANTSELECT, INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA app TO app_rw;GRANT USAGE ON ALL SEQUENCES IN SCHEMA app TO app_rw;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative application grants
Note: Assumes the app schema and its referenced tables and sequences exist; command wording can differ by PostgreSQL client.
Key takeaway: Separate owner, application and read-only roles; never run the app as owner or superuser.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
List who needs what
Identify each kind of access: application read-write, migrations, analysts, support, backups.
app_rw · migrator · analyst_ro
When to use it & trade-offs
What it is
Databases control access with roles (in PostgreSQL, a user is simply a role that can log in) and privileges granted on objects: SELECT, INSERT, UPDATE and DELETE on tables, USAGE on schemas, EXECUTE on functions. GRANT gives a privilege, REVOKE removes it, and roles can be members of other roles, so you grant to a group role once and add people to it.
Why it matters: Least privilege limits the damage from a leaked password, a buggy script or an SQL injection: an application role that can't DROP tables can't drop them. Setting up read-only analyst access and separate application and migration roles is everyday work, and a common security interview question.
When to reach for it
- Read-only access for analysts and BI tools
- Separate application and migration roles
- Limiting damage from leaked credentials or SQL injection
When to avoid or be careful
- Applications connecting as superuser or as the table owner
- Granting privileges to individual users instead of groups
- Forgetting default privileges, so new tables are invisible to read-only roles
Group roles vs per-user grants vs row-level security
Strengths: Grant once, add or remove members; easy audits.
Watch out: Needs a little upfront design.
Strengths: Quick.
Watch out: Becomes unmanageable and hard to review.
Strengths: Enforced by the database on every query.
Watch out: More complex; must be tested carefully.
Interview signals & numbers to know
When this topic comes up
- “Give the analytics team read-only access”
- “permission denied for table …”
- “How would you limit what the app can do?”
- Least privilege
- Security review
Benchmarks & rules of thumb
- 3
- levels to grant: database, schema, table
- PostgreSQL 15
- PUBLIC no longer gets CREATE on the public schema
- MySQL 8.0
- added roles
Common mistakes & follow-ups
Common interview mistakes
- Granting SELECT on all tables but forgetting USAGE on the schema, so every query fails with permission denied.
- Granting on existing tables only, so tables created next week are invisible to the analyst role.
- Running the application as the owner of its tables, which can drop them whatever the grants say.
Likely interviewer follow-ups
- How would you set up read-only access that also covers tables created later?
- Why shouldn't the application connect as the table owner?
- How would you restrict each tenant to its own rows?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Users, Roles & Privilege Management (GRANT, REVOKE) with hands-on SQL practice and interview Q&A.