CREATE_MATERIALIZED_VIEW(7)
โจ CREATE MATERIALIZED VIEW(7) โจ
okay bestie, so this is the CREATE MATERIALIZED VIEW page from the PostgreSQL 18.6 docs, and it lives in section 7 of the manual ๐ literally one of my fave pages because it's giving "save your query results, refresh them later, be iconic" energy. no cap.
๐ธ NAME ๐ธ
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 ๐
so CREATE MATERIALIZED VIEW defines a materialized view of a query, and that's the whole vibe. the query gets executed and used to populate the view right at the moment you issue the command โ unless you said WITH NO DATA, in which case it just... doesn't ๐ โ and you can refresh it later with REFRESH MATERIALIZED VIEW. periodt.
it's basically the cousin of CREATE TABLE AS, except it remembers the query it was initialized with, so it can be refreshed later whenever you feel like it. main character behavior honestly. a materialized view has a lot of the same properties as a regular table, but โ and this is the important bit โ there is no support for temporary materialized views. sorry bestie, that's just not a thing. ๐
also, CREATE MATERIALIZED VIEW requires CREATE privilege on the schema you're putting the materialized view in. no privilege, no view. that's the rules. ๐
๐ PARAMETERS ๐
IF NOT EXISTSโ don't throw an error if a materialized view with the same name already exists. instead a notice gets issued. but heads up babes: there is no guarantee that the materialized view that already exists is anything like the one you would've created. it could be a whole different situation. ๐ญtable_nameโ the name of the materialized view you're creating, optionally schema-qualified. and it has to be distinct from the name of any other relation in the same schema โ that's tables, sequences, indexes, views, materialized views, foreign tables, the whole friend group. no name collisions allowed, we don't do that here. โจcolumn_nameโ the name of a column in the new materialized view. if you don't provide column names, they just get taken from the output column names of thequery. easy peasy. ๐USING methodโ optional clause that says which table access method to use for storing the contents of the new materialized view. the method has to be an access method of typeTABLE. see Chapter 62 for the deets. if you skip this option, the default table access method gets chosen for the new materialized view โ check outdefault_table_access_methodfor more. ๐WITH ( storage_parameter [= value] [, ... ] )โ this clause sets optional storage parameters for the new materialized view. check the Storage Parameters section in theCREATE TABLE(CREATE_TABLE(7)) docs for more info. literally all the parameters supported forCREATE TABLEare also supported here. seeCREATE TABLE(CREATE_TABLE(7)) for more. ๐TABLESPACE tablespace_nameโtablespace_nameis the name of the tablespace where the new materialized view gets created. if you don't specify one,default_tablespacegets consulted instead. ๐queryโ aSELECT,TABLE, orVALUEScommand. heads up though: this query runs inside a security-restricted operation. specifically, calls to functions that themselves create temporary tables will fail. also, while the query is running, thesearch_pathis temporarily changed topg_catalog, pg_temp. iykyk. ๐ญWITH [ NO ] DATAโ this clause decides whether the materialized view gets populated at creation time or not. if it doesn't, the materialized view gets flagged as unscannable and you can't query it until you runREFRESH MATERIALIZED VIEW. so like... it's just sitting there. unusable. waiting. ๐
โญ COMPATIBILITY โญ
CREATE MATERIALIZED VIEW is a PostgreSQL extension. so like, don't go expecting it in other databases bestie, it's our thing. ๐
๐ซถ SEE ALSO ๐ซถ
ALTER MATERIALIZED VIEW(ALTER_MATERIALIZED_VIEW(7))CREATE TABLE AS(CREATE_TABLE_AS(7))CREATE VIEW(CREATE_VIEW(7))DROP MATERIALIZED VIEW(DROP_MATERIALIZED_VIEW(7))REFRESH MATERIALIZED VIEW(REFRESH_MATERIALIZED_VIEW(7))
and that's the whole page!! this one's from PostgreSQL 18.6, dated 2026, and the page itself is CREATE MATERIALIZED VIEW(7) โ go forth and materialize your views, bestie, you're gonna ate ๐
โจ๐ฆ๐ธ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms