Users, Roles and Privileges

Updated · 9 min read
mediumPostgreSQL 18sqlrolesgrantrevokeprivilegesleast-privilegeinterview

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 name is CREATE ROLE name LOGIN.
  • GRANT SELECT ON table TO role gives a privilege; REVOKE removes it.
  • Grant privileges to group roles, then grant the group to users.
  • Least privilege: every person and program gets only what it needs.

Syntax

SQL
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;
PrivilegeAllows
SELECTReading rows
INSERT, UPDATE, DELETEChanging rows
TRUNCATEEmptying the table
REFERENCESCreating foreign keys that point at the table
ALL PRIVILEGESAll 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:

SQL▶ Try 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;
Output
CREATE ROLE
 
GRANT
 
CREATE ROLE
 
GRANT ROLE
 
SET
 
 current_user
--------------
 report_tutor
(1 row)
 
 payments
----------
        7
(1 row)
 
ERROR:  permission denied for table payments
 
RESET

Explanation: 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.

Privileges are granted to the reporting group; report_tutor inherits them by membership

Checking Privileges

has_table_privilege answers "can this role do this?" without trying it:

SQL▶ Try 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;
Output
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.

SELECT, INSERT and DELETE for two roles on two tables, from has_table_privilege

Revoking Privileges

SQL▶ Try it
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;
Output
CREATE ROLE
 
GRANT
 
REVOKE
 
SET
 
ERROR:  permission denied for table payments
 
 payments
----------
        7
(1 row)
 
RESET

Explanation: 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:

SQL▶ Try it
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;
Output
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
 
RESET

Explanation: 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

WhoRolePrivileges
Fee-collection appfee_appSELECT on students and courses; SELECT, INSERT on payments
Reports and dashboardsreportingSELECT only
MigrationsmigratorOwns the tables; used only by deployment scripts
Peoplepersonal loginsMembership 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

DatabaseCreate a loginGrant
PostgreSQLCREATE ROLE name LOGIN PASSWORD '...' (or CREATE USER)GRANT SELECT ON t TO name;
MySQLCREATE USER 'name'@'host' IDENTIFIED BY '...'GRANT SELECT ON db.t TO 'name'@'host';
SQL ServerCREATE LOGIN name WITH PASSWORD = '...', then CREATE USER name FOR LOGIN name in each databaseGRANT SELECT ON t TO name; and roles via ALTER ROLE ... ADD MEMBER
OracleCREATE USER name IDENTIFIED BY ...GRANT SELECT ON t TO name;

Tabs showing each database's version are coming to this course.

A Key for Every Door

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
SQL▶ Try it
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;
Output
CREATE ROLE
 
GRANT
 
GRANT
 
CREATE ROLE
 
GRANT ROLE
 
SET
 
INSERT 0 1
 
ERROR:  permission denied for table courses
 
RESET

The 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.

Quick check: Users, roles and privileges

3 questions
1What is CREATE USER in PostgreSQL?
2A role has SELECT on two of a table's columns. What does SELECT * do?
3Why grant privileges to group roles?

Key Takeaways

You want toWrite
A group roleCREATE ROLE reporting NOLOGIN;
A loginCREATE ROLE name LOGIN PASSWORD '...';
Give read accessGRANT SELECT ON payments TO reporting;
Add a memberGRANT reporting TO name;
Take access awayREVOKE DELETE ON payments FROM fee_app;

Was this tutorial helpful?

Lesson 93 of 115