SQL · Module 14 · Lesson 11 min read

Roles, Users & Permissions

Roles

In PostgreSQL, users and groups are both roles.

  • A role that can log in = a "user": CREATE ROLE analyst LOGIN PASSWORD '…';
  • A role without login = a "group": CREATE ROLE readonly; then GRANT readonly TO analyst;
  • Attributes: SUPERUSER, CREATEDB, CREATEROLE, LOGIN, BYPASSRLS

Privileges: GRANT and REVOKE

  • Tables: SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER
  • Column-level: GRANT SELECT (id, name) ON users TO support;
  • Schemas: USAGE (access objects in it), CREATE
  • Functions: EXECUTE; sequences: USAGE, SELECT
vbnet
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
REVOKE DELETE ON expenses FROM app_user;

Default privileges for future tables

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;

Principle of least privilege ⭐

Each role gets only what it needs:

RoleCan do
app_user (your backend)SELECT/INSERT/UPDATE/DELETE on app tables — no DDL
readonly (analytics, BI tools)SELECT only
migrator (deployments)DDL
postgres superuseremergencies only; never used by the app

Supabase roles

  • anon – requests without a logged-in user (public API key)
  • authenticated – logged-in users
  • service_role – full access, bypasses RLS → ⚠️ server-side only, never in the browser
  • postgres – the owner/admin Tables are protected by Row Level Security policies (lesson 14.3) on top of these roles.

Checking permissions

\dp table in psql, has_table_privilege('role', 'table', 'SELECT'), information_schema.role_table_grants

Example

SQL
-- Group roles (no login) describing access levels
CREATE ROLE readonly_group NOLOGIN;
CREATE ROLE app_group NOLOGIN;

GRANT USAGE ON SCHEMA public TO readonly_group, app_group;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_group;
GRANT SELECT, INSERT, UPDATE, DELETE ON expenses, bills, accounts TO app_group;
GRANT SELECT ON users, categories TO app_group;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_group;

-- Future tables get the same read access automatically
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_group;

-- Login roles inherit from groups
CREATE ROLE report_bot LOGIN PASSWORD 'change-me-123' IN ROLE readonly_group;
CREATE ROLE api_server LOGIN PASSWORD 'change-me-456' IN ROLE app_group;

-- Column-level privilege: support staff can't see emails
CREATE ROLE support NOLOGIN;
GRANT SELECT (id, name, city) ON users TO support;

-- Check what each role can do
SELECT has_table_privilege('report_bot', 'expenses', 'SELECT') AS bot_can_read,     -- true
       has_table_privilege('report_bot', 'expenses', 'DELETE') AS bot_can_delete,   -- false
       has_table_privilege('api_server', 'users', 'UPDATE')    AS api_can_edit_users; -- false

-- Try it: act as the report bot
SET ROLE report_bot;
SELECT count(*) FROM expenses;               -- ✅ 31
-- DELETE FROM expenses;                     -- ❌ permission denied for table expenses
RESET ROLE;

SET ROLE support;
SELECT name, city FROM users LIMIT 2;        -- ✅
-- SELECT email FROM users;                  -- ❌ permission denied for table users
RESET ROLE;

-- Take a permission away
REVOKE DELETE ON bills FROM app_group;