3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE VIEW" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_VIEW \- define a new view
35 CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW \fIname\fR [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
36 [ WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] ) ]
38 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
43 defines a view of a query\&. The view is not physically materialized\&. Instead, the query is run every time the view is referenced in a query\&.
45 \fBCREATE OR REPLACE VIEW\fR
46 is similar, but if a view of the same name already exists, it is replaced\&. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list\&. The calculations giving rise to the output columns may be completely different\&.
48 If a schema name is given (for example,
49 CREATE VIEW myschema\&.myview \&.\&.\&.) then the view is created in the specified schema\&. Otherwise it is created in the current schema\&. Temporary views exist in a special schema, so a schema name cannot be given when creating a temporary view\&. The name of the view must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema\&.
54 If specified, the view is created as a temporary view\&. Temporary views are automatically dropped at the end of the current session\&. Existing permanent relations with the same name are not visible to the current session while the temporary view exists, unless they are referenced with schema\-qualified names\&.
56 If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether
58 is specified or not)\&.
63 Creates a recursive view\&. The syntax
69 CREATE RECURSIVE VIEW [ \fIschema\fR \&. ] \fIview_name\fR (\fIcolumn_names\fR) AS SELECT \fI\&.\&.\&.\fR;
81 CREATE VIEW [ \fIschema\fR \&. ] \fIview_name\fR AS WITH RECURSIVE \fIview_name\fR (\fIcolumn_names\fR) AS (SELECT \fI\&.\&.\&.\fR) SELECT \fIcolumn_names\fR FROM \fIview_name\fR;
87 A view column name list must be specified for a recursive view\&.
92 The name (optionally schema\-qualified) of a view to be created\&.
97 An optional list of names to be used for columns of the view\&. If not given, the column names are deduced from the query\&.
100 WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] )
102 This clause specifies optional parameters for a view; the following parameters are supported:
106 This parameter may be either
109 cascaded, and is equivalent to specifying
110 WITH [ CASCADED | LOCAL ] CHECK OPTION
114 security_barrier (boolean)
116 This should be used if the view is intended to provide row\-level security\&. See
121 security_invoker (boolean)
123 This option causes the underlying base relations to be checked against the privileges of the user of the view rather than the view owner\&. See the notes below for full details\&.
126 All of the above options can be changed on existing views using
136 command which will provide the columns and rows of the view\&.
139 WITH [ CASCADED | LOCAL ] CHECK OPTION
141 This option controls the behavior of automatically updatable views\&. When this option is specified,
145 commands on the view will be checked to ensure that new rows satisfy the view\-defining condition (that is, the new rows are checked to ensure that they are visible through the view)\&. If they are not, the update will be rejected\&. If the
151 commands on the view are allowed to create rows that are not visible through the view\&. The following check options are supported:
155 New rows are only checked against the conditions defined directly in the view itself\&. Any conditions defined on underlying base views are not checked (unless they also specify the
161 New rows are checked against the conditions of the view and all underlying base views\&. If the
163 is specified, and neither
180 is only supported on views that are automatically updatable, and do not have
184 rules\&. If an automatically updatable view is defined on top of a base view that has
188 may be used to check the conditions on the automatically updatable view, but the conditions on the base view with
190 triggers will not be checked (a cascaded check option will not cascade down to a trigger\-updatable view, and any check options defined directly on a trigger\-updatable view will be ignored)\&. If the view or any of its base relations has an
196 command to be rewritten, then all check options will be ignored in the rewritten query, including any checks from automatically updatable views defined on top of the relation with the
200 is not supported if the view or any of its base relations have rules\&.
206 statement to drop views\&.
208 Be careful that the names and types of the view\*(Aqs columns will be assigned the way you want\&. For example:
214 CREATE VIEW vista AS SELECT \*(AqHello World\*(Aq;
220 is bad form because the column name defaults to
221 ?column?; also, the column data type defaults to
222 text, which might not be what you wanted\&. Better style for a string literal in a view\*(Aqs result is something like:
228 CREATE VIEW vista AS SELECT text \*(AqHello World\*(Aq AS hello;
234 By default, access to the underlying base relations referenced in the view is determined by the permissions of the view owner\&. In some cases, this can be used to provide secure but restricted access to the underlying tables\&. However, not all views are secure against tampering; see
241 true, access to the underlying base relations is determined by the permissions of the user executing the query, rather than the view owner\&. Thus, the user of a security invoker view must have the relevant permissions on the view and its underlying base relations\&.
243 If any of the underlying base relations is a security invoker view, it will be treated as if it had been accessed directly from the original query\&. Thus, a security invoker view will always check its underlying base relations using the permissions of the current user, even if it is accessed from a view without the
247 If any of the underlying base relations has
249 enabled, then by default, the row\-level security policies of the view owner are applied, and access to any additional relations referred to by those policies is determined by the permissions of the view owner\&. However, if the view has
252 true, then the policies and permissions of the invoking user are used instead, as if the base relations had been referenced directly from the query using the view\&.
254 Functions called in the view are treated the same as if they had been called directly from the query using the view\&. Therefore, the user of a view must have permissions to call all functions used by the view\&. Functions in the view are executed with the privileges of the user executing the query or the function owner, depending on whether the functions are defined as
257 SECURITY DEFINER\&. Thus, for example, calling
259 directly in a view will always return the invoking user, not the view owner\&. This is not affected by the view\*(Aqs
261 setting, and so a view with
269 function and those concepts should not be confused\&.
271 The user creating or replacing a view must have
273 privileges on any schemas referred to in the view query, in order to look up the referenced objects in those schemas\&. Note, however, that this lookup only happens when the view is created or replaced\&. Therefore, the user of the view only requires the
275 privilege on the schema containing the view, not on the schemas referred to in the view query, even for a security invoker view\&.
278 \fBCREATE OR REPLACE VIEW\fR
279 is used on an existing view, only the view\*(Aqs defining SELECT rule, plus any
283 are changed\&. Other view properties, including ownership, permissions, and non\-SELECT rules, remain unchanged\&. You must own the view to replace it (this includes being a member of the owning role)\&.
284 .SS "Updatable Views"
286 Simple views are automatically updatable: the system will allow
291 statements to be used on the view in the same way as on a regular table\&. A view is automatically updatable if it satisfies all of the following conditions:
301 The view must have exactly one entry in its
303 list, which must be a table or another updatable view\&.
314 The view definition must not contain
321 clauses at the top level\&.
332 The view definition must not contain set operations (UNION,
335 EXCEPT) at the top level\&.
346 The view\*(Aqs select list must not contain any aggregates, window functions or set\-returning functions\&.
349 An automatically updatable view may contain a mix of updatable and non\-updatable columns\&. A column is updatable if it is a simple reference to an updatable column of the underlying base relation; otherwise the column is read\-only, and an error will be raised if an
353 statement attempts to assign a value to it\&.
355 If the view is automatically updatable the system will convert any
360 statement on the view into the corresponding statement on the underlying base relation\&.
362 statements that have an
364 clause are fully supported\&.
366 If an automatically updatable view contains a
368 condition, the condition restricts which rows of the base relation are available to be modified by
372 statements on the view\&. However, an
376 is allowed to change a row so that it no longer satisfies the
378 condition, and thus is no longer visible through the view\&. Similarly, an
382 command can potentially insert base\-relation rows that do not satisfy the
384 condition and thus are not visible through the view (ON CONFLICT UPDATE
385 may similarly affect an existing row not visible through the view)\&. The
387 may be used to prevent
391 commands from creating such rows that are not visible through the view\&.
393 If an automatically updatable view is marked with the
395 property then all the view\*(Aqs
397 conditions (and any conditions using operators which are marked as
398 LEAKPROOF) will always be evaluated before any conditions that a user of the view has added\&. See
400 for full details\&. Note that, due to this, rows which are not ultimately returned (because they do not pass the user\*(Aqs
402 conditions) may still end up being locked\&.
404 can be used to see which conditions are applied at the relation level (and therefore do not lock rows) and which are not\&.
406 A more complex view that does not satisfy all these conditions is read\-only by default: the system will not allow an
411 on the view\&. You can get the effect of an updatable view by creating
413 triggers on the view, which must convert attempted inserts, etc\&. on the view into appropriate actions on other tables\&. For more information see
414 CREATE TRIGGER (\fBCREATE_TRIGGER\fR(7))\&. Another possibility is to create rules (see
415 CREATE RULE (\fBCREATE_RULE\fR(7))), but in practice triggers are easier to understand and use correctly\&. Also note that
417 is not supported on relations with rules\&.
419 Note that the user performing the insert, update or delete on the view must have the corresponding insert, update or delete privilege on the view\&. In addition, by default, the view\*(Aqs owner must have the relevant privileges on the underlying base relations, whereas the user performing the update does not need any permissions on the underlying base relations (see
420 Section\ \&39.5)\&. However, if the view has
423 true, the user performing the update, rather than the view owner, must have the relevant privileges on the underlying base relations\&.
426 Create a view consisting of all comedy films:
432 CREATE VIEW comedies AS
435 WHERE kind = \*(AqComedy\*(Aq;
441 This will create a view containing the columns that are in the
443 table at the time of view creation\&. Though
445 was used to create the view, columns added later to the table will not be part of the view\&.
454 CREATE VIEW universal_comedies AS
457 WHERE classification = \*(AqU\*(Aq
458 WITH LOCAL CHECK OPTION;
464 This will create a view based on the
466 view, showing only films with
467 kind = \*(AqComedy\*(Aq
469 classification = \*(AqU\*(Aq\&. Any attempt to
473 a row in the view will be rejected if the new row doesn\*(Aqt have
474 classification = \*(AqU\*(Aq, but the film
476 will not be checked\&.
479 CASCADED CHECK OPTION:
485 CREATE VIEW pg_comedies AS
488 WHERE classification = \*(AqPG\*(Aq
489 WITH CASCADED CHECK OPTION;
495 This will create a view that checks both the
501 Create a view with a mix of updatable and non\-updatable columns:
507 CREATE VIEW comedies AS
509 country_code_to_name(f\&.country_code) AS country,
510 (SELECT avg(r\&.rating)
512 WHERE r\&.film_id = f\&.id) AS avg_rating
514 WHERE f\&.kind = \*(AqComedy\*(Aq;
520 This view will support
524 \fBDELETE\fR\&. All the columns from the
526 table will be updatable, whereas the computed columns
530 will be read\-only\&.
532 Create a recursive view consisting of the numbers from 1 to 100:
538 CREATE RECURSIVE VIEW public\&.nums_1_100 (n) AS
541 SELECT n+1 FROM nums_1_100 WHERE n < 100;
547 Notice that although the recursive view\*(Aqs name is schema\-qualified in this
548 \fBCREATE\fR, its internal self\-reference is not schema\-qualified\&. This is because the implicitly\-created CTE\*(Aqs name cannot be schema\-qualified\&.
551 \fBCREATE OR REPLACE VIEW\fR
554 language extension\&. So is the concept of a temporary view\&. The
556 clause is an extension as well, as are security barrier views and security invoker views\&.
558 ALTER VIEW (\fBALTER_VIEW\fR(7)), DROP VIEW (\fBDROP_VIEW\fR(7)), CREATE MATERIALIZED VIEW (\fBCREATE_MATERIALIZED_VIEW\fR(7))