17 / 19 · Operating a Data Model
Roles and permissions
Give a reporting job access to order amounts, trace where its permission comes from and test what the job can read.
From a domain to a data model · Common table expressions and views
On this page 4
A reporting job needs the total value of two orders. The order table also contains a customer email for each row. The job only needs the amount and order identifier. If it connects as the table’s owner, its authority includes changing the table and reading every column. One connection identity gives the program more power than the task calls for.
Give a job an identity and a role#
Authentication establishes who connects. Authorization decides whether that identity may perform an operation on a database object. In PostgreSQL, a role can represent an identity that logs in, a group that holds permissions, or both. A privilege allows an operation such as SELECT on a relation or USAGE on a schema. Ownership carries additional authority, including the right to change or drop the object. PostgreSQL 18, §5.8 and Chapter 21
The report can use a dedicated login. Its privileges come from membership in a role that represents reporting work. A second reporting job can use its own login and receive the same membership. The mapping from users to roles is independent of the mapping from roles to permitted operations. That separation is the part of role-based access control used here. It lets us assign a job its intended authority without making it a database owner. Ferraiolo and Kuhn, 1992, RBAC model
Choose the columns the report needs#
In a scratch database, run this setup as an administrator who can create schemas and roles and switch to the demonstration login. Leave the transaction open while testing. ROLLBACK at the end removes the setup.
BEGIN;
CREATE ROLE report_reader NOLOGIN;
CREATE ROLE report_job LOGIN;
CREATE SCHEMA shop_private;
CREATE SCHEMA shop_report;
CREATE TABLE shop_private.order_headers (
order_id integer PRIMARY KEY,
amount_cents integer NOT NULL,
customer_email text NOT NULL
);
INSERT INTO shop_private.order_headers VALUES
(101, 1300, 'order101@example.test'),
(102, 350, 'order102@example.test');
CREATE VIEW shop_report.order_amounts AS
SELECT order_id, amount_cents
FROM shop_private.order_headers;
GRANT USAGE ON SCHEMA shop_report TO report_reader;
GRANT SELECT ON shop_report.order_amounts TO report_reader;
GRANT report_reader TO report_job WITH INHERIT TRUE;
The grant on the schema allows lookup of names inside it. The grant on the view allows reading the exposed rows and columns. Membership with INHERIT TRUE makes those ordinary privileges available to report_job without switching to report_reader. That is enough for the intended query, which is an application of least privilege. PostgreSQL 18, §§5.8 and 21.3 Saltzer and Schroeder, 1975, §I.C
The job gets access through a role
Test with the job’s authority#
Change the effective role for the session. This checks the queries as the job’s role without opening a new login session. The setup role can switch back after the check.
SET ROLE report_job;
SELECT order_id, amount_cents
FROM shop_report.order_amounts
ORDER BY order_id;
| order_id | amount_cents |
|---|---|
| 101 | 1300 |
| 102 | 350 |
By default PostgreSQL checks access to the view’s base relations using the view owner’s permissions. Our view owner is the same role that created the table, so the view query can read its two declared columns. The job has no permission to read the source schema or table directly. A view created with security_invoker = true instead checks the invoking role’s privileges on the underlying relations. With that setting this job would need an additional grant to read the relevant base columns. PostgreSQL 18, CREATE VIEW
To try the direct read, establish a savepoint first. The SELECT should fail with a permission error. Continue with the last three commands to recover the transaction and remove the setup.
SAVEPOINT before_denied_read;
SELECT customer_email FROM shop_private.order_headers;
ROLLBACK TO SAVEPOINT before_denied_read;
RESET ROLE;
ROLLBACK;
Check the other ways in#
The report’s ability to read the base table depends on its total effective privileges. If someone later grants another inherited role access to the private schema and table, the report may reach the original data through that role. Revoking access from one role only removes that path. Direct grants and grants to PUBLIC, which applies to every role, can create additional paths. Verify the permitted and denied operations as the identity the application uses. PostgreSQL 18, §5.8
An ordinary view can restrict its output to chosen columns. Its privileges do not regulate copies the report makes after reading them, and a view with a row filter requires separate analysis of what a caller can infer. Here the selected columns are enough for one report. Different consumers may need different interfaces and authority.
Next#
A view gives readers a named interface to part of a schema. Changing the table underneath it is a separate problem. Schema evolution follows a column change while old and new versions of the application are both active.
References#
- PostgreSQL Global Development Group. PostgreSQL 18 documentation, 2025. Privileges, database roles, role membership and CREATE VIEW specify grants, inheritance and permission checks on views.
- David F. Ferraiolo and D. Richard Kuhn. Role-Based Access Controls. 15th National Computer Security Conference, 554–563, 1992. Defines the association of access permissions with roles.
- Jerome H. Saltzer and Michael D. Schroeder. The Protection of Information in Computer Systems. Proceedings of the IEEE, 63(9), 1278–1308, 1975. §I.C gives the design principles, including least privilege.