Full answer
A PostgreSQL example:
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
CREATE ROLE reporting_demo NOLOGIN;
GRANT USAGE ON SCHEMA ib TO reporting_demo;
GRANT SELECT ON orders TO reporting_demo;
SELECT privilege_type FROM information_schema.role_table_grants
WHERE grantee = 'reporting_demo' ORDER BY privilege_type;
REVOKE ALL ON orders FROM reporting_demo;
REVOKE USAGE ON SCHEMA ib FROM reporting_demo;
DROP ROLE reporting_demo;| privilege_type |
|---|
| SELECT |
The reporting role can read orders but cannot change them, and it can read nothing else. SQLite has no users or GRANT; access control there is the file system's permissions plus whatever the application enforces.
A common layout:
app_migrator: owns the schema, runs DDL during deployments only.app_rw: SELECT, INSERT, UPDATE, DELETE on application tables, no DDL.app_ro: SELECT only, for reports and analysts, often on a replica.
Grant to roles, then grant roles to login users, so access changes in one place. ALTER DEFAULT PRIVILEGES covers tables created later. Views and row-level security can restrict columns and rows further.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- GRANT gives privileges; REVOKE removes them
- Least privilege: only what the role needs
- Separate migration, read-write and read-only roles
- SQLite relies on file permissions instead
Common mistakes
- Running the application as the database owner or superuser
- Granting to individual users instead of roles
Follow-ups
- What is row-level security?
- How would you give analysts access without personal data?
Tips
- Describe a three-role layout