Whose permissions does a PostgreSQL view use?
Too long, didn’t read
A shop can see just its own rows in a table and get another shop’s row through an ordinary view. The view’s column list sets what the report can show. Its access mode determines whose table permissions and row policies PostgreSQL applies.
Pickup 104 is back
Cedar and Maple are two shops that share a pickup table. Cedar signs in with its shop role and opens the dashboard. The shop policy returns pickup 103 when Cedar reads the table directly.
The developer creates a view called dispatch.pickup_owner. A view lets the dashboard run a saved query by name. Cedar opens the new report and gets a bonus pickup. It’s 104, and it still belongs to Maple.
| Read from | Pickup numbers |
|---|---|
| Table | 103 |
| Ordinary view | 103, 104 |
| Invoker view | 103 |
Both views select id and status from the same table. Both shops can read both views. One setting accounts for the difference in the result.
Cedar stays signed in
Start with the view. Cedar needs access to its schema and SELECT permission on the view itself. That check uses Cedar’s role in both cases.
Then PostgreSQL checks access to dispatch.pickup_jobs. Whose table grants and row policies should it use?
An ordinary view uses the view owner’s privileges and row policies on the underlying table. Here the view owner also owns the pickup table. Table owners normally bypass row-level security, so the ordinary view can reach both pickups.
With security_invoker enabled, the table check uses Cedar, the role issuing the request. Cedar’s row policy lets 103 through. Maple can run that same report through the same view and gets 104. PostgreSQL CREATE VIEW · PostgreSQL row security
Cedar stays signed in along both paths. Ask either view for the active role (current_user) and it still says Cedar. The lower boxes show whose permissions PostgreSQL uses for the table.
CREATE VIEW dispatch.pickup_owner AS
SELECT id, status FROM dispatch.pickup_jobs;
CREATE VIEW dispatch.pickup_invoker
WITH (security_invoker = true) AS
SELECT id, status FROM dispatch.pickup_jobs;
GRANT SELECT ON dispatch.pickup_owner, dispatch.pickup_invoker
TO cedar_operator, maple_operator; Cedar keeps the view grant and loses the table grant
Suppose the developer lets Cedar read both views and then removes Cedar’s permission to read the table. Would both reports still work?
The ordinary view still returns both pickups. The invoker view reports a permission error on the table. Cedar can get through the first check at the view and still fail the second.
| What Cedar can read on the table | Cedar asks the invoker view for just id |
|---|---|
| No columns | Permission denied |
id only | Permission denied |
id and status | Pickup 103 |
Notice the middle row. Cedar asks for one field. The view’s own query still reads two. The base-table grants must cover the columns the definition uses.
Once Cedar gets those column grants back, it can also query those columns directly on the table. The shop policy still decides which rows are visible. This matters if the view was meant to be the only way into the underlying data. PostgreSQL CREATE VIEW
Give support just the status
Now suppose a customer calls about pickup 103. Support needs to tell them whether it’s running. The desk needs that answer for both shops and only needs the pickup number and status.
Take a separate pickup table with delivery addresses and no row-level security. The owner creates an ordinary view called dispatch.pickup_status that exposes just the pickup number and status. The support login gets access to the schema and SELECT on that view.
| Field | Table | View |
|---|---|---|
| ID | 103 | 103 |
| Status | running | running |
| Address | Dock 4 | No column |
The support login sees 103 running and 104 pending. The desk can answer the question about 103 without seeing Dock 4. Even a direct request for the pickup number on the table gets a permission error.
PostgreSQL also denied an update through the view. The support login has a read grant. A simple view like this can accept updates if the caller has the required privileges. PostgreSQL automatically updatable views
Check both reports from the login that will use them. Cedar should see just 103. The support desk should be able to answer for 103 and 104, with two fields to work from.
Replay the two view queries
The results are from PostgreSQL 18.7. Invoker views are available starting in PostgreSQL 15.
- Start with the Cedar and Maple table and read policy from the preceding article . You can use a fresh copy of that fixture. This report compares pickup IDs, so the earlier update to 103’s status does not affect the comparison.
- As the owner, create both views with the source shown above.
- Run both queries from each shop’s separate connection.
SELECT id FROM dispatch.pickup_owner ORDER BY id;
SELECT id FROM dispatch.pickup_invoker ORDER BY id; - As owner, revoke Cedar’s table read grant. Re-run both queries as Cedar.
REVOKE SELECT ON dispatch.pickup_jobs FROM cedar_operator; - Grant Cedar
SELECT (id)ondispatch.pickup_jobsand try again. Then add the view’s pair of columns.
GRANT SELECT (id, status) ON dispatch.pickup_jobs TO cedar_operator; A separate control used a non-superuser owner without BYPASSRLS. After FORCE ROW LEVEL SECURITY, the owner’s ordinary view returned zero rows because no policy allowed that owner to read any. Cedar’s invoker view still returned 103. The control kept both view definitions fixed.
Replay the support projection
Use a separate scratch database with the role name pickup_support available. From its owner connection, set up the address-bearing table, create the projection and grant the support login access.
CREATE SCHEMA dispatch;
CREATE TABLE dispatch.pickup_jobs (
id bigint PRIMARY KEY,
status text NOT NULL,
delivery_address text NOT NULL
);
INSERT INTO dispatch.pickup_jobs VALUES
(103, 'running', 'Dock 4'),
(104, 'pending', 'Dock 7'); CREATE ROLE pickup_support LOGIN;
CREATE VIEW dispatch.pickup_status AS
SELECT id, status FROM dispatch.pickup_jobs;
GRANT USAGE ON SCHEMA dispatch TO pickup_support;
GRANT SELECT ON dispatch.pickup_status TO pickup_support; Connect as pickup_support and try the view. Then ask the table directly for the pickup number. In the recorded run, both the direct request and UPDATE dispatch.pickup_status SET status = 'done' WHERE id = 103 were denied.
SELECT id, status
FROM dispatch.pickup_status
ORDER BY id;