<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CREATE VIEW</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.6.15 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE USER MAPPING" HREF="sql-createusermapping.html"><LINK REL="NEXT" TITLE="DEALLOCATE" HREF="sql-deallocate.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2019-08-11T16:06:51"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="4" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.6.15 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="CREATE USER MAPPING" HREF="sql-createusermapping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="DEALLOCATE" HREF="sql-deallocate.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CREATEVIEW" ></A >CREATE VIEW</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN84922" ></A ><H2 >Name</H2 >CREATE VIEW -- define a new view</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN84925" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column_name</I ></TT > [, ...] ) ] [ WITH ( <TT CLASS="REPLACEABLE" ><I >view_option_name</I ></TT > [= <TT CLASS="REPLACEABLE" ><I >view_option_value</I ></TT >] [, ... ] ) ] AS <TT CLASS="REPLACEABLE" ><I >query</I ></TT > [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN84932" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CREATE VIEW</TT > 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. </P ><P > <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > 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. </P ><P > If a schema name is given (for example, <TT CLASS="LITERAL" >CREATE VIEW myschema.myview ...</TT >) 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 view, table, sequence, index or foreign table in the same schema. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN84940" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >TEMPORARY</TT > or <TT CLASS="LITERAL" >TEMP</TT ></DT ><DD ><P > 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. </P ><P > If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether <TT CLASS="LITERAL" >TEMPORARY</TT > is specified or not). </P ></DD ><DT ><TT CLASS="LITERAL" >RECURSIVE</TT ></DT ><DD ><P > Creates a recursive view. The syntax </P><PRE CLASS="SYNOPSIS" >CREATE RECURSIVE VIEW [ <TT CLASS="REPLACEABLE" ><I >schema</I ></TT > . ] <TT CLASS="REPLACEABLE" ><I >view_name</I ></TT > (<TT CLASS="REPLACEABLE" ><I >column_names</I ></TT >) AS SELECT <TT CLASS="REPLACEABLE" ><I >...</I ></TT >;</PRE ><P> is equivalent to </P><PRE CLASS="SYNOPSIS" >CREATE VIEW [ <TT CLASS="REPLACEABLE" ><I >schema</I ></TT > . ] <TT CLASS="REPLACEABLE" ><I >view_name</I ></TT > AS WITH RECURSIVE <TT CLASS="REPLACEABLE" ><I >view_name</I ></TT > (<TT CLASS="REPLACEABLE" ><I >column_names</I ></TT >) AS (SELECT <TT CLASS="REPLACEABLE" ><I >...</I ></TT >) SELECT <TT CLASS="REPLACEABLE" ><I >column_names</I ></TT > FROM <TT CLASS="REPLACEABLE" ><I >view_name</I ></TT >;</PRE ><P> A view column name list must be specified for a recursive view. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >name</I ></TT ></DT ><DD ><P > The name (optionally schema-qualified) of a view to be created. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >column_name</I ></TT ></DT ><DD ><P > An optional list of names to be used for columns of the view. If not given, the column names are deduced from the query. </P ></DD ><DT ><TT CLASS="LITERAL" >WITH ( <TT CLASS="REPLACEABLE" ><I >view_option_name</I ></TT > [= <TT CLASS="REPLACEABLE" ><I >view_option_value</I ></TT >] [, ... ] )</TT ></DT ><DD ><P > This clause specifies optional parameters for a view; the following parameters are supported: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >check_option</TT > (<TT CLASS="TYPE" >string</TT >)</DT ><DD ><P > This parameter may be either <TT CLASS="LITERAL" >local</TT > or <TT CLASS="LITERAL" >cascaded</TT >, and is equivalent to specifying <TT CLASS="LITERAL" >WITH [ CASCADED | LOCAL ] CHECK OPTION</TT > (see below). This option can be changed on existing views using <A HREF="sql-alterview.html" >ALTER VIEW</A >. </P ></DD ><DT ><TT CLASS="LITERAL" >security_barrier</TT > (<TT CLASS="TYPE" >boolean</TT >)</DT ><DD ><P > This should be used if the view is intended to provide row-level security. See <A HREF="rules-privileges.html" >Section 39.5</A > for full details. </P ></DD ></DL ></DIV ><P> </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >query</I ></TT ></DT ><DD ><P > A <A HREF="sql-select.html" >SELECT</A > or <A HREF="sql-values.html" >VALUES</A > command which will provide the columns and rows of the view. </P ></DD ><DT ><TT CLASS="LITERAL" >WITH [ CASCADED | LOCAL ] CHECK OPTION</TT > </DT ><DD ><P > This option controls the behavior of automatically updatable views. When this option is specified, <TT CLASS="COMMAND" >INSERT</TT > and <TT CLASS="COMMAND" >UPDATE</TT > 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 <TT CLASS="LITERAL" >CHECK OPTION</TT > is not specified, <TT CLASS="COMMAND" >INSERT</TT > and <TT CLASS="COMMAND" >UPDATE</TT > commands on the view are allowed to create rows that are not visible through the view. The following check options are supported: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >LOCAL</TT ></DT ><DD ><P > 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 <TT CLASS="LITERAL" >CHECK OPTION</TT >). </P ></DD ><DT ><TT CLASS="LITERAL" >CASCADED</TT ></DT ><DD ><P > New rows are checked against the conditions of the view and all underlying base views. If the <TT CLASS="LITERAL" >CHECK OPTION</TT > is specified, and neither <TT CLASS="LITERAL" >LOCAL</TT > nor <TT CLASS="LITERAL" >CASCADED</TT > is specified, then <TT CLASS="LITERAL" >CASCADED</TT > is assumed. </P ></DD ></DL ></DIV ><P> </P ><P > The <TT CLASS="LITERAL" >CHECK OPTION</TT > may not be used with <TT CLASS="LITERAL" >RECURSIVE</TT > views. </P ><P > Note that the <TT CLASS="LITERAL" >CHECK OPTION</TT > is only supported on views that are automatically updatable, and do not have <TT CLASS="LITERAL" >INSTEAD OF</TT > triggers or <TT CLASS="LITERAL" >INSTEAD</TT > rules. If an automatically updatable view is defined on top of a base view that has <TT CLASS="LITERAL" >INSTEAD OF</TT > triggers, then the <TT CLASS="LITERAL" >LOCAL CHECK OPTION</TT > may be used to check the conditions on the automatically updatable view, but the conditions on the base view with <TT CLASS="LITERAL" >INSTEAD OF</TT > 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 <TT CLASS="LITERAL" >INSTEAD</TT > rule that causes the <TT CLASS="COMMAND" >INSERT</TT > or <TT CLASS="COMMAND" >UPDATE</TT > 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 <TT CLASS="LITERAL" >INSTEAD</TT > rule. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN85055" ></A ><H2 >Notes</H2 ><P > Use the <A HREF="sql-dropview.html" >DROP VIEW</A > statement to drop views. </P ><P > Be careful that the names and types of the view's columns will be assigned the way you want. For example: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW vista AS SELECT 'Hello World';</PRE ><P> is bad form in two ways: the column name defaults to <TT CLASS="LITERAL" >?column?</TT >, and the column data type defaults to <TT CLASS="TYPE" >unknown</TT >. If you want a string literal in a view's result, use something like: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW vista AS SELECT text 'Hello World' AS hello;</PRE ><P> </P ><P > Access to tables referenced in the view is determined by 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 <A HREF="rules-privileges.html" >Section 39.5</A > for details. 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. </P ><P > When <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > is used on an existing view, only the view's defining SELECT rule is 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). </P ><DIV CLASS="REFSECT2" ><A NAME="SQL-CREATEVIEW-UPDATABLE-VIEWS" ></A ><H3 >Updatable Views</H3 ><P > Simple views are automatically updatable: the system will allow <TT CLASS="COMMAND" >INSERT</TT >, <TT CLASS="COMMAND" >UPDATE</TT > and <TT CLASS="COMMAND" >DELETE</TT > 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: <P ></P ></P><UL ><LI ><P > The view must have exactly one entry in its <TT CLASS="LITERAL" >FROM</TT > list, which must be a table or another updatable view. </P ></LI ><LI ><P > The view definition must not contain <TT CLASS="LITERAL" >WITH</TT >, <TT CLASS="LITERAL" >DISTINCT</TT >, <TT CLASS="LITERAL" >GROUP BY</TT >, <TT CLASS="LITERAL" >HAVING</TT >, <TT CLASS="LITERAL" >LIMIT</TT >, or <TT CLASS="LITERAL" >OFFSET</TT > clauses at the top level. </P ></LI ><LI ><P > The view definition must not contain set operations (<TT CLASS="LITERAL" >UNION</TT >, <TT CLASS="LITERAL" >INTERSECT</TT > or <TT CLASS="LITERAL" >EXCEPT</TT >) at the top level. </P ></LI ><LI ><P > The view's select list must not contain any aggregates, window functions or set-returning functions. </P ></LI ></UL ><P> </P ><P > 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 <TT CLASS="COMMAND" >INSERT</TT > or <TT CLASS="COMMAND" >UPDATE</TT > statement attempts to assign a value to it. </P ><P > If the view is automatically updatable the system will convert any <TT CLASS="COMMAND" >INSERT</TT >, <TT CLASS="COMMAND" >UPDATE</TT > or <TT CLASS="COMMAND" >DELETE</TT > statement on the view into the corresponding statement on the underlying base relation. <TT CLASS="COMMAND" >INSERT</TT > statements that have an <TT CLASS="LITERAL" >ON CONFLICT UPDATE</TT > clause are fully supported. </P ><P > If an automatically updatable view contains a <TT CLASS="LITERAL" >WHERE</TT > condition, the condition restricts which rows of the base relation are available to be modified by <TT CLASS="COMMAND" >UPDATE</TT > and <TT CLASS="COMMAND" >DELETE</TT > statements on the view. However, an <TT CLASS="COMMAND" >UPDATE</TT > is allowed to change a row so that it no longer satisfies the <TT CLASS="LITERAL" >WHERE</TT > condition, and thus is no longer visible through the view. Similarly, an <TT CLASS="COMMAND" >INSERT</TT > command can potentially insert base-relation rows that do not satisfy the <TT CLASS="LITERAL" >WHERE</TT > condition and thus are not visible through the view (<TT CLASS="LITERAL" >ON CONFLICT UPDATE</TT > may similarly affect an existing row not visible through the view). The <TT CLASS="LITERAL" >CHECK OPTION</TT > may be used to prevent <TT CLASS="COMMAND" >INSERT</TT > and <TT CLASS="COMMAND" >UPDATE</TT > commands from creating such rows that are not visible through the view. </P ><P > If an automatically updatable view is marked with the <TT CLASS="LITERAL" >security_barrier</TT > property then all the view's <TT CLASS="LITERAL" >WHERE</TT > conditions (and any conditions using operators which are marked as <TT CLASS="LITERAL" >LEAKPROOF</TT >) will always be evaluated before any conditions that a user of the view has added. See <A HREF="rules-privileges.html" >Section 39.5</A > for full details. Note that, due to this, rows which are not ultimately returned (because they do not pass the user's <TT CLASS="LITERAL" >WHERE</TT > conditions) may still end up being locked. <TT CLASS="COMMAND" >EXPLAIN</TT > can be used to see which conditions are applied at the relation level (and therefore do not lock rows) and which are not. </P ><P > A more complex view that does not satisfy all these conditions is read-only by default: the system will not allow an insert, update, or delete on the view. You can get the effect of an updatable view by creating <TT CLASS="LITERAL" >INSTEAD OF</TT > triggers on the view, which must convert attempted inserts, etc. on the view into appropriate actions on other tables. For more information see <A HREF="sql-createtrigger.html" >CREATE TRIGGER</A >. Another possibility is to create rules (see <A HREF="sql-createrule.html" >CREATE RULE</A >), but in practice triggers are easier to understand and use correctly. </P ><P > 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 the view's owner must have the relevant privileges on the underlying base relations, but the user performing the update does not need any permissions on the underlying base relations (see <A HREF="rules-privileges.html" >Section 39.5</A >). </P ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN85129" ></A ><H2 >Examples</H2 ><P > Create a view consisting of all comedy films: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';</PRE ><P> This will create a view containing the columns that are in the <TT CLASS="LITERAL" >film</TT > table at the time of view creation. Though <TT CLASS="LITERAL" >*</TT > was used to create the view, columns added later to the table will not be part of the view. </P ><P > Create a view with <TT CLASS="LITERAL" >LOCAL CHECK OPTION</TT >: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW universal_comedies AS SELECT * FROM comedies WHERE classification = 'U' WITH LOCAL CHECK OPTION;</PRE ><P> This will create a view based on the <TT CLASS="LITERAL" >comedies</TT > view, showing only films with <TT CLASS="LITERAL" >kind = 'Comedy'</TT > and <TT CLASS="LITERAL" >classification = 'U'</TT >. Any attempt to <TT CLASS="COMMAND" >INSERT</TT > or <TT CLASS="COMMAND" >UPDATE</TT > a row in the view will be rejected if the new row doesn't have <TT CLASS="LITERAL" >classification = 'U'</TT >, but the film <TT CLASS="LITERAL" >kind</TT > will not be checked. </P ><P > Create a view with <TT CLASS="LITERAL" >CASCADED CHECK OPTION</TT >: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW pg_comedies AS SELECT * FROM comedies WHERE classification = 'PG' WITH CASCADED CHECK OPTION;</PRE ><P> This will create a view that checks both the <TT CLASS="LITERAL" >kind</TT > and <TT CLASS="LITERAL" >classification</TT > of new rows. </P ><P > Create a view with a mix of updatable and non-updatable columns: </P><PRE CLASS="PROGRAMLISTING" >CREATE VIEW comedies AS SELECT f.*, country_code_to_name(f.country_code) AS country, (SELECT avg(r.rating) FROM user_ratings r WHERE r.film_id = f.id) AS avg_rating FROM films f WHERE f.kind = 'Comedy';</PRE ><P> This view will support <TT CLASS="COMMAND" >INSERT</TT >, <TT CLASS="COMMAND" >UPDATE</TT > and <TT CLASS="COMMAND" >DELETE</TT >. All the columns from the <TT CLASS="LITERAL" >films</TT > table will be updatable, whereas the computed columns <TT CLASS="LITERAL" >country</TT > and <TT CLASS="LITERAL" >avg_rating</TT > will be read-only. </P ><P > Create a recursive view consisting of the numbers from 1 to 100: </P><PRE CLASS="PROGRAMLISTING" >CREATE RECURSIVE VIEW public.nums_1_100 (n) AS VALUES (1) UNION ALL SELECT n+1 FROM nums_1_100 WHERE n < 100;</PRE ><P> Notice that although the recursive view's name is schema-qualified in this <TT CLASS="COMMAND" >CREATE</TT >, its internal self-reference is not schema-qualified. This is because the implicitly-created CTE's name cannot be schema-qualified. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN85161" ></A ><H2 >Compatibility</H2 ><P > <TT CLASS="COMMAND" >CREATE OR REPLACE VIEW</TT > is a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > language extension. So is the concept of a temporary view. The <TT CLASS="LITERAL" >WITH ( ... )</TT > clause is an extension as well. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN85167" ></A ><H2 >See Also</H2 ><A HREF="sql-alterview.html" >ALTER VIEW</A >, <A HREF="sql-dropview.html" >DROP VIEW</A >, <A HREF="sql-creatematerializedview.html" >CREATE MATERIALIZED VIEW</A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-createusermapping.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-deallocate.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CREATE USER MAPPING</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >DEALLOCATE</TD ></TR ></TABLE ></DIV ></BODY ></HTML >