Roles & Permissions — Complete Guide
Roles & Permissions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 71 of 100
Roles & Permissions
SQL ✓ → Advanced
Advanced · 2 — Production · ~10 min · PostgreSQL — Security & Cloud
What is this?
Roles are login users or groups — GRANT controls database, schema, table, and column privileges. REVOKE removes them. PUBLIC is default grantee to avoid.
Why should you care?
PostgresVerse interns get read-only on staging schemas, not SUPERUSER — RBAC prevents DROP DATABASE accidents.
See it live — copy this example
Run in pgAdmin or psql.
CREATE ROLE analyst LOGIN PASSWORD '***';
GRANT CONNECT ON DATABASE "PostgresVerse" TO analyst;
GRANT USAGE ON SCHEMA public TO analyst;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO analyst;
What happened?
- analyst connects and reads public tables.
- DEFAULT PRIVILEGES auto-grants SELECT on future tables — critical for migrations adding tables monthly.
Practice next
- CREATE ROLE analyst and GRANT as shown.
- Login as analyst in psql -U analyst -d PostgresVerse.
- Try SELECT ok; INSERT denied.
- Column-level GRANT SELECT (email) ON customers.
- CREATE ROLE readwrite IN ROLE analyst GRANT INSERT,UPDATE.
Remember
Roles can inherit via GRANT role TO role. Least privilege per service account. REVOKE when offboarding contractors.
PostgresVerse RBAC rollout
Each microservice gets unique role with 3-table GRANT set; vault stores passwords.
Outcome: Blast radius of leaked creds limited to one domain.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!