Roles & Privileges

What is a Role?

A role is a named group of privileges. Instead of granting ten privileges to fifty users one by one, you grant them to a role and add users to it. When someone changes team, you change their role, not fifty grants.

Example

-- PostgreSQL / MySQL 8 / SQL Server (syntax very similar)
CREATE ROLE readonly;
GRANT SELECT ON Customers, Orders, Products TO readonly;

CREATE ROLE support;
GRANT SELECT, UPDATE ON Orders TO support;

-- give roles to users
GRANT readonly TO priya;      -- MySQL: GRANT readonly TO 'priya'@'%';
GRANT support  TO david;

Principle of Least Privilege

  • Give each user or application only the privileges it needs.
  • Applications should never connect as the database owner or root/sa.
  • Use separate accounts for the app, for reporting and for migrations.
  • Review grants regularly and revoke what is no longer used.

Tip: This is covered in more depth in the Advance level, SQL Security module.