grant.sgml 12.3 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/grant.sgml,v 1.28 2002/08/12 20:02:09 petere Exp $
3
PostgreSQL documentation
4 5
-->

6 7
<refentry id="SQL-GRANT">
 <refmeta>
8
  <refentrytitle id="sql-grant-title">GRANT</refentrytitle>
9 10
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
11

12
 <refnamediv>
13
  <refname>GRANT</refname>
14
  <refpurpose>define access privileges</refpurpose>
15 16
 </refnamediv>

17 18
 <refsynopsisdiv>
<synopsis>
19 20
GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
    [,...] | ALL [ PRIVILEGES ] }
21 22 23 24 25
    ON [ TABLE ] <replaceable class="PARAMETER">tablename</replaceable> [, ...]
    TO { <replaceable class="PARAMETER">username</replaceable> | GROUP <replaceable class="PARAMETER">groupname</replaceable> | PUBLIC } [, ...]

GRANT { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }
    ON DATABASE <replaceable>dbname</replaceable> [, ...]
26
    TO { <replaceable class="PARAMETER">username</replaceable> | GROUP <replaceable class="PARAMETER">groupname</replaceable> | PUBLIC } [, ...]
27 28 29 30 31 32 33 34

GRANT { EXECUTE | ALL [ PRIVILEGES ] }
    ON FUNCTION <replaceable>funcname</replaceable> ([<replaceable>type</replaceable>, ...]) [, ...]
    TO { <replaceable class="PARAMETER">username</replaceable> | GROUP <replaceable class="PARAMETER">groupname</replaceable> | PUBLIC } [, ...]

GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON LANGUAGE <replaceable>langname</replaceable> [, ...]
    TO { <replaceable class="PARAMETER">username</replaceable> | GROUP <replaceable class="PARAMETER">groupname</replaceable> | PUBLIC } [, ...]
35 36 37 38

GRANT { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }
    ON SCHEMA <replaceable>schemaname</replaceable> [, ...]
    TO { <replaceable class="PARAMETER">username</replaceable> | GROUP <replaceable class="PARAMETER">groupname</replaceable> | PUBLIC } [, ...]
39
</synopsis>
40 41
 </refsynopsisdiv>

42 43 44 45 46
 <refsect1 id="sql-grant-description">
  <title>Description</title>

  <para>
   The <command>GRANT</command> command gives specific permissions on
47 48
   an object (table, view, sequence, database, function, procedural language,
   or schema) to
49 50
   one or more users or groups of users.  These permissions are added
   to those already granted, if any.
51 52 53 54
  </para>

  <para>
   The key word <literal>PUBLIC</literal> indicates that the
55
   privileges are to be granted to all users, including those that may
56 57 58 59 60 61
   be created later.  <literal>PUBLIC</literal> may be thought of as an
   implicitly defined group that always includes all users.
   Note that any particular user will have the sum
   of privileges granted directly to him, privileges granted to any group he
   is presently a member of, and privileges granted to
   <literal>PUBLIC</literal>.
62
  </para>
63

64
  <para>
65 66
   Users other than the creator of an object do not have any access privileges
   to the object unless the creator grants permissions.
67
   There is no need to grant privileges to the creator of an object,
68 69
   as the creator automatically holds all privileges.
   (The creator could, however, choose to revoke
70 71
   some of his own privileges for safety.  Note that the ability to
   grant and revoke privileges is inherent in the creator and cannot
72 73
   be lost.  The right to drop the object is likewise inherent in the
   creator, and cannot be granted or revoked.)
74 75 76
  </para>

  <para>
77 78 79 80 81 82 83
   The possible privileges are:

   <variablelist>
    <varlistentry>
     <term>SELECT</term>
     <listitem>
      <para>
84
       Allows <xref linkend="sql-select" endterm="sql-select-title"> from any column of the
85
       specified table, view, or sequence.  Also allows the use of
86 87
       <xref linkend="sql-copy" endterm="sql-copy-title"> TO.  For sequences, this
       privilege also allows the use of the <function>currval</function> function.
88 89 90 91 92 93 94 95
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>INSERT</term>
     <listitem>
      <para>
96
       Allows <xref linkend="sql-insert" endterm="sql-insert-title"> of a new row into the
97
       specified table.  Also allows <xref linkend="sql-copy" endterm="sql-copy-title"> FROM.
98 99 100 101 102 103 104 105
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>UPDATE</term>
     <listitem>
      <para>
106
       Allows <xref linkend="sql-update" endterm="sql-update-title"> of any column of the
107 108 109
       specified table.  <literal>SELECT ... FOR UPDATE</literal>
       also requires this privilege (besides the
       <literal>SELECT</literal> privilege).  For sequences, this
110 111
       privilege allows the use of the <function>nextval</function> and
       <function>setval</function> functions.
112 113 114 115 116 117 118 119
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>DELETE</term>
     <listitem>
      <para>
120
       Allows <xref linkend="sql-delete" endterm="sql-delete-title"> of a row from the
121 122 123 124 125 126 127 128 129 130
       specified table.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>RULE</term>
     <listitem>
      <para>
       Allows the creation of a rule on the table/view.  (See <xref
131
       linkend="sql-createrule" endterm="sql-createrule-title"> statement.)
132 133 134 135 136 137 138 139
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>REFERENCES</term>
     <listitem>
      <para>
140
       To create a table with a foreign key constraint, it is
141
       necessary to have this privilege on the table with the referenced
142 143 144 145 146 147 148 149 150 151
       key.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>TRIGGER</term>
     <listitem>
      <para>
       Allows the creation of a trigger on the specified table.  (See
152
       <xref linkend="sql-createtrigger" endterm="sql-createtrigger-title"> statement.)
153 154 155 156
      </para>
     </listitem>
    </varlistentry>

157 158 159 160
    <varlistentry>
     <term>CREATE</term>
     <listitem>
      <para>
161
       For databases, allows new schemas to be created within the database.
162 163
      </para>
      <para>
164
       For schemas, allows new objects to be created within the schema.
165 166
       To rename an existing object, you must own the object <emphasis>and</>
       have this privilege for the containing schema.
167 168 169 170 171 172 173 174 175 176 177 178 179 180
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>TEMPORARY</term>
     <term>TEMP</term>
     <listitem>
      <para>
       Allows temporary tables to be created while using the database.
      </para>
     </listitem>
    </varlistentry>

181 182 183 184 185 186 187
    <varlistentry>
     <term>EXECUTE</term>
     <listitem>
      <para>
       Allows the use of the specified function and the use of any
       operators that are implemented on top of the function.  This is
       the only type of privilege that is applicable to functions.
188
       (This syntax works for aggregate functions, as well.)
189 190 191 192 193 194 195 196
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>USAGE</term>
     <listitem>
      <para>
197 198
       For procedural languages, allows the use of the specified language for
       the creation of functions in that language.  This is the only type
199 200
       of privilege that is applicable to procedural languages.
      </para>
201
      <para>
202
       For schemas, allows access to objects contained in the specified 
203
       schema (assuming that the objects' own privilege requirements are
204
       also met).  Essentially this allows the grantee to <quote>look up</>
205 206
       objects within the schema.
      </para>
207 208 209
     </listitem>
    </varlistentry>

210 211 212 213
    <varlistentry>
     <term>ALL PRIVILEGES</term>
     <listitem>
      <para>
214 215 216 217
       Grant all of the privileges applicable to the object at once.
       The <literal>PRIVILEGES</literal> key word is optional in
       <productname>PostgreSQL</productname>, though it is required by
       strict SQL.
218 219 220 221 222 223 224
      </para>
     </listitem>
    </varlistentry>
   </variablelist>

   The privileges required by other commands are listed on the
   reference page of the respective command.
225
  </para>
226 227
 </refsect1>

228

229 230
 <refsect1 id="SQL-GRANT-notes">
  <title>Notes</title>
231

232 233 234 235 236
   <para>
    The <xref linkend="sql-revoke" endterm="sql-revoke-title"> command is used
    to revoke access privileges.
   </para>

237 238 239 240 241 242 243 244
   <para>
    It should be noted that database <firstterm>superusers</> can access
    all objects regardless of object privilege settings.  This
    is comparable to the rights of <literal>root</> in a Unix system.
    As with <literal>root</>, it's unwise to operate as a superuser
    except when absolutely necessary.
   </para>

245
   <para>
246
    Currently, to grant privileges in <productname>PostgreSQL</productname>
B
Bruce Momjian 已提交
247
    to only a few columns, you must
248
    create a view having the desired columns and then grant privileges
249 250 251 252
    to that view.
   </para>

   <para>
253
    Use <xref linkend="app-psql">'s <command>\dp</command> command
254 255
    to obtain information about existing privileges, for example:
<programlisting>
256 257 258 259 260 261
lusitania=> \dp mytable
        Access privileges for database "lusitania"
 Schema |  Table  |           Access privileges
--------+---------+---------------------------------------
 public | mytable | {=r,miriam=arwdRxt,"group todos=arw"}
(1 row)
262
</programlisting>
263
    The entries shown by <command>\dp</command> are interpreted thus:
264
<programlisting>
265 266 267
              =xxxx -- privileges granted to PUBLIC
         uname=xxxx -- privileges granted to a user
   group gname=xxxx -- privileges granted to a group
268

269 270 271 272
                  r -- SELECT ("read")
                  w -- UPDATE ("write")
                  a -- INSERT ("append")
                  d -- DELETE
273
                  R -- RULE
274 275
                  x -- REFERENCES
                  t -- TRIGGER
276 277 278 279 280
                  X -- EXECUTE
                  U -- USAGE
                  C -- CREATE
                  T -- TEMPORARY
            arwdRxt -- ALL PRIVILEGES (for tables)
281 282 283 284 285 286 287 288
</programlisting>

    The above example display would be seen by user <literal>miriam</> after
    creating table <literal>mytable</> and doing

<programlisting>
GRANT SELECT ON mytable TO PUBLIC;
GRANT SELECT,UPDATE,INSERT ON mytable TO GROUP todos;
289
</programlisting>
290 291 292
   </para>

   <para>
293 294 295 296 297 298 299
   If the <quote>Access privileges</> column is empty for a given object,
it means the object has default privileges (that is, its privileges field
is NULL).  Currently, default privileges are interpreted the same way
for all object types: all privileges for the owner and no privileges for
anyone else.  The first <command>GRANT</> on an object will instantiate
this default (producing, for example, <literal>{=,miriam=arwdRxt}</>)
and then modify it per the specified request.
300 301 302
   </para>
 </refsect1>

303 304 305
 <refsect1 id="sql-grant-examples">
  <title>Examples</title>

306 307 308
  <para>
   Grant insert privilege to all users on table films:

309
<programlisting>
310
GRANT INSERT ON films TO PUBLIC;
311
</programlisting>
312 313 314
  </para>

  <para>
P
Peter Eisentraut 已提交
315
   Grant all privileges to user <literal>manuel</literal> on view <literal>kinds</literal>:
316

317 318 319
<programlisting>
GRANT ALL PRIVILEGES ON kinds TO manuel;
</programlisting>
320 321 322
  </para>
 </refsect1>

323 324
 <refsect1 id="sql-grant-compatibility">
  <title>Compatibility</title>
325
    
326 327 328
  <refsect2>
   <title>SQL92</title>

329
   <para>
330 331 332 333 334 335 336 337 338
    The <literal>PRIVILEGES</literal> key word in <literal>ALL
    PRIVILEGES</literal> is required.  <acronym>SQL</acronym> does not
    support setting the privileges on more than one table per command.
   </para>

   <para>
    The <acronym>SQL92</acronym> syntax for GRANT allows setting
    privileges for individual columns within a table, and allows
    setting a privilege to grant the same privileges to others:
339

340
<synopsis>
341 342 343
GRANT <replaceable class="PARAMETER">privilege</replaceable> [, ...]
    ON <replaceable class="PARAMETER">object</replaceable> [ ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) ] [, ...]
    TO { PUBLIC | <replaceable class="PARAMETER">username</replaceable> [, ...] } [ WITH GRANT OPTION ]
344 345 346 347 348 349
</synopsis>
   </para>

   <para>
    <acronym>SQL</acronym> allows to grant the USAGE privilege on
    other kinds of objects:  CHARACTER SET, COLLATION, TRANSLATION, DOMAIN.
350 351 352
   </para>

   <para>
353 354
    The TRIGGER privilege was introduced in SQL99.  The RULE privilege
    is a PostgreSQL extension.
355 356
   </para>
  </refsect2>
357 358 359 360 361 362 363 364 365 366

 </refsect1>


 <refsect1>
  <title>See Also</title>

  <simpara>
   <xref linkend="sql-revoke">
  </simpara>
367
 </refsect1>
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
386
-->