]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_VIEW.7
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / man7 / CREATE_VIEW.7
1 '\" t
2 .\"     Title: CREATE VIEW
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_VIEW \- define a new view
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW \fIname\fR [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
36     [ WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] ) ]
37     AS \fIquery\fR
38     [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
39 .fi
40 .SH "DESCRIPTION"
41 .PP
42 \fBCREATE VIEW\fR
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\&.
44 .PP
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\&.
47 .PP
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\&.
50 .SH "PARAMETERS"
51 .PP
52 TEMPORARY or TEMP
53 .RS 4
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\&.
55 .sp
56 If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether
57 TEMPORARY
58 is specified or not)\&.
59 .RE
60 .PP
61 RECURSIVE
62 .RS 4
63 Creates a recursive view\&. The syntax
64 .sp
65 .if n \{\
66 .RS 4
67 .\}
68 .nf
69 CREATE RECURSIVE VIEW [ \fIschema\fR \&. ] \fIview_name\fR (\fIcolumn_names\fR) AS SELECT \fI\&.\&.\&.\fR;
70 .fi
71 .if n \{\
72 .RE
73 .\}
74 .sp
75 is equivalent to
76 .sp
77 .if n \{\
78 .RS 4
79 .\}
80 .nf
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;
82 .fi
83 .if n \{\
84 .RE
85 .\}
86 .sp
87 A view column name list must be specified for a recursive view\&.
88 .RE
89 .PP
90 \fIname\fR
91 .RS 4
92 The name (optionally schema\-qualified) of a view to be created\&.
93 .RE
94 .PP
95 \fIcolumn_name\fR
96 .RS 4
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\&.
98 .RE
99 .PP
100 WITH ( \fIview_option_name\fR [= \fIview_option_value\fR] [, \&.\&.\&. ] )
101 .RS 4
102 This clause specifies optional parameters for a view; the following parameters are supported:
103 .PP
104 check_option (enum)
105 .RS 4
106 This parameter may be either
107 local
108 or
109 cascaded, and is equivalent to specifying
110 WITH [ CASCADED | LOCAL ] CHECK OPTION
111 (see below)\&.
112 .RE
113 .PP
114 security_barrier (boolean)
115 .RS 4
116 This should be used if the view is intended to provide row\-level security\&. See
117 Section\ \&39.5
118 for full details\&.
119 .RE
120 .PP
121 security_invoker (boolean)
122 .RS 4
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\&.
124 .RE
125 .sp
126 All of the above options can be changed on existing views using
127 \fBALTER VIEW\fR\&.
128 .RE
129 .PP
130 \fIquery\fR
131 .RS 4
132 A
133 \fBSELECT\fR
134 or
135 \fBVALUES\fR
136 command which will provide the columns and rows of the view\&.
137 .RE
138 .PP
139 WITH [ CASCADED | LOCAL ] CHECK OPTION
140 .RS 4
141 This option controls the behavior of automatically updatable views\&. When this option is specified,
142 \fBINSERT\fR,
143 \fBUPDATE\fR, and
144 \fBMERGE\fR
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
146 CHECK OPTION
147 is not specified,
148 \fBINSERT\fR,
149 \fBUPDATE\fR, and
150 \fBMERGE\fR
151 commands on the view are allowed to create rows that are not visible through the view\&. The following check options are supported:
152 .PP
153 LOCAL
154 .RS 4
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
156 CHECK OPTION)\&.
157 .RE
158 .PP
159 CASCADED
160 .RS 4
161 New rows are checked against the conditions of the view and all underlying base views\&. If the
162 CHECK OPTION
163 is specified, and neither
164 LOCAL
165 nor
166 CASCADED
167 is specified, then
168 CASCADED
169 is assumed\&.
170 .RE
171 .sp
172 The
173 CHECK OPTION
174 may not be used with
175 RECURSIVE
176 views\&.
177 .sp
178 Note that the
179 CHECK OPTION
180 is only supported on views that are automatically updatable, and do not have
181 INSTEAD OF
182 triggers or
183 INSTEAD
184 rules\&. If an automatically updatable view is defined on top of a base view that has
185 INSTEAD OF
186 triggers, then the
187 LOCAL CHECK OPTION
188 may be used to check the conditions on the automatically updatable view, but the conditions on the base view with
189 INSTEAD OF
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
191 INSTEAD
192 rule that causes the
193 \fBINSERT\fR
194 or
195 \fBUPDATE\fR
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
197 INSTEAD
198 rule\&.
199 \fBMERGE\fR
200 is not supported if the view or any of its base relations have rules\&.
201 .RE
202 .SH "NOTES"
203 .PP
204 Use the
205 \fBDROP VIEW\fR
206 statement to drop views\&.
207 .PP
208 Be careful that the names and types of the view\*(Aqs columns will be assigned the way you want\&. For example:
209 .sp
210 .if n \{\
211 .RS 4
212 .\}
213 .nf
214 CREATE VIEW vista AS SELECT \*(AqHello World\*(Aq;
215 .fi
216 .if n \{\
217 .RE
218 .\}
219 .sp
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:
223 .sp
224 .if n \{\
225 .RS 4
226 .\}
227 .nf
228 CREATE VIEW vista AS SELECT text \*(AqHello World\*(Aq AS hello;
229 .fi
230 .if n \{\
231 .RE
232 .\}
233 .PP
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
235 Section\ \&39.5
236 for details\&.
237 .PP
238 If the view has the
239 security_invoker
240 property set to
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\&.
242 .PP
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
244 security_invoker
245 property\&.
246 .PP
247 If any of the underlying base relations has
248 row\-level security
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
250 security_invoker
251 set to
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\&.
253 .PP
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
255 SECURITY INVOKER
256 or
257 SECURITY DEFINER\&. Thus, for example, calling
258 CURRENT_USER
259 directly in a view will always return the invoking user, not the view owner\&. This is not affected by the view\*(Aqs
260 security_invoker
261 setting, and so a view with
262 security_invoker
263 set to
264 false
265 is
266 \fInot\fR
267 equivalent to a
268 SECURITY DEFINER
269 function and those concepts should not be confused\&.
270 .PP
271 The user creating or replacing a view must have
272 USAGE
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
274 USAGE
275 privilege on the schema containing the view, not on the schemas referred to in the view query, even for a security invoker view\&.
276 .PP
277 When
278 \fBCREATE OR REPLACE VIEW\fR
279 is used on an existing view, only the view\*(Aqs defining SELECT rule, plus any
280 WITH ( \&.\&.\&. )
281 parameters and its
282 CHECK OPTION
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"
285 .PP
286 Simple views are automatically updatable: the system will allow
287 \fBINSERT\fR,
288 \fBUPDATE\fR,
289 \fBDELETE\fR, and
290 \fBMERGE\fR
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:
292 .sp
293 .RS 4
294 .ie n \{\
295 \h'-04'\(bu\h'+03'\c
296 .\}
297 .el \{\
298 .sp -1
299 .IP \(bu 2.3
300 .\}
301 The view must have exactly one entry in its
302 FROM
303 list, which must be a table or another updatable view\&.
304 .RE
305 .sp
306 .RS 4
307 .ie n \{\
308 \h'-04'\(bu\h'+03'\c
309 .\}
310 .el \{\
311 .sp -1
312 .IP \(bu 2.3
313 .\}
314 The view definition must not contain
315 WITH,
316 DISTINCT,
317 GROUP BY,
318 HAVING,
319 LIMIT, or
320 OFFSET
321 clauses at the top level\&.
322 .RE
323 .sp
324 .RS 4
325 .ie n \{\
326 \h'-04'\(bu\h'+03'\c
327 .\}
328 .el \{\
329 .sp -1
330 .IP \(bu 2.3
331 .\}
332 The view definition must not contain set operations (UNION,
333 INTERSECT
334 or
335 EXCEPT) at the top level\&.
336 .RE
337 .sp
338 .RS 4
339 .ie n \{\
340 \h'-04'\(bu\h'+03'\c
341 .\}
342 .el \{\
343 .sp -1
344 .IP \(bu 2.3
345 .\}
346 The view\*(Aqs select list must not contain any aggregates, window functions or set\-returning functions\&.
347 .RE
348 .PP
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
350 \fBINSERT\fR,
351 \fBUPDATE\fR, or
352 \fBMERGE\fR
353 statement attempts to assign a value to it\&.
354 .PP
355 If the view is automatically updatable the system will convert any
356 \fBINSERT\fR,
357 \fBUPDATE\fR,
358 \fBDELETE\fR, or
359 \fBMERGE\fR
360 statement on the view into the corresponding statement on the underlying base relation\&.
361 \fBINSERT\fR
362 statements that have an
363 ON CONFLICT UPDATE
364 clause are fully supported\&.
365 .PP
366 If an automatically updatable view contains a
367 WHERE
368 condition, the condition restricts which rows of the base relation are available to be modified by
369 \fBUPDATE\fR,
370 \fBDELETE\fR, and
371 \fBMERGE\fR
372 statements on the view\&. However, an
373 \fBUPDATE\fR
374 or
375 \fBMERGE\fR
376 is allowed to change a row so that it no longer satisfies the
377 WHERE
378 condition, and thus is no longer visible through the view\&. Similarly, an
379 \fBINSERT\fR
380 or
381 \fBMERGE\fR
382 command can potentially insert base\-relation rows that do not satisfy the
383 WHERE
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
386 CHECK OPTION
387 may be used to prevent
388 \fBINSERT\fR,
389 \fBUPDATE\fR, and
390 \fBMERGE\fR
391 commands from creating such rows that are not visible through the view\&.
392 .PP
393 If an automatically updatable view is marked with the
394 security_barrier
395 property then all the view\*(Aqs
396 WHERE
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
399 Section\ \&39.5
400 for full details\&. Note that, due to this, rows which are not ultimately returned (because they do not pass the user\*(Aqs
401 WHERE
402 conditions) may still end up being locked\&.
403 \fBEXPLAIN\fR
404 can be used to see which conditions are applied at the relation level (and therefore do not lock rows) and which are not\&.
405 .PP
406 A more complex view that does not satisfy all these conditions is read\-only by default: the system will not allow an
407 \fBINSERT\fR,
408 \fBUPDATE\fR,
409 \fBDELETE\fR, or
410 \fBMERGE\fR
411 on the view\&. You can get the effect of an updatable view by creating
412 INSTEAD OF
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
416 \fBMERGE\fR
417 is not supported on relations with rules\&.
418 .PP
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
421 security_invoker
422 set to
423 true, the user performing the update, rather than the view owner, must have the relevant privileges on the underlying base relations\&.
424 .SH "EXAMPLES"
425 .PP
426 Create a view consisting of all comedy films:
427 .sp
428 .if n \{\
429 .RS 4
430 .\}
431 .nf
432 CREATE VIEW comedies AS
433     SELECT *
434     FROM films
435     WHERE kind = \*(AqComedy\*(Aq;
436 .fi
437 .if n \{\
438 .RE
439 .\}
440 .sp
441 This will create a view containing the columns that are in the
442 film
443 table at the time of view creation\&. Though
444 *
445 was used to create the view, columns added later to the table will not be part of the view\&.
446 .PP
447 Create a view with
448 LOCAL CHECK OPTION:
449 .sp
450 .if n \{\
451 .RS 4
452 .\}
453 .nf
454 CREATE VIEW universal_comedies AS
455     SELECT *
456     FROM comedies
457     WHERE classification = \*(AqU\*(Aq
458     WITH LOCAL CHECK OPTION;
459 .fi
460 .if n \{\
461 .RE
462 .\}
463 .sp
464 This will create a view based on the
465 comedies
466 view, showing only films with
467 kind = \*(AqComedy\*(Aq
468 and
469 classification = \*(AqU\*(Aq\&. Any attempt to
470 \fBINSERT\fR
471 or
472 \fBUPDATE\fR
473 a row in the view will be rejected if the new row doesn\*(Aqt have
474 classification = \*(AqU\*(Aq, but the film
475 kind
476 will not be checked\&.
477 .PP
478 Create a view with
479 CASCADED CHECK OPTION:
480 .sp
481 .if n \{\
482 .RS 4
483 .\}
484 .nf
485 CREATE VIEW pg_comedies AS
486     SELECT *
487     FROM comedies
488     WHERE classification = \*(AqPG\*(Aq
489     WITH CASCADED CHECK OPTION;
490 .fi
491 .if n \{\
492 .RE
493 .\}
494 .sp
495 This will create a view that checks both the
496 kind
497 and
498 classification
499 of new rows\&.
500 .PP
501 Create a view with a mix of updatable and non\-updatable columns:
502 .sp
503 .if n \{\
504 .RS 4
505 .\}
506 .nf
507 CREATE VIEW comedies AS
508     SELECT f\&.*,
509            country_code_to_name(f\&.country_code) AS country,
510            (SELECT avg(r\&.rating)
511             FROM user_ratings r
512             WHERE r\&.film_id = f\&.id) AS avg_rating
513     FROM films f
514     WHERE f\&.kind = \*(AqComedy\*(Aq;
515 .fi
516 .if n \{\
517 .RE
518 .\}
519 .sp
520 This view will support
521 \fBINSERT\fR,
522 \fBUPDATE\fR
523 and
524 \fBDELETE\fR\&. All the columns from the
525 films
526 table will be updatable, whereas the computed columns
527 country
528 and
529 avg_rating
530 will be read\-only\&.
531 .PP
532 Create a recursive view consisting of the numbers from 1 to 100:
533 .sp
534 .if n \{\
535 .RS 4
536 .\}
537 .nf
538 CREATE RECURSIVE VIEW public\&.nums_1_100 (n) AS
539     VALUES (1)
540 UNION ALL
541     SELECT n+1 FROM nums_1_100 WHERE n < 100;
542 .fi
543 .if n \{\
544 .RE
545 .\}
546 .sp
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\&.
549 .SH "COMPATIBILITY"
550 .PP
551 \fBCREATE OR REPLACE VIEW\fR
552 is a
553 PostgreSQL
554 language extension\&. So is the concept of a temporary view\&. The
555 WITH ( \&.\&.\&. )
556 clause is an extension as well, as are security barrier views and security invoker views\&.
557 .SH "SEE ALSO"
558 ALTER VIEW (\fBALTER_VIEW\fR(7)), DROP VIEW (\fBDROP_VIEW\fR(7)), CREATE MATERIALIZED VIEW (\fBCREATE_MATERIALIZED_VIEW\fR(7))