Users, Roles and Privileges
Users, roles and privileges in SQL decide who can connect to a database and what each of them may do there: read a table, insert rows, change a schema, or nothing at all. A privilege is one permission, such as SELECT on payments; GRANT gives it and REVOKE takes it away. In PostgreSQL, users and groups are both roles. Getting this right limits the damage from mistakes, stolen passwords and SQL injection alike. In this lesson, we will create roles, grant and revoke privileges, build a read-only reporting role, limit access to certain columns, and apply the principle of least privilege.
In One Minute
- A role can be a user (it can log in) or a group (other roles are members of it).
CREATE USER nameisCREATE ROLE name LOGIN.GRANT SELECT ON table TO rolegives a privilege;REVOKEremoves it.- Grant privileges to group roles, then grant the group to users.
- Least privilege: every person and program gets only what it needs.
Syntax
CREATE ROLE role_name [LOGIN] [PASSWORD 'secret'];
CREATE USER user_name [PASSWORD 'secret']; -- same as CREATE ROLE ... LOGIN
GRANT privilege [, ...] ON table_name [, ...] TO role_name;
GRANT group_role TO member_role;
REVOKE privilege [, ...] ON table_name FROM role_name;
DROP ROLE role_name;| Privilege | Allows |
|---|---|
SELECT | Reading rows |
INSERT, UPDATE, DELETE | Changing rows |
TRUNCATE | Emptying the table |
REFERENCES | Creating foreign keys that point at the table |
ALL PRIVILEGES | All of the above |
USAGE (on a schema) | Seeing objects in the schema at all |
A Read-Only Reporting Role
A tutor who makes reports needs to read the Fee Tracker, never change it. Create a group role with the privileges, then a login role that belongs to it:
CREATE ROLE reporting NOLOGIN;
GRANT SELECT ON students, courses, enrollments, payments TO reporting;
CREATE ROLE report_tutor LOGIN;
GRANT reporting TO report_tutor;
SET ROLE report_tutor;
SELECT current_user;
SELECT count(*) AS payments FROM payments;
DELETE FROM payments WHERE payment_id = 7;
RESET ROLE;CREATE ROLE
GRANT
CREATE ROLE
GRANT ROLE
SET
current_user
--------------
report_tutor
(1 row)
payments
----------
7
(1 row)
ERROR: permission denied for table payments
RESETExplanation: reporting can't log in (NOLOGIN); it only holds privileges. report_tutor can log in and inherits everything reporting has. SET ROLE lets this lesson act as that user without a second connection: reading works, and the DELETE is refused with permission denied. RESET ROLE returns to the original user. In real use, the tutor would connect with their own login and password.
Checking Privileges
has_table_privilege answers "can this role do this?" without trying it:
CREATE ROLE reporting NOLOGIN;
GRANT SELECT ON students, payments TO reporting;
CREATE ROLE fee_app LOGIN;
GRANT SELECT, INSERT ON payments TO fee_app;
GRANT SELECT ON students, courses TO fee_app;
SELECT r.role_name, t.table_name,
has_table_privilege(r.role_name, t.table_name, 'SELECT') AS can_select,
has_table_privilege(r.role_name, t.table_name, 'INSERT') AS can_insert,
has_table_privilege(r.role_name, t.table_name, 'DELETE') AS can_delete
FROM (VALUES ('reporting'), ('fee_app')) AS r (role_name),
(VALUES ('students'), ('payments')) AS t (table_name)
ORDER BY r.role_name, t.table_name;CREATE ROLE
GRANT
CREATE ROLE
GRANT
GRANT
role_name | table_name | can_select | can_insert | can_delete
-----------+------------+------------+------------+------------
fee_app | payments | t | t | f
fee_app | students | t | f | f
reporting | payments | t | f | f
reporting | students | t | f | f
(4 rows)Explanation: fee_app, the role a fee-collection program connects as, can read students and payments and insert payments, but delete nothing. reporting can only read. A grid like this is a quick review of who can do what.
Revoking Privileges
CREATE ROLE fee_app LOGIN;
GRANT SELECT, INSERT, DELETE ON payments TO fee_app;
REVOKE DELETE ON payments FROM fee_app;
SET ROLE fee_app;
DELETE FROM payments WHERE payment_id = 7;
SELECT count(*) AS payments FROM payments;
RESET ROLE;CREATE ROLE
GRANT
REVOKE
SET
ERROR: permission denied for table payments
payments
----------
7
(1 row)
RESETExplanation: REVOKE DELETE removed just that privilege; SELECT and INSERT remain. Programs rarely need DELETE on financial records; corrections are usually new rows.
Column Privileges
A grant can name columns, so a role sees only the ones it needs:
CREATE ROLE tutor NOLOGIN;
GRANT SELECT (student_id, first_name, grade) ON students TO tutor;
SET ROLE tutor;
SELECT student_id, first_name, grade FROM students ORDER BY student_id;
SELECT first_name, phone FROM students ORDER BY student_id;
RESET ROLE;CREATE ROLE
GRANT
SET
student_id | first_name | grade
------------+------------+-------
101 | Srihitha | 12
102 | Advitha | 10
103 | Navika | 7
(3 rows)
ERROR: permission denied for table students
RESETExplanation: The tutor can read names and grades, but phone numbers stay private: selecting phone is refused. SELECT * would be refused too, because it includes columns the role can't read. For rules about rows, such as "each student sees only their own payments", use Row-Level Security.
Least Privilege in Practice
| Who | Role | Privileges |
|---|---|---|
| Fee-collection app | fee_app | SELECT on students and courses; SELECT, INSERT on payments |
| Reports and dashboards | reporting | SELECT only |
| Migrations | migrator | Owns the tables; used only by deployment scripts |
| People | personal logins | Membership in the roles their job needs |
Owners and superusers can do anything. The role that creates a table owns it and can always read, change or drop it; superusers bypass privilege checks entirely. Keep both away from everyday use.
Users and Roles in Other Databases
| Database | Create a login | Grant |
|---|---|---|
| PostgreSQL | CREATE ROLE name LOGIN PASSWORD '...' (or CREATE USER) | GRANT SELECT ON t TO name; |
| MySQL | CREATE USER 'name'@'host' IDENTIFIED BY '...' | GRANT SELECT ON db.t TO 'name'@'host'; |
| SQL Server | CREATE LOGIN name WITH PASSWORD = '...', then CREATE USER name FOR LOGIN name in each database | GRANT SELECT ON t TO name; and roles via ALTER ROLE ... ADD MEMBER |
| Oracle | CREATE USER name IDENTIFIED BY ... | GRANT SELECT ON t TO name; |
Tabs showing each database's version are coming to this course.
Common Mistakes
Apps connecting as a superuser. One bug or injection and everything is exposed.
Granting to individuals. Grants pile up and are never cleaned; grant to group roles.
Forgetting new tables. Grants apply to existing tables only; use ALTER DEFAULT PRIVILEGES so future tables get them too.
Storing passwords in scripts. Keep credentials in a secrets store, not in SQL files or source code.
Interview Corner
What's the difference between a user and a role in PostgreSQL?
They're the same kind of object. A role with the LOGIN attribute can connect, which is what we call a user; a role without it is typically used as a group that holds privileges.
What is the principle of least privilege?
Give each person and program only the permissions it needs for its job, so mistakes and attacks can do as little damage as possible.
What do GRANT and REVOKE do?
GRANT gives privileges on objects (or role membership) to a role; REVOKE takes them away.
Practice
Challenge: Create a group role fee_clerk that can read courses and read and insert payments, and a login role clerk_navika that's a member. As clerk_navika, record a 50.00 card payment for Advitha (102) toward SQL Basics (4), then try to update a fee.
Show solution
CREATE ROLE fee_clerk NOLOGIN;
GRANT SELECT ON courses TO fee_clerk;
GRANT SELECT, INSERT ON payments TO fee_clerk;
CREATE ROLE clerk_navika LOGIN;
GRANT fee_clerk TO clerk_navika;
SET ROLE clerk_navika;
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on, method)
VALUES (102, 4, 50.00, '2026-01-05', '2026-01-05', 'card');
UPDATE courses SET fee_usd = 1.00 WHERE course_id = 4;
RESET ROLE;CREATE ROLE
GRANT
GRANT
CREATE ROLE
GRANT ROLE
SET
INSERT 0 1
ERROR: permission denied for table courses
RESETThe insert works because fee_clerk has INSERT on payments. (Inserting into a table with a foreign key also checks the referenced table, which PostgreSQL does with the owner's rights, so no extra grant is needed.) Changing a fee is refused: the clerk can read courses but not update them.
Key Takeaways
| You want to | Write |
|---|---|
| A group role | CREATE ROLE reporting NOLOGIN; |
| A login | CREATE ROLE name LOGIN PASSWORD '...'; |
| Give read access | GRANT SELECT ON payments TO reporting; |
| Add a member | GRANT reporting TO name; |
| Take access away | REVOKE DELETE ON payments FROM fee_app; |
Was this tutorial helpful?