Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs(database): split out views and group the tables guide by information type (#50022)

Miranda Limonczenko5979218c973f
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index 6a286b82642..0909dba5851 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -1063,9 +1063,13 @@ export const database: NavMenuConstant = {
url: undefined,
items: [
{
- name: 'Managing tables, views, and data',
+ name: 'Managing tables and data',
url: '/guides/database/tables' as `/${string}`,
},
+ {
+ name: 'Views',
+ url: '/guides/database/views' as `/${string}`,
+ },
{
name: 'Working with arrays',
url: '/guides/database/arrays' as `/${string}`,
@@ -1650,9 +1654,13 @@ export const api: NavMenuConstant = {
url: '/guides/api/data-apis',
items: [
{
- name: 'Managing tables, views, and data',
+ name: 'Managing tables and data',
url: '/guides/database/tables' as `/${string}`,
},
+ {
+ name: 'Views',
+ url: '/guides/database/views' as `/${string}`,
+ },
{
name: 'Querying joins and nested tables',
url: '/guides/database/joins-and-nesting' as `/${string}`,
diff --git a/apps/docs/content/guides/ai/engineering-for-scale.mdx b/apps/docs/content/guides/ai/engineering-for-scale.mdx
index bf6beeae17f..9342876b71f 100644
--- a/apps/docs/content/guides/ai/engineering-for-scale.mdx
+++ b/apps/docs/content/guides/ai/engineering-for-scale.mdx
@@ -12,7 +12,7 @@ Content sources for vectors can be extremely large. As you grow you should run y
For small workloads, you can typically store your data in a single database.
-If you've used [Vecs](/docs/guides/ai/vecs-python-client) to create 3 different collections, you can expose collections to your web or mobile application using [views](/docs/guides/database/tables#views):
+If you've used [Vecs](/docs/guides/ai/vecs-python-client) to create 3 different collections, you can expose collections to your web or mobile application using [views](/docs/guides/database/views):
The diagram below shows a single database holding the three vector collections of `docs`, `posts`, and `images`. Each are exposed to your application through a view.
diff --git a/apps/docs/content/guides/database/tables.mdx b/apps/docs/content/guides/database/tables.mdx
index 78a42ba4c53..5132939f310 100644
--- a/apps/docs/content/guides/database/tables.mdx
+++ b/apps/docs/content/guides/database/tables.mdx
@@ -1,10 +1,23 @@
---
id: 'tables'
-title: 'Tables and Data'
+title: 'Tables and data'
description: 'Creating and using Postgres tables.'
video: 'https://www.youtube.com/v/TKwF3IGij5c'
---
+Learn what tables are and how to use them.
+
+This guide is organized into several groups:
+
+- [What is a table?](#what-is-a-table) explains the basics if you're new to relational databases.
+- [Creating and managing tables](#creating-and-managing-tables) is the action path: create a table, load rows into it, and link it to other tables.
+- [How tables are organized](#how-tables-are-organized) is the background: primary keys, relationships, and schemas.
+- [Reference](#reference) lists the column data types that Postgres supports.
+
+For saved queries that behave like tables, see [Views](/docs/guides/database/views).
+
+## What is a table?
+
Tables are where you store your data.
Tables are similar to Excel spreadsheets. They contain columns and rows.
@@ -23,7 +36,9 @@ For example, this table has 3 columns named `id`, `name`, and `description`, and
There are a few important differences from a spreadsheet, but it's a good starting point if you're new to relational databases.
-## Creating tables
+## Creating and managing tables
+
+### Creating tables
When creating a table, it's best practice to add columns at the same time.
@@ -86,103 +101,14 @@ When naming tables, use lowercase and underscores instead of spaces. For example
</Admonition>
-## Columns
-
-You must define the data type when you create a column.
-
-### Data types
-
-Every column has a data type. Postgres provides many [default types](https://www.postgresql.org/docs/current/datatype.html), and you can design your own or use extensions if the default types don't fit your needs. You can use any data type that Postgres supports via the SQL editor. The Table Editor supports a subset of these, which keeps the experience focused for people with less database experience.
-
-<details>
-<summary>Show/Hide default data types</summary>
-
-| `Name` | `Aliases` | `Description` |
-| --------------------------------- | ------------- | ---------------------------------------------------------------- |
-| `bigint` | `int8` | signed eight-byte integer |
-| `bigserial` | `serial8` | autoincrementing eight-byte integer |
-| `bit` | | fixed-length bit string |
-| `bit varying` | `varbit` | variable-length bit string |
-| `boolean` | `bool` | logical Boolean (true/false) |
-| `box` | | rectangular box on a plane |
-| `bytea` | | binary data (“byte array”) |
-| `character` | `char` | fixed-length character string |
-| `character varying` | `varchar` | variable-length character string |
-| `cidr` | | IPv4 or IPv6 network address |
-| `circle` | | circle on a plane |
-| `date` | | calendar date (year, month, day) |
-| `double precision` | `float8` | double precision floating-point number (8 bytes) |
-| `inet` | | IPv4 or IPv6 host address |
-| `integer` | `int`, `int4` | signed four-byte integer |
-| `interval [ fields ]` | | time span |
-| `json` | | textual JSON data |
-| `jsonb` | | binary JSON data, decomposed |
-| `line` | | infinite line on a plane |
-| `lseg` | | line segment on a plane |
-| `macaddr` | | MAC (Media Access Control) address |
-| `macaddr8` | | MAC (Media Access Control) address (EUI-64 format) |
-| `money` | | currency amount |
-| `numeric` | `decimal` | exact numeric of selectable precision |
-| `path` | | geometric path on a plane |
-| `pg_lsn` | | Postgres Log Sequence Number |
-| `pg_snapshot` | | user-level transaction ID snapshot |
-| `point` | | geometric point on a plane |
-| `polygon` | | closed geometric path on a plane |
-| `real` | `float4` | single precision floating-point number (4 bytes) |
-| `smallint` | `int2` | signed two-byte integer |
-| `smallserial` | `serial2` | autoincrementing two-byte integer |
-| `serial` | `serial4` | autoincrementing four-byte integer |
-| `text` | | variable-length character string |
-| `time [ without time zone ]` | | time of day (no time zone) |
-| `time with time zone` | `timetz` | time of day, including time zone |
-| `timestamp [ without time zone ]` | | date and time (no time zone) |
-| `timestamp with time zone` | `timestamptz` | date and time, including time zone |
-| `tsquery` | | text search query |
-| `tsvector` | | text search document |
-| `txid_snapshot` | | user-level transaction ID snapshot (deprecated; see pg_snapshot) |
-| `uuid` | | universally unique identifier |
-| `xml` | | XML data |
-
-</details>
-
-You can cast columns from one type to another, but some types are incompatible.
-For example, if you cast a `timestamp` to a `date`, you lose all the time information that was previously saved.
-
-### Primary keys
-
-A table can have a primary key, a unique identifier for every row of data. A few tips for primary keys:
-
-- Create a primary key for every table in your database.
-- You can use any column as a primary key, as long as it is unique for every row.
-- It's common to use a `uuid` type or a numbered `identity` column as your primary key.
-
-```sql
-create table movies (
- id bigint generated always as identity primary key
-);
-```
-
-In the example above, you:
-
-1. Created a column called `id`.
-2. Assigned the data type `bigint`.
-3. Instructed the database that this column is `generated always as identity`, so Postgres automatically assigns it a unique number.
-4. Used it as the `primary key`, because the value is unique.
-
-You can also use `generated by default as identity`, which lets you insert your own unique values.
-
-```sql
-create table movies (
- id bigint generated by default as identity primary key
-);
-```
+You now have a table with its columns defined. To put rows in it, continue to [Loading data](#loading-data).
-## Loading data
+### Loading data
There are several ways to load data in Supabase. You can load data directly into the database, or use the [Data API](/docs/guides/api).
-If you're loading large data sets, follow the bulk data loading instructions.
+If you're loading large data sets, follow the [bulk data loading](#bulk-data-loading) instructions.
-### Basic data loading
+#### Basic data loading
<Tabs
scrollable
@@ -347,7 +273,7 @@ await supabase.From<Movie>().Insert(movies);
</$Show>
</Tabs>
-### Bulk data loading
+#### Bulk data loading
When inserting large data sets, use Postgres's [COPY](https://www.postgresql.org/docs/current/sql-copy.html) command.
This loads data directly from a file into a table. Several file formats are available for copying data, including text, CSV, binary, and JSON.
@@ -375,22 +301,9 @@ psql -h DATABASE_URL -p 5432 -d postgres -U postgres \
If you receive an error `FATAL: password authentication failed for user "postgres"`, reset your database password in **Database Settings** and try again.
-## Joining tables with foreign keys
-
-Tables can be joined together using foreign keys.
-
-<Image
- alt="Two tables. An arrow runs from a highlighted column in the first table to a matching highlighted column in the second."
+### Joining tables with foreign keys
-src={{
- dark: '/docs/img/database/managing-tables/joining-tables.png',
- light: '/docs/img/database/managing-tables/joining-tables--light.png',
- }}
-width={1600}
-height={1145}
-/>
-
-This is where the term relational comes from, because data typically forms some sort of relationship.
+Foreign keys are how you express a relationship between two tables. For what that relationship means, see [Relationships between tables](#relationships-between-tables).
In the `movies` example above, you might want to add a category for each movie, such as Action or Documentary.
Create a new table called `categories` and link it to the `movies` table.
@@ -412,20 +325,6 @@ For example, consider this situation:
- A movie can have several `actors`.
- An `actor` can perform in several movies.
-<Tabs
- scrollable
- size="small"
- type="underlined"
- defaultActiveId="dashboard"
- queryGroup="database-method"
->
-<TabPanel id="dashboard" label="Dashboard">
-
-<YouTube id="TKwF3IGij5c" title="Joining tables with foreign keys in the dashboard" />
-
-</TabPanel>
-<TabPanel id="sql" label="SQL">
-
```sql
create table movies (
id bigint generated by default as identity primary key,
@@ -445,10 +344,59 @@ create table performances (
);
```
-</TabPanel>
-</Tabs>
+## How tables are organized
-## Schemas
+Background on the pieces the procedures above use. Read these when you want to know why a table is shaped the way it is.
+
+### Primary keys
+
+A table can have a primary key, a unique identifier for every row of data. A few tips for primary keys:
+
+- Create a primary key for every table in your database.
+- You can use any column as a primary key, as long as it is unique for every row.
+- It's common to use a `uuid` type or a numbered `identity` column as your primary key.
+
+```sql
+create table movies (
+ id bigint generated always as identity primary key
+);
+```
+
+In the example above, you:
+
+1. Created a column called `id`.
+2. Assigned the data type `bigint`.
+3. Instructed the database that this column is `generated always as identity`, so Postgres automatically assigns it a unique number.
+4. Used it as the `primary key`, because the value is unique.
+
+You can also use `generated by default as identity`, which lets you insert your own unique values.
+
+```sql
+create table movies (
+ id bigint generated by default as identity primary key
+);
+```
+
+### Relationships between tables
+
+Tables can be joined together using foreign keys.
+
+<Image
+ alt="Two tables. An arrow runs from a highlighted column in the first table to a matching highlighted column in the second."
+
+src={{
+ dark: '/docs/img/database/managing-tables/joining-tables.png',
+ light: '/docs/img/database/managing-tables/joining-tables--light.png',
+ }}
+width={1600}
+height={1145}
+/>
+
+This is where the term relational comes from, because data typically forms some sort of relationship.
+
+To create a foreign key, see [Joining tables with foreign keys](#joining-tables-with-foreign-keys).
+
+### Schemas
Tables belong to schemas. Schemas are a way of organizing your tables, often for security reasons.
@@ -487,184 +435,70 @@ A custom schema isn't reachable through the Supabase Data API until you expose i
</Admonition>
-## Views
-
-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`**
-
-{/* supa-mdx-lint-disable Rule003Spelling */}
-
-| id | name | type |
-| --- | ---------------- | ------------- |
-| 1 | Princess Leia | undergraduate |
-| 2 | Yoda | graduate |
-| 3 | Anakin Skywalker | graduate |
-
-{/* supa-mdx-lint-enable Rule003Spelling */}
+## Reference
-**`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, views are accessed with their creator's permission, known as `security definer`. If a privileged role creates a view, others accessing it use that role's elevated permissions. To enforce row level security policies, define the view with the `security_invoker` modifier.
-
-```sql
--- alter a security_definer view to be security_invoker
-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 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';
-```
+Reference material for choosing a column type.
-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.
+### Data types
-```sql
-refresh materialized view transcripts;
-```
+Every column has a data type. Postgres provides many [default types](https://www.postgresql.org/docs/current/datatype.html), and you can design your own or use extensions if the default types don't fit your needs. You can use any data type that Postgres supports via the SQL editor. The Table Editor supports a subset of these, which keeps the experience focused for people with less database experience.
-How often you refresh a materialized view is up to you, and it probably differs for each view depending on its use case.
+<details>
+<summary>Show/Hide default data types</summary>
-### Materialized views vs conventional views
+| `Name` | `Aliases` | `Description` |
+| --------------------------------- | ------------- | ---------------------------------------------------------------- |
+| `bigint` | `int8` | signed eight-byte integer |
+| `bigserial` | `serial8` | autoincrementing eight-byte integer |
+| `bit` | | fixed-length bit string |
+| `bit varying` | `varbit` | variable-length bit string |
+| `boolean` | `bool` | logical Boolean (true/false) |
+| `box` | | rectangular box on a plane |
+| `bytea` | | binary data (“byte array”) |
+| `character` | `char` | fixed-length character string |
+| `character varying` | `varchar` | variable-length character string |
+| `cidr` | | IPv4 or IPv6 network address |
+| `circle` | | circle on a plane |
+| `date` | | calendar date (year, month, day) |
+| `double precision` | `float8` | double precision floating-point number (8 bytes) |
+| `inet` | | IPv4 or IPv6 host address |
+| `integer` | `int`, `int4` | signed four-byte integer |
+| `interval [ fields ]` | | time span |
+| `json` | | textual JSON data |
+| `jsonb` | | binary JSON data, decomposed |
+| `line` | | infinite line on a plane |
+| `lseg` | | line segment on a plane |
+| `macaddr` | | MAC (Media Access Control) address |
+| `macaddr8` | | MAC (Media Access Control) address (EUI-64 format) |
+| `money` | | currency amount |
+| `numeric` | `decimal` | exact numeric of selectable precision |
+| `path` | | geometric path on a plane |
+| `pg_lsn` | | Postgres Log Sequence Number |
+| `pg_snapshot` | | user-level transaction ID snapshot |
+| `point` | | geometric point on a plane |
+| `polygon` | | closed geometric path on a plane |
+| `real` | `float4` | single precision floating-point number (4 bytes) |
+| `smallint` | `int2` | signed two-byte integer |
+| `smallserial` | `serial2` | autoincrementing two-byte integer |
+| `serial` | `serial4` | autoincrementing four-byte integer |
+| `text` | | variable-length character string |
+| `time [ without time zone ]` | | time of day (no time zone) |
+| `time with time zone` | `timetz` | time of day, including time zone |
+| `timestamp [ without time zone ]` | | date and time (no time zone) |
+| `timestamp with time zone` | `timestamptz` | date and time, including time zone |
+| `tsquery` | | text search query |
+| `tsvector` | | text search document |
+| `txid_snapshot` | | user-level transaction ID snapshot (deprecated; see pg_snapshot) |
+| `uuid` | | universally unique identifier |
+| `xml` | | XML data |
-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.
+</details>
-Creating a materialized view isn't a solution to inefficient queries. Always optimize a slow-running query, even when you implement a materialized view.
+You can cast columns from one type to another, but some types are incompatible.
+For example, if you cast a `timestamp` to a `date`, you lose all the time information that was previously saved.
## Resources
- [Official Docs: Create table](https://www.postgresql.org/docs/current/sql-createtable.html)
-- [Official Docs: Create view](https://www.postgresql.org/docs/current/sql-createview.html)
- [Postgres Tutorial: Create tables](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-create-table/)
- [Postgres Tutorial: Add column](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-add-column/)
-- [Postgres Tutorial: Views](https://www.postgresqltutorial.com/postgresql-views/)
diff --git a/apps/docs/content/guides/database/views.mdx b/apps/docs/content/guides/database/views.mdx
new file mode 100644
index 00000000000..bda82d3df2e
--- /dev/null
+++ b/apps/docs/content/guides/database/views.mdx
@@ -0,0 +1,186 @@
+---
+id: 'views'
+title: 'Views'
+description: 'Creating and using views in Postgres.'
+---
+
+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`**
+
+{/* supa-mdx-lint-disable Rule003Spelling */}
+
+| id | name | type |
+| --- | ---------------- | ------------- |
+| 1 | Princess Leia | undergraduate |
+| 2 | Yoda | graduate |
+| 3 | Anakin Skywalker | graduate |
+
+{/* supa-mdx-lint-enable Rule003Spelling */}
+
+**`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 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 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/)