> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/reference/sql-commands/sql-creatematerializedview.md).

# CREATE MATERIALIZED VIEW

### CREATE MATERIALIZED VIEW

CREATE MATERIALIZED VIEW — define a new materialized view

### Synopsis

```

CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] table_name
    [ (column_name [, ...] ) ]
    [ USING method ]
    [ WITH ( storage_parameter [= value] [, ... ] ) ]
    [ TABLESPACE tablespace_name ]
    AS query
    [ WITH [ NO ] DATA ]
```

### Description

`CREATE MATERIALIZED VIEW` defines a materialized view of a query. The query is executed and used to populate the view at the time the command is issued (unless `WITH NO DATA` is used) and may be refreshed later using `REFRESH MATERIALIZED VIEW`.

`CREATE MATERIALIZED VIEW` is similar to `CREATE TABLE AS`, except that it also remembers the query used to initialize the view, so that it can be refreshed later upon demand. A materialized view has many of the same properties as a table, but there is no support for temporary materialized views.

`CREATE MATERIALIZED VIEW` requires `CREATE` privilege on the schema used for the materialized view.

### Parameters

`IF NOT EXISTS` : Do not throw an error if a materialized view with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing materialized view is anything like the one that would have been created.

*`table_name`* : The name (optionally schema-qualified) of the materialized view to be created. The name must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema.

*`column_name`* : The name of a column in the new materialized view. If column names are not provided, they are taken from the output column names of the query.

`USING method` : This optional clause specifies the table access method to use to store the contents for the new materialized view; the method needs be an access method of type `TABLE`. See [Chapter 62](/internals/tableam.md) for more information. If this option is not specified, the default table access method is chosen for the new materialized view. See [default\_table\_access\_method](/server-administration/runtime-config/runtime-config-client.md#GUC-DEFAULT-TABLE-ACCESS-METHOD) for more information.

`WITH ( storage_parameter [= value] [, ... ] )` : This clause specifies optional storage parameters for the new materialized view; see [Storage Parameters](/reference/sql-commands/sql-createtable.md#SQL-CREATETABLE-STORAGE-PARAMETERS) in the [CREATE TABLE](/reference/sql-commands/sql-createtable.md) documentation for more information. All parameters supported for `CREATE TABLE` are also supported for `CREATE MATERIALIZED VIEW`. See [CREATE TABLE](/reference/sql-commands/sql-createtable.md) for more information.

`TABLESPACE tablespace_name` : The *`tablespace_name`* is the name of the tablespace in which the new materialized view is to be created. If not specified, [default\_tablespace](/server-administration/runtime-config/runtime-config-client.md#GUC-DEFAULT-TABLESPACE) is consulted.

*`query`* : A [`SELECT`](/reference/sql-commands/sql-select.md), [`TABLE`](/reference/sql-commands/sql-select.md#SQL-TABLE), or [`VALUES`](/reference/sql-commands/sql-values.md) command. This query will run within a security-restricted operation; in particular, calls to functions that themselves create temporary tables will fail. Also, while the query is running, the [search\_path](/server-administration/runtime-config/runtime-config-client.md#GUC-SEARCH-PATH) is temporarily changed to `pg_catalog, pg_temp`.

`WITH [ NO ] DATA` : This clause specifies whether or not the materialized view should be populated at creation time. If not, the materialized view will be flagged as unscannable and cannot be queried until `REFRESH MATERIALIZED VIEW` is used.

### Compatibility

`CREATE MATERIALIZED VIEW` is a PostgreSQL extension.

### See Also

[ALTER MATERIALIZED VIEW](/reference/sql-commands/sql-altermaterializedview.md), [CREATE TABLE AS](/reference/sql-commands/sql-createtableas.md), [CREATE VIEW](/reference/sql-commands/sql-createview.md), [DROP MATERIALIZED VIEW](/reference/sql-commands/sql-dropmaterializedview.md), [REFRESH MATERIALIZED VIEW](/reference/sql-commands/sql-refreshmaterializedview.md)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/sql-creatematerializedview.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/reference/sql-commands/sql-creatematerializedview.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
