mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem The "View security" section of the views guide explains `security_invoker` but not which role is checked when the view is queried through the Data API, or why a `security_invoker` view over `auth` tables fails with every API key. Related to [FDBKIN-21897](https://linear.app/supabase/issue/FDBKIN-21897) Closes [DOCS-1687](https://linear.app/supabase/issue/DOCS-1687) ## Solution Adds two paragraphs of body text after the SQL example in "View security" (`apps/docs/content/guides/database/views.mdx`). The existing paragraph and SQL are unchanged. - The first paragraph says that through the Data API, Postgres checks the role from the request's API key or JWT (`anon`, `authenticated`, or `service_role`). That role needs privileges, such as `select`, on every table the view reads, not only on the view. Postgres logs show `authenticator`, the role the Data API connects as, not the role the view checks. - The second paragraph says that by default, API roles have no privileges on tables in the `auth` schema, so a `security_invoker` view over `auth.users` fails with `permission denied for table users`, even with a secret key. It points readers to storing user data in a `public` table, with a link to the User management guide. It does not recommend granting `auth` tables to any role. Needs review: the text says "by default" for `auth` grants because older projects may carry extra grants. Checks run: `prettier --check` on the changed file and `pnpm turbo run lint --filter='[origin/master]'` pass on the latest commit. `pnpm turbo run typecheck --filter='...[origin/master]'` passed on the first commit; the second commit changes only MDX prose. Docs tests were skipped because they need a local Supabase stack (Docker). ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
188 lines
7.0 KiB
Plaintext
188 lines
7.0 KiB
Plaintext
---
|
|
id: 'views'
|
|
title: 'Database views'
|
|
description: 'Creating and using views in Postgres.'
|
|
tocVideo: 'C9kJwxhdw9A'
|
|
---
|
|
|
|
Learn what views are and when to use them.
|
|
|
|
Views sit on top of the tables you create in [Tables and data](/docs/guides/database/tables).
|
|
|
|
A view is a convenient shortcut to a query. Creating a view doesn't involve new tables or data. When you run a view, Postgres executes the underlying query and returns its results.
|
|
|
|
Say you have the following tables from a university database:
|
|
|
|
**`students`**
|
|
|
|
| id | name | type |
|
|
| --- | ---------------- | ------------- |
|
|
| 1 | Princess Leia | undergraduate |
|
|
| 2 | Yoda | graduate |
|
|
| 3 | Anakin Skywalker | graduate |
|
|
|
|
**`courses`**
|
|
|
|
| id | title | code |
|
|
| --- | ------------------------ | ------- |
|
|
| 1 | Introduction to Postgres | PG101 |
|
|
| 2 | Authentication Theories | AUTH205 |
|
|
| 3 | Fundamentals of Supabase | SUP412 |
|
|
|
|
**`grades`**
|
|
|
|
| id | student_id | course_id | result |
|
|
| --- | ---------- | --------- | ------ |
|
|
| 1 | 1 | 1 | B+ |
|
|
| 2 | 1 | 3 | A+ |
|
|
| 3 | 2 | 2 | A |
|
|
| 4 | 3 | 1 | A- |
|
|
| 5 | 3 | 2 | A |
|
|
| 6 | 3 | 3 | B- |
|
|
|
|
Creating a view that consists of all three tables looks like this:
|
|
|
|
```sql
|
|
create view transcripts as
|
|
select
|
|
students.name,
|
|
students.type,
|
|
courses.title,
|
|
courses.code,
|
|
grades.result
|
|
from grades
|
|
left join students on grades.student_id = students.id
|
|
left join courses on grades.course_id = courses.id;
|
|
|
|
grant all on table transcripts to authenticated;
|
|
```
|
|
|
|
Then you can access the underlying query with:
|
|
|
|
```sql
|
|
select * from transcripts;
|
|
```
|
|
|
|
## View security
|
|
|
|
By default, Postgres checks permissions on a view's underlying tables against the view's owner rather than the role running the query. If a privileged role creates a view, everyone reading it does so through that role's permissions. Define the view with the `security_invoker` modifier to check the querying role's permissions instead, which is also what makes the underlying tables' [row level security](/docs/guides/database/postgres/row-level-security) policies apply.
|
|
|
|
```sql
|
|
-- switch an existing view to the querying role's permissions
|
|
alter view <view name>
|
|
set (security_invoker = true);
|
|
|
|
-- create a view with the security_invoker modifier
|
|
create view <view name> with(security_invoker=true) as (
|
|
select * from <some table>
|
|
);
|
|
```
|
|
|
|
When you query a `security_invoker` view through the Data API, Postgres checks the role from the request's API key or JWT: `anon`, `authenticated`, or `service_role`. That role needs privileges, such as `select`, on every table the view reads, not only on the view. Postgres logs show `authenticator`, but that's the role the Data API connects as, not the role the view checks.
|
|
|
|
By default, API roles have no privileges on tables in the `auth` schema. A `security_invoker` view that reads `auth.users` fails with `permission denied for table users`, even with a secret key. To expose user data through the Data API, store it in a table in the `public` schema. See [User management](/docs/guides/auth/managing-user-data).
|
|
|
|
## When to use views
|
|
|
|
Views provide several benefits.
|
|
|
|
### Simplicity
|
|
|
|
As a query becomes more complex, calling it repeatedly gets tedious, especially when you run it regularly. In the example above, instead of repeatedly running:
|
|
|
|
```sql
|
|
select
|
|
students.name,
|
|
students.type,
|
|
courses.title,
|
|
courses.code,
|
|
grades.result
|
|
from
|
|
grades
|
|
left join students on grades.student_id = students.id
|
|
left join courses on grades.course_id = courses.id;
|
|
```
|
|
|
|
You can run this instead:
|
|
|
|
```sql
|
|
select * from transcripts;
|
|
```
|
|
|
|
A view also behaves like a typical table. You can safely use it in table joins or create new views from existing views.
|
|
|
|
### Consistency
|
|
|
|
Views reduce the likelihood of mistakes when you execute a query repeatedly. In the example above, you might decide to exclude the course _Introduction to Postgres_. The query becomes:
|
|
|
|
```sql
|
|
select
|
|
students.name,
|
|
students.type,
|
|
courses.title,
|
|
courses.code,
|
|
grades.result
|
|
from
|
|
grades
|
|
left join students on grades.student_id = students.id
|
|
left join courses on grades.course_id = courses.id
|
|
where courses.code != 'PG101';
|
|
```
|
|
|
|
Without a view, you need to add the new rule to every dependent query. That increases the likelihood of errors and inconsistencies, and it takes considerable effort. With views, you alter the underlying query in the `transcripts` view, and the change applies to every application using it.
|
|
|
|
### Logical organization
|
|
|
|
With views, you can give your query a name. This is useful for teams working with the same database. Instead of guessing what a query does, a well-named view explains it. For example, the name of the `transcripts` view suggests that the underlying query involves the `students`, `courses`, and `grades` tables.
|
|
|
|
### Security
|
|
|
|
Views can restrict the amount and type of data presented to a user. Instead of giving a user direct access to a set of tables, you give them a view. You can prevent them from reading sensitive columns by excluding those columns from the underlying query.
|
|
|
|
## Materialized views
|
|
|
|
A [materialized view](https://www.postgresql.org/docs/current/rules-materializedviews.html) is a form of view that also stores its results to disk. Subsequent reads of a materialized view return results much faster than a conventional view, because the data is already available. A conventional view executes the underlying query each time you call it.
|
|
|
|
Using the example above, you can create a materialized view like this:
|
|
|
|
```sql
|
|
create materialized view transcripts as
|
|
select
|
|
students.name,
|
|
students.type,
|
|
courses.title,
|
|
courses.code,
|
|
grades.result
|
|
from
|
|
grades
|
|
left join students on grades.student_id = students.id
|
|
left join courses on grades.course_id = courses.id;
|
|
```
|
|
|
|
Reading from the materialized view is the same as a conventional view:
|
|
|
|
```sql
|
|
select * from transcripts;
|
|
```
|
|
|
|
## Refreshing materialized views
|
|
|
|
There's a trade-off: data in a materialized view isn't always up to date. Refresh it regularly to prevent the data from becoming too stale.
|
|
|
|
```sql
|
|
refresh materialized view transcripts;
|
|
```
|
|
|
|
How often you refresh a materialized view is up to you, and it probably differs for each view depending on its use case.
|
|
|
|
## Materialized views vs conventional views
|
|
|
|
Materialized views are useful when execution times for queries or views are too slow. This happens in views or queries that involve multiple tables and billions of rows. Use a materialized view only when you can tolerate outdated data. Internal dashboards and analytics are common use cases.
|
|
|
|
Creating a materialized view isn't a solution to inefficient queries. Always optimize a slow-running query, even when you implement a materialized view.
|
|
|
|
## Resources
|
|
|
|
- [Official Docs: Create view](https://www.postgresql.org/docs/current/sql-createview.html)
|
|
- [Postgres Tutorial: Views](https://www.postgresqltutorial.com/postgresql-views/)
|