Users, Roles and Privileges (GRANT, REVOKE)
Part of the SQL Roadmap
In short
If every person and app logs in as the admin, one mistake or one leaked password can wipe everything. Roles and privileges give each one exactly the access it needs, and no more.
Databases control access with users, roles and privileges. GRANT gives rights such as SELECT or INSERT on tables, and REVOKE takes them away. Giving each person or app only the rights it needs limits damage from mistakes and attacks.

Think of it like this
Hotel key cards
At a hotel in Goa, a guest's card opens one room. A housekeeping card opens every room on one floor. The manager's card opens the store room too. Nobody gets a master key just because it's easier. Roles are the card types, privileges are the doors each card opens, and GRANT and REVOKE are the front desk programming and cancelling cards.
Watch it happen
Who can do what in the sales schema
1/5Everything lives in the sales schema. By default, a new user can't read any of its tables.
The big ideas
What you need to know
Grant to roles, not people
Give privileges to a role like analyst, then add people to the role. Joining or leaving becomes one line.
Least privilege
Each account gets only what it needs. A reporting user never needs DELETE.
REVOKE takes it back
REVOKE SELECT ON orders FROM analyst, or REVOKE analyst FROM sneha, removes access immediately for new statements.
Schemas need USAGE
In PostgreSQL, table rights aren't enough. The role also needs USAGE on the schema to see the tables in it.
See it in code
Walk through the code
1-- A group role: a bundle of rights, no login2CREATE ROLE analyst NOLOGIN;3GRANT USAGE ON SCHEMA sales TO analyst;4GRANT SELECT ON ALL TABLES IN SCHEMA sales TO analyst;5ALTER DEFAULT PRIVILEGES IN SCHEMA sales6 GRANT SELECT ON TABLES TO analyst;7 8-- A login user that inherits the analyst rights9CREATE ROLE sneha LOGIN; -- set her password with \password sneha in psql10GRANT analyst TO sneha;11 12-- The order app may read and write orders, nothing else13CREATE ROLE order_app LOGIN;14GRANT USAGE ON SCHEMA sales TO order_app;15GRANT SELECT, INSERT, UPDATE ON sales.orders TO order_app;16 17-- Sneha moves teams: remove the access18REVOKE analyst FROM sneha;1/5The analyst role can enter the schema and read every table that exists today.
PostgreSQL syntax, where users and groups are both roles. In MySQL: CREATE USER 'sneha'@'%' IDENTIFIED BY ...; GRANT SELECT ON sales.* TO 'sneha'@'%'; MySQL 8 also supports CREATE ROLE.
Save yourself the pain
Common mistakes
Oops
Letting the web app connect as the postgres superuser.
Do this instead
Create a dedicated app role with only the table rights it needs. Keep the superuser for admin work.
Oops
Granting privileges to each person one table at a time.
Do this instead
Grant to a role once and add people to it. It's easier to audit and to revoke.
Oops
Writing real passwords in SQL scripts that go into Git.
Do this instead
Set passwords interactively or load them from a secrets manager, never in committed files.
Your mission
Lock down a practice database
Quick check
Why grant privileges to a role like analyst instead of directly to each person?
Go deeper
- 1Create a read-only role that can only SELECT from reporting tables.
- 2Log in as that user and confirm INSERT and DELETE are refused.
- 3Write down which roles a small app needs, such as app_writer, analyst_reader and admin.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.