CREATE MATERIALIZED VIEW (Create Materialized View)

Last updated: 2026-11-04

CREATE MATERIALIZED VIEW (Create Materialized View)

CREATE MATERIALIZED VIEW materializes the result of a query into a real table on disk, trading space and write time for query speed. It suits reporting workloads that re-aggregate the same data over and over. A materialized view never updates on its own — after the base data changes you must run REFRESH MATERIALIZED VIEW.

Kindcommand
Example LanguageSQL

📝 Syntax

CREATE MATERIALIZED VIEW [IF NOT EXISTS] <materialized view name>
[(<column>, ...)]
AS <query>
[WITH [NO] DATA]
[TABLESPACE <tablespace name>]

⚙️ Parameters

ASThe query that defines the materialized view
WITH DATAPopulate the view immediately (the default)
WITH NO DATACreate the structure only and populate it later
TABLESPACEThe tablespace that holds the data

Returns:No return value. Once it succeeds the materialized view is an independent, queryable relation.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
DROP MATERIALIZED VIEW
CREATE TABLE
INSERT 0 3
CREATE MATERIALIZED VIEW
region | orders | total 
-------+--------+-------
east   |      2 | 350.50
west   |      1 | 300.00

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhen does a materialized view need a unique index?
AREFRESH MATERIALIZED VIEW CONCURRENTLY requires a unique index; without one it fails with cannot refresh materialized view ... concurrently.
QIs the data in a materialized view up to date?
ANo — it is a snapshot from creation or the last refresh. Run REFRESH MATERIALIZED VIEW to update it, usually on a schedule.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏