Sub topic 14
Designing a least-privilege setup
Assume you know basic SQL, can connect to PostgreSQL with psql, and understand that a server contains databases, which contain schemas and tables. This chapter combines those pieces into a role design that gives each account only the access it needs.
Start with separate responsibilities
A least-privilege setup separates ownership, human login, and application access:
| Role | Login | Purpose |
|---|---|---|
| `app_owner` | No | Owns schema objects and manages grants |
| `app_readonly` | No | Group role for read-only access |
| `app_writer` | No | Group role for read-write access |
| `alice` | Yes | Human or service account that receives group membership |
A NOLOGIN role is useful as a group. A login role should normally inherit permissions from one or more group roles rather than receive many direct grants.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
GRANT app_readonly TO alice;The owner creates objects, while users receive privileges on those objects. Ownership is stronger than a normal grant: the owner can usually alter or drop the object and change its privileges. Therefore, application accounts should not own production tables.
Grant access in layers
A user typically needs CONNECT on the database, USAGE on the schema, and object privileges such as SELECT or INSERT. These permissions are separate.
REVOKE ALL ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
GRANT USAGE ON SCHEMA shop TO app_readonly, app_writer;
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA shop TO app_writer;PUBLIC means every role, including roles created later. Revoke unwanted default access before adding the narrow access that the application needs. Avoid granting ALL unless the role truly needs every available privilege.
Cover objects created later
A grant on all existing tables does not affect future tables. Default privileges solve that gap, but they apply to objects created by the specified owner.
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT ON TABLES TO app_readonly;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT, INSERT, UPDATE ON TABLES TO app_writer;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT USAGE ON SEQUENCES TO app_writer;The sequence grant matters when identity or serial columns generate new IDs. Functions also need explicit EXECUTE privileges when the application calls them.
Check the effective design
Test the permission that a role actually has instead of inferring it from one GRANT statement.
SELECT has_table_privilege('alice', 'shop.orders', 'SELECT') AS can_select;
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_schema = 'shop' AND table_name = 'orders'
ORDER BY grantee, privilege_type;Membership inheritance can change results. PostgreSQL 16 and later can control inheritance and SET ROLE per membership. If rows must be restricted by user, use row-level security in addition to grants. Grants decide which table operations are possible; row-level security decides which rows are visible or changeable.
Exercise
Exercise
Why should an application login usually be a member of app_writer instead of owning the tables?
Show answer
Group membership limits the login to granted operations, while ownership also permits changing or dropping the objects and their privileges.