1<!--
2doc/src/sgml/ref/alter_view.sgml
3PostgreSQL documentation
4-->
5
6<refentry id="sql-alterview">
7 <indexterm zone="sql-alterview">
8  <primary>ALTER VIEW</primary>
9 </indexterm>
10
11 <refmeta>
12  <refentrytitle>ALTER VIEW</refentrytitle>
13  <manvolnum>7</manvolnum>
14  <refmiscinfo>SQL - Language Statements</refmiscinfo>
15 </refmeta>
16
17 <refnamediv>
18  <refname>ALTER VIEW</refname>
19  <refpurpose>change the definition of a view</refpurpose>
20 </refnamediv>
21
22 <refsynopsisdiv>
23<synopsis>
24ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET DEFAULT <replaceable class="parameter">expression</replaceable>
25ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> DROP DEFAULT
26ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> OWNER TO { <replaceable class="parameter">new_owner</replaceable> | CURRENT_USER | SESSION_USER }
27ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RENAME [ COLUMN ] <replaceable class="parameter">column_name</replaceable> TO <replaceable class="parameter">new_column_name</replaceable>
28ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
29ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
30ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )
31ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )
32</synopsis>
33 </refsynopsisdiv>
34
35 <refsect1>
36  <title>Description</title>
37
38  <para>
39   <command>ALTER VIEW</command> changes various auxiliary properties
40   of a view.  (If you want to modify the view's defining query,
41   use <command>CREATE OR REPLACE VIEW</command>.)
42  </para>
43
44  <para>
45   You must own the view to use <command>ALTER VIEW</command>.
46   To change a view's schema, you must also have <literal>CREATE</literal>
47   privilege on the new schema.
48   To alter the owner, you must also be a direct or indirect member of the new
49   owning role, and that role must have <literal>CREATE</literal> privilege on
50   the view's schema.  (These restrictions enforce that altering the owner
51   doesn't do anything you couldn't do by dropping and recreating the view.
52   However, a superuser can alter ownership of any view anyway.)
53  </para>
54 </refsect1>
55
56 <refsect1>
57  <title>Parameters</title>
58
59  <variablelist>
60   <varlistentry>
61    <term><replaceable class="parameter">name</replaceable></term>
62    <listitem>
63     <para>
64      The name (optionally schema-qualified) of an existing view.
65     </para>
66    </listitem>
67   </varlistentry>
68
69   <varlistentry>
70    <term><replaceable class="parameter">column_name</replaceable></term>
71    <listitem>
72     <para>
73      Name of an existing column.
74     </para>
75    </listitem>
76   </varlistentry>
77
78   <varlistentry>
79    <term><replaceable class="parameter">new_column_name</replaceable></term>
80    <listitem>
81     <para>
82      New name for an existing column.
83     </para>
84    </listitem>
85   </varlistentry>
86
87   <varlistentry>
88    <term><literal>IF EXISTS</literal></term>
89    <listitem>
90     <para>
91      Do not throw an error if the view does not exist. A notice is issued
92      in this case.
93     </para>
94    </listitem>
95   </varlistentry>
96
97   <varlistentry>
98    <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
99    <listitem>
100     <para>
101      These forms set or remove the default value for a column.
102      A view column's default value is substituted into any
103      <command>INSERT</command> or <command>UPDATE</command> command whose target is the
104      view, before applying any rules or triggers for the view.  The view's
105      default will therefore take precedence over any default values from
106      underlying relations.
107     </para>
108    </listitem>
109   </varlistentry>
110
111   <varlistentry>
112    <term><replaceable class="parameter">new_owner</replaceable></term>
113    <listitem>
114     <para>
115      The user name of the new owner of the view.
116     </para>
117    </listitem>
118   </varlistentry>
119
120   <varlistentry>
121    <term><replaceable class="parameter">new_name</replaceable></term>
122    <listitem>
123     <para>
124      The new name for the view.
125     </para>
126    </listitem>
127   </varlistentry>
128
129   <varlistentry>
130    <term><replaceable class="parameter">new_schema</replaceable></term>
131    <listitem>
132     <para>
133      The new schema for the view.
134     </para>
135    </listitem>
136   </varlistentry>
137
138   <varlistentry>
139    <term><literal>SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )</literal></term>
140    <term><literal>RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )</literal></term>
141    <listitem>
142     <para>
143      Sets or resets a view option.  Currently supported options are:
144      <variablelist>
145       <varlistentry>
146        <term><literal>check_option</literal> (<type>enum</type>)</term>
147        <listitem>
148         <para>
149          Changes the check option of the view.  The value must
150          be <literal>local</literal> or <literal>cascaded</literal>.
151         </para>
152        </listitem>
153       </varlistentry>
154       <varlistentry>
155        <term><literal>security_barrier</literal> (<type>boolean</type>)</term>
156        <listitem>
157         <para>
158          Changes the security-barrier property of the view.  The value must
159          be Boolean value, such as <literal>true</literal>
160          or <literal>false</literal>.
161         </para>
162        </listitem>
163       </varlistentry>
164      </variablelist></para>
165    </listitem>
166   </varlistentry>
167  </variablelist>
168 </refsect1>
169
170 <refsect1>
171  <title>Notes</title>
172
173  <para>
174   For historical reasons, <command>ALTER TABLE</command> can be used with
175   views too; but the only variants of <command>ALTER TABLE</command>
176   that are allowed with views are equivalent to the ones shown above.
177  </para>
178 </refsect1>
179
180 <refsect1>
181  <title>Examples</title>
182
183  <para>
184   To rename the view <literal>foo</literal> to
185   <literal>bar</literal>:
186<programlisting>
187ALTER VIEW foo RENAME TO bar;
188</programlisting>
189  </para>
190
191  <para>
192   To attach a default column value to an updatable view:
193<programlisting>
194CREATE TABLE base_table (id int, ts timestamptz);
195CREATE VIEW a_view AS SELECT * FROM base_table;
196ALTER VIEW a_view ALTER COLUMN ts SET DEFAULT now();
197INSERT INTO base_table(id) VALUES(1);  -- ts will receive a NULL
198INSERT INTO a_view(id) VALUES(2);  -- ts will receive the current time
199</programlisting></para>
200 </refsect1>
201
202 <refsect1>
203  <title>Compatibility</title>
204
205  <para>
206   <command>ALTER VIEW</command> is a <productname>PostgreSQL</productname>
207   extension of the SQL standard.
208  </para>
209 </refsect1>
210
211 <refsect1>
212  <title>See Also</title>
213
214  <simplelist type="inline">
215   <member><xref linkend="sql-createview"/></member>
216   <member><xref linkend="sql-dropview"/></member>
217  </simplelist>
218 </refsect1>
219</refentry>
220