4 CREATE VIEW — define a new view
8 CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( column_na
10 [ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
12 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
16 CREATE VIEW defines a view of a query. The view is not physically
17 materialized. Instead, the query is run every time the view is
18 referenced in a query.
20 CREATE OR REPLACE VIEW is similar, but if a view of the same name
21 already exists, it is replaced. The new query must generate the same
22 columns that were generated by the existing view query (that is, the
23 same column names in the same order and with the same data types), but
24 it may add additional columns to the end of the list. The calculations
25 giving rise to the output columns may be completely different.
27 If a schema name is given (for example, CREATE VIEW myschema.myview
28 ...) then the view is created in the specified schema. Otherwise it is
29 created in the current schema. Temporary views exist in a special
30 schema, so a schema name cannot be given when creating a temporary
31 view. The name of the view must be distinct from the name of any other
32 relation (table, sequence, index, view, materialized view, or foreign
33 table) in the same schema.
38 If specified, the view is created as a temporary view. Temporary
39 views are automatically dropped at the end of the current
40 session. Existing permanent relations with the same name are not
41 visible to the current session while the temporary view exists,
42 unless they are referenced with schema-qualified names.
44 If any of the tables referenced by the view are temporary, the
45 view is created as a temporary view (whether TEMPORARY is
49 Creates a recursive view. The syntax
51 CREATE RECURSIVE VIEW [ schema . ] view_name (column_names) AS SELECT ...;
55 CREATE VIEW [ schema . ] view_name AS WITH RECURSIVE view_name (column_names) AS
56 (SELECT ...) SELECT column_names FROM view_name;
58 A view column name list must be specified for a recursive view.
61 The name (optionally schema-qualified) of a view to be created.
64 An optional list of names to be used for columns of the view. If
65 not given, the column names are deduced from the query.
67 WITH ( view_option_name [= view_option_value] [, ... ] )
68 This clause specifies optional parameters for a view; the
69 following parameters are supported:
72 This parameter may be either local or cascaded, and is
73 equivalent to specifying WITH [ CASCADED | LOCAL ] CHECK
76 security_barrier (boolean)
77 This should be used if the view is intended to provide
78 row-level security. See Section 39.5 for full details.
80 security_invoker (boolean)
81 This option causes the underlying base relations to be
82 checked against the privileges of the user of the view
83 rather than the view owner. See the notes below for full
86 All of the above options can be changed on existing views using
90 A SELECT or VALUES command which will provide the columns and
93 WITH [ CASCADED | LOCAL ] CHECK OPTION
94 This option controls the behavior of automatically updatable
95 views. When this option is specified, INSERT, UPDATE, and MERGE
96 commands on the view will be checked to ensure that new rows
97 satisfy the view-defining condition (that is, the new rows are
98 checked to ensure that they are visible through the view). If
99 they are not, the update will be rejected. If the CHECK OPTION
100 is not specified, INSERT, UPDATE, and MERGE commands on the view
101 are allowed to create rows that are not visible through the
102 view. The following check options are supported:
105 New rows are only checked against the conditions defined
106 directly in the view itself. Any conditions defined on
107 underlying base views are not checked (unless they also
108 specify the CHECK OPTION).
111 New rows are checked against the conditions of the view
112 and all underlying base views. If the CHECK OPTION is
113 specified, and neither LOCAL nor CASCADED is specified,
114 then CASCADED is assumed.
116 The CHECK OPTION may not be used with RECURSIVE views.
118 Note that the CHECK OPTION is only supported on views that are
119 automatically updatable, and do not have INSTEAD OF triggers or
120 INSTEAD rules. If an automatically updatable view is defined on
121 top of a base view that has INSTEAD OF triggers, then the LOCAL
122 CHECK OPTION may be used to check the conditions on the
123 automatically updatable view, but the conditions on the base
124 view with INSTEAD OF triggers will not be checked (a cascaded
125 check option will not cascade down to a trigger-updatable view,
126 and any check options defined directly on a trigger-updatable
127 view will be ignored). If the view or any of its base relations
128 has an INSTEAD rule that causes the INSERT or UPDATE command to
129 be rewritten, then all check options will be ignored in the
130 rewritten query, including any checks from automatically
131 updatable views defined on top of the relation with the INSTEAD
132 rule. MERGE is not supported if the view or any of its base
133 relations have rules.
137 Use the DROP VIEW statement to drop views.
139 Be careful that the names and types of the view's columns will be
140 assigned the way you want. For example:
141 CREATE VIEW vista AS SELECT 'Hello World';
143 is bad form because the column name defaults to ?column?; also, the
144 column data type defaults to text, which might not be what you wanted.
145 Better style for a string literal in a view's result is something like:
146 CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
148 By default, access to the underlying base relations referenced in the
149 view is determined by the permissions of the view owner. In some cases,
150 this can be used to provide secure but restricted access to the
151 underlying tables. However, not all views are secure against tampering;
152 see Section 39.5 for details.
154 If the view has the security_invoker property set to true, access to
155 the underlying base relations is determined by the permissions of the
156 user executing the query, rather than the view owner. Thus, the user of
157 a security invoker view must have the relevant permissions on the view
158 and its underlying base relations.
160 If any of the underlying base relations is a security invoker view, it
161 will be treated as if it had been accessed directly from the original
162 query. Thus, a security invoker view will always check its underlying
163 base relations using the permissions of the current user, even if it is
164 accessed from a view without the security_invoker property.
166 If any of the underlying base relations has row-level security enabled,
167 then by default, the row-level security policies of the view owner are
168 applied, and access to any additional relations referred to by those
169 policies is determined by the permissions of the view owner. However,
170 if the view has security_invoker set to true, then the policies and
171 permissions of the invoking user are used instead, as if the base
172 relations had been referenced directly from the query using the view.
174 Functions called in the view are treated the same as if they had been
175 called directly from the query using the view. Therefore, the user of a
176 view must have permissions to call all functions used by the view.
177 Functions in the view are executed with the privileges of the user
178 executing the query or the function owner, depending on whether the
179 functions are defined as SECURITY INVOKER or SECURITY DEFINER. Thus,
180 for example, calling CURRENT_USER directly in a view will always return
181 the invoking user, not the view owner. This is not affected by the
182 view's security_invoker setting, and so a view with security_invoker
183 set to false is not equivalent to a SECURITY DEFINER function and those
184 concepts should not be confused.
186 The user creating or replacing a view must have USAGE privileges on any
187 schemas referred to in the view query, in order to look up the
188 referenced objects in those schemas. Note, however, that this lookup
189 only happens when the view is created or replaced. Therefore, the user
190 of the view only requires the USAGE privilege on the schema containing
191 the view, not on the schemas referred to in the view query, even for a
192 security invoker view.
194 When CREATE OR REPLACE VIEW is used on an existing view, only the
195 view's defining SELECT rule, plus any WITH ( ... ) parameters and its
196 CHECK OPTION are changed. Other view properties, including ownership,
197 permissions, and non-SELECT rules, remain unchanged. You must own the
198 view to replace it (this includes being a member of the owning role).
202 Simple views are automatically updatable: the system will allow INSERT,
203 UPDATE, DELETE, and MERGE statements to be used on the view in the same
204 way as on a regular table. A view is automatically updatable if it
205 satisfies all of the following conditions:
206 * The view must have exactly one entry in its FROM list, which must
207 be a table or another updatable view.
208 * The view definition must not contain WITH, DISTINCT, GROUP BY,
209 HAVING, LIMIT, or OFFSET clauses at the top level.
210 * The view definition must not contain set operations (UNION,
211 INTERSECT or EXCEPT) at the top level.
212 * The view's select list must not contain any aggregates, window
213 functions or set-returning functions.
215 An automatically updatable view may contain a mix of updatable and
216 non-updatable columns. A column is updatable if it is a simple
217 reference to an updatable column of the underlying base relation;
218 otherwise the column is read-only, and an error will be raised if an
219 INSERT, UPDATE, or MERGE statement attempts to assign a value to it.
221 If the view is automatically updatable the system will convert any
222 INSERT, UPDATE, DELETE, or MERGE statement on the view into the
223 corresponding statement on the underlying base relation. INSERT
224 statements that have an ON CONFLICT UPDATE clause are fully supported.
226 If an automatically updatable view contains a WHERE condition, the
227 condition restricts which rows of the base relation are available to be
228 modified by UPDATE, DELETE, and MERGE statements on the view. However,
229 an UPDATE or MERGE is allowed to change a row so that it no longer
230 satisfies the WHERE condition, and thus is no longer visible through
231 the view. Similarly, an INSERT or MERGE command can potentially insert
232 base-relation rows that do not satisfy the WHERE condition and thus are
233 not visible through the view (ON CONFLICT UPDATE may similarly affect
234 an existing row not visible through the view). The CHECK OPTION may be
235 used to prevent INSERT, UPDATE, and MERGE commands from creating such
236 rows that are not visible through the view.
238 If an automatically updatable view is marked with the security_barrier
239 property then all the view's WHERE conditions (and any conditions using
240 operators which are marked as LEAKPROOF) will always be evaluated
241 before any conditions that a user of the view has added. See
242 Section 39.5 for full details. Note that, due to this, rows which are
243 not ultimately returned (because they do not pass the user's WHERE
244 conditions) may still end up being locked. EXPLAIN can be used to see
245 which conditions are applied at the relation level (and therefore do
246 not lock rows) and which are not.
248 A more complex view that does not satisfy all these conditions is
249 read-only by default: the system will not allow an INSERT, UPDATE,
250 DELETE, or MERGE on the view. You can get the effect of an updatable
251 view by creating INSTEAD OF triggers on the view, which must convert
252 attempted inserts, etc. on the view into appropriate actions on other
253 tables. For more information see CREATE TRIGGER. Another possibility is
254 to create rules (see CREATE RULE), but in practice triggers are easier
255 to understand and use correctly. Also note that MERGE is not supported
256 on relations with rules.
258 Note that the user performing the insert, update or delete on the view
259 must have the corresponding insert, update or delete privilege on the
260 view. In addition, by default, the view's owner must have the relevant
261 privileges on the underlying base relations, whereas the user
262 performing the update does not need any permissions on the underlying
263 base relations (see Section 39.5). However, if the view has
264 security_invoker set to true, the user performing the update, rather
265 than the view owner, must have the relevant privileges on the
266 underlying base relations.
270 Create a view consisting of all comedy films:
271 CREATE VIEW comedies AS
274 WHERE kind = 'Comedy';
276 This will create a view containing the columns that are in the film
277 table at the time of view creation. Though * was used to create the
278 view, columns added later to the table will not be part of the view.
280 Create a view with LOCAL CHECK OPTION:
281 CREATE VIEW universal_comedies AS
284 WHERE classification = 'U'
285 WITH LOCAL CHECK OPTION;
287 This will create a view based on the comedies view, showing only films
288 with kind = 'Comedy' and classification = 'U'. Any attempt to INSERT or
289 UPDATE a row in the view will be rejected if the new row doesn't have
290 classification = 'U', but the film kind will not be checked.
292 Create a view with CASCADED CHECK OPTION:
293 CREATE VIEW pg_comedies AS
296 WHERE classification = 'PG'
297 WITH CASCADED CHECK OPTION;
299 This will create a view that checks both the kind and classification of
302 Create a view with a mix of updatable and non-updatable columns:
303 CREATE VIEW comedies AS
305 country_code_to_name(f.country_code) AS country,
306 (SELECT avg(r.rating)
308 WHERE r.film_id = f.id) AS avg_rating
310 WHERE f.kind = 'Comedy';
312 This view will support INSERT, UPDATE and DELETE. All the columns from
313 the films table will be updatable, whereas the computed columns country
314 and avg_rating will be read-only.
316 Create a recursive view consisting of the numbers from 1 to 100:
317 CREATE RECURSIVE VIEW public.nums_1_100 (n) AS
320 SELECT n+1 FROM nums_1_100 WHERE n < 100;
322 Notice that although the recursive view's name is schema-qualified in
323 this CREATE, its internal self-reference is not schema-qualified. This
324 is because the implicitly-created CTE's name cannot be
329 CREATE OR REPLACE VIEW is a PostgreSQL language extension. So is the
330 concept of a temporary view. The WITH ( ... ) clause is an extension as
331 well, as are security barrier views and security invoker views.
335 ALTER VIEW, DROP VIEW, CREATE MATERIALIZED VIEW