]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/alter_default_privileges.sgml
Trim trailing whitespace
[postgresql] / doc / src / sgml / ref / alter_default_privileges.sgml
1 <!--
2 doc/src/sgml/ref/alter_default_privileges.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-ALTERDEFAULTPRIVILEGES">
7  <indexterm zone="sql-alterdefaultprivileges">
8   <primary>ALTER DEFAULT PRIVILEGES</primary>
9  </indexterm>
10
11  <refmeta>
12   <refentrytitle>ALTER DEFAULT PRIVILEGES</refentrytitle>
13   <manvolnum>7</manvolnum>
14   <refmiscinfo>SQL - Language Statements</refmiscinfo>
15  </refmeta>
16
17  <refnamediv>
18   <refname>ALTER DEFAULT PRIVILEGES</refname>
19   <refpurpose>define default access privileges</refpurpose>
20  </refnamediv>
21
22  <refsynopsisdiv>
23 <synopsis>
24 ALTER DEFAULT PRIVILEGES
25     [ FOR { ROLE | USER } <replaceable>target_role</replaceable> [, ...] ]
26     [ IN SCHEMA <replaceable>schema_name</replaceable> [, ...] ]
27     <replaceable class="parameter">abbreviated_grant_or_revoke</replaceable>
28
29 <phrase>where <replaceable class="parameter">abbreviated_grant_or_revoke</replaceable> is one of:</phrase>
30
31 GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
32     [, ...] | ALL [ PRIVILEGES ] }
33     ON TABLES
34     TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
35
36 GRANT { { USAGE | SELECT | UPDATE }
37     [, ...] | ALL [ PRIVILEGES ] }
38     ON SEQUENCES
39     TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
40
41 GRANT { EXECUTE | ALL [ PRIVILEGES ] }
42     ON FUNCTIONS
43     TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
44
45 GRANT { USAGE | ALL [ PRIVILEGES ] }
46     ON TYPES
47     TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
48
49 GRANT { USAGE | CREATE | ALL [ PRIVILEGES ] }
50     ON SCHEMAS
51     TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
52
53 REVOKE [ GRANT OPTION FOR ]
54     { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
55     [, ...] | ALL [ PRIVILEGES ] }
56     ON TABLES
57     FROM { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...]
58     [ CASCADE | RESTRICT ]
59
60 REVOKE [ GRANT OPTION FOR ]
61     { { USAGE | SELECT | UPDATE }
62     [, ...] | ALL [ PRIVILEGES ] }
63     ON SEQUENCES
64     FROM { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...]
65     [ CASCADE | RESTRICT ]
66
67 REVOKE [ GRANT OPTION FOR ]
68     { EXECUTE | ALL [ PRIVILEGES ] }
69     ON FUNCTIONS
70     FROM { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...]
71     [ CASCADE | RESTRICT ]
72
73 REVOKE [ GRANT OPTION FOR ]
74     { USAGE | ALL [ PRIVILEGES ] }
75     ON TYPES
76     FROM { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...]
77     [ CASCADE | RESTRICT ]
78
79 REVOKE [ GRANT OPTION FOR ]
80     { USAGE | CREATE | ALL [ PRIVILEGES ] }
81     ON SCHEMAS
82     FROM { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...]
83     [ CASCADE | RESTRICT ]
84 </synopsis>
85  </refsynopsisdiv>
86
87  <refsect1 id="sql-alterdefaultprivileges-description">
88   <title>Description</title>
89
90   <para>
91    <command>ALTER DEFAULT PRIVILEGES</> allows you to set the privileges
92    that will be applied to objects created in the future.  (It does not
93    affect privileges assigned to already-existing objects.)  Currently,
94    only the privileges for schemas, tables (including views and foreign
95    tables), sequences, functions, and types (including domains) can be
96    altered.
97   </para>
98
99   <para>
100    You can change default privileges only for objects that will be created by
101    yourself or by roles that you are a member of.  The privileges can be set
102    globally (i.e., for all objects created in the current database),
103    or just for objects created in specified schemas.  Default privileges
104    that are specified per-schema are added to whatever the global default
105    privileges are for the particular object type.
106   </para>
107
108   <para>
109    As explained under <xref linkend="sql-grant">,
110    the default privileges for any object type normally grant all grantable
111    permissions to the object owner, and may grant some privileges to
112    <literal>PUBLIC</> as well.  However, this behavior can be changed by
113    altering the global default privileges with
114    <command>ALTER DEFAULT PRIVILEGES</>.
115   </para>
116
117  <refsect2>
118   <title>Parameters</title>
119
120   <variablelist>
121    <varlistentry>
122     <term><replaceable>target_role</replaceable></term>
123     <listitem>
124      <para>
125       The name of an existing role of which the current role is a member.
126       If <literal>FOR ROLE</> is omitted, the current role is assumed.
127      </para>
128     </listitem>
129    </varlistentry>
130
131    <varlistentry>
132     <term><replaceable>schema_name</replaceable></term>
133     <listitem>
134      <para>
135       The name of an existing schema.  If specified, the default privileges
136       are altered for objects later created in that schema.
137       If <literal>IN SCHEMA</> is omitted, the global default privileges
138       are altered.
139       <literal>IN SCHEMA</> is not allowed when using <literal>ON SCHEMAS</>
140       as schemas can't be nested.
141      </para>
142     </listitem>
143    </varlistentry>
144
145    <varlistentry>
146     <term><replaceable>role_name</replaceable></term>
147     <listitem>
148      <para>
149       The name of an existing role to grant or revoke privileges for.
150       This parameter, and all the other parameters in
151       <replaceable class="parameter">abbreviated_grant_or_revoke</>,
152       act as described under
153       <xref linkend="sql-grant"> or
154       <xref linkend="sql-revoke">,
155       except that one is setting permissions for a whole class of objects
156       rather than specific named objects.
157      </para>
158     </listitem>
159    </varlistentry>
160   </variablelist>
161  </refsect2>
162  </refsect1>
163
164  <refsect1 id="sql-alterdefaultprivileges-notes">
165   <title>Notes</title>
166
167   <para>
168    Use <xref linkend="app-psql">'s <command>\ddp</command> command
169    to obtain information about existing assignments of default privileges.
170    The meaning of the privilege values is the same as explained for
171    <command>\dp</command> under
172    <xref linkend="sql-grant">.
173   </para>
174
175   <para>
176    If you wish to drop a role for which the default privileges have been
177    altered, it is necessary to reverse the changes in its default privileges
178    or use <command>DROP OWNED BY</> to get rid of the default privileges entry
179    for the role.
180   </para>
181  </refsect1>
182
183  <refsect1 id="sql-alterdefaultprivileges-examples">
184   <title>Examples</title>
185
186   <para>
187    Grant SELECT privilege to everyone for all tables (and views) you
188    subsequently create in schema <literal>myschema</literal>, and allow
189    role <literal>webuser</> to INSERT into them too:
190
191 <programlisting>
192 ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO PUBLIC;
193 ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT INSERT ON TABLES TO webuser;
194 </programlisting>
195   </para>
196
197   <para>
198    Undo the above, so that subsequently-created tables won't have any
199    more permissions than normal:
200
201 <programlisting>
202 ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE SELECT ON TABLES FROM PUBLIC;
203 ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE INSERT ON TABLES FROM webuser;
204 </programlisting>
205   </para>
206
207   <para>
208    Remove the public EXECUTE permission that is normally granted on functions,
209    for all functions subsequently created by role <literal>admin</>:
210
211 <programlisting>
212 ALTER DEFAULT PRIVILEGES FOR ROLE admin REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
213 </programlisting></para>
214  </refsect1>
215
216  <refsect1>
217   <title>Compatibility</title>
218
219   <para>
220    There is no <command>ALTER DEFAULT PRIVILEGES</command> statement in the SQL
221    standard.
222   </para>
223  </refsect1>
224
225  <refsect1>
226   <title>See Also</title>
227
228   <simplelist type="inline">
229    <member><xref linkend="sql-grant"></member>
230    <member><xref linkend="sql-revoke"></member>
231   </simplelist>
232  </refsect1>
233
234 </refentry>