2 doc/src/sgml/ref/grant.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-GRANT">
8 <refentrytitle>GRANT</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>GRANT</refname>
15 <refpurpose>define access privileges</refpurpose>
18 <indexterm zone="sql-grant">
19 <primary>GRANT</primary>
24 GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
25 [, ...] | ALL [ PRIVILEGES ] }
26 ON { [ TABLE ] <replaceable class="PARAMETER">table_name</replaceable> [, ...]
27 | ALL TABLES IN SCHEMA <replaceable class="PARAMETER">schema_name</replaceable> [, ...] }
28 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
30 GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( <replaceable class="PARAMETER">column</replaceable> [, ...] )
31 [, ...] | ALL [ PRIVILEGES ] ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) }
32 ON [ TABLE ] <replaceable class="PARAMETER">table_name</replaceable> [, ...]
33 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
35 GRANT { { USAGE | SELECT | UPDATE }
36 [, ...] | ALL [ PRIVILEGES ] }
37 ON { SEQUENCE <replaceable class="PARAMETER">sequence_name</replaceable> [, ...]
38 | ALL SEQUENCES IN SCHEMA <replaceable class="PARAMETER">schema_name</replaceable> [, ...] }
39 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
41 GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] }
42 ON DATABASE <replaceable>database_name</replaceable> [, ...]
43 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
45 GRANT { USAGE | ALL [ PRIVILEGES ] }
46 ON DOMAIN <replaceable>domain_name</replaceable> [, ...]
47 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
49 GRANT { USAGE | ALL [ PRIVILEGES ] }
50 ON FOREIGN DATA WRAPPER <replaceable>fdw_name</replaceable> [, ...]
51 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
53 GRANT { USAGE | ALL [ PRIVILEGES ] }
54 ON FOREIGN SERVER <replaceable>server_name</replaceable> [, ...]
55 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
57 GRANT { EXECUTE | ALL [ PRIVILEGES ] }
58 ON { FUNCTION <replaceable>function_name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">arg_name</replaceable> ] <replaceable class="parameter">arg_type</replaceable> [, ...] ] ) [, ...]
59 | ALL FUNCTIONS IN SCHEMA <replaceable class="PARAMETER">schema_name</replaceable> [, ...] }
60 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
62 GRANT { USAGE | ALL [ PRIVILEGES ] }
63 ON LANGUAGE <replaceable>lang_name</replaceable> [, ...]
64 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
66 GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
67 ON LARGE OBJECT <replaceable class="PARAMETER">loid</replaceable> [, ...]
68 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
70 GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
71 ON SCHEMA <replaceable>schema_name</replaceable> [, ...]
72 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
74 GRANT { CREATE | ALL [ PRIVILEGES ] }
75 ON TABLESPACE <replaceable>tablespace_name</replaceable> [, ...]
76 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
78 GRANT { USAGE | ALL [ PRIVILEGES ] }
79 ON TYPE <replaceable>type_name</replaceable> [, ...]
80 TO { [ GROUP ] <replaceable class="PARAMETER">role_name</replaceable> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
82 GRANT <replaceable class="PARAMETER">role_name</replaceable> [, ...] TO <replaceable class="PARAMETER">role_name</replaceable> [, ...] [ WITH ADMIN OPTION ]
86 <refsect1 id="sql-grant-description">
87 <title>Description</title>
90 The <command>GRANT</command> command has two basic variants: one
91 that grants privileges on a database object (table, column, view, foreign
92 table, sequence, database, foreign-data wrapper, foreign server, function,
93 procedural language, schema, or tablespace), and one that grants
94 membership in a role. These variants are similar in many ways, but
95 they are different enough to be described separately.
98 <refsect2 id="sql-grant-description-objects">
99 <title>GRANT on Database Objects</title>
102 This variant of the <command>GRANT</command> command gives specific
103 privileges on a database object to
104 one or more roles. These privileges are added
105 to those already granted, if any.
109 There is also an option to grant privileges on all objects of the same
110 type within one or more schemas. This functionality is currently supported
111 only for tables, sequences, and functions (but note that <literal>ALL
112 TABLES</> is considered to include views and foreign tables).
116 The key word <literal>PUBLIC</literal> indicates that the
117 privileges are to be granted to all roles, including those that might
118 be created later. <literal>PUBLIC</literal> can be thought of as an
119 implicitly defined group that always includes all roles.
120 Any particular role will have the sum
121 of privileges granted directly to it, privileges granted to any role it
122 is presently a member of, and privileges granted to
123 <literal>PUBLIC</literal>.
127 If <literal>WITH GRANT OPTION</literal> is specified, the recipient
128 of the privilege can in turn grant it to others. Without a grant
129 option, the recipient cannot do that. Grant options cannot be granted
130 to <literal>PUBLIC</literal>.
134 There is no need to grant privileges to the owner of an object
135 (usually the user that created it),
136 as the owner has all privileges by default. (The owner could,
137 however, choose to revoke some of his own privileges for safety.)
141 The right to drop an object, or to alter its definition in any way, is
142 not treated as a grantable privilege; it is inherent in the owner,
143 and cannot be granted or revoked. (However, a similar effect can be
144 obtained by granting or revoking membership in the role that owns
145 the object; see below.) The owner implicitly has all grant
146 options for the object, too.
150 PostgreSQL grants default privileges on some types of objects to
151 <literal>PUBLIC</literal>. No privileges are granted to
152 <literal>PUBLIC</literal> by default on tables,
153 columns, schemas or tablespaces. For other types, the default privileges
154 granted to <literal>PUBLIC</literal> are as follows:
155 <literal>CONNECT</literal> and <literal>CREATE TEMP TABLE</literal> for
156 databases; <literal>EXECUTE</literal> privilege for functions; and
157 <literal>USAGE</literal> privilege for languages.
158 The object owner can, of course, <command>REVOKE</command>
159 both default and expressly granted privileges. (For maximum
160 security, issue the <command>REVOKE</> in the same transaction that
161 creates the object; then there is no window in which another user
163 Also, these initial default privilege settings can be changed using the
164 <xref linkend="sql-alterdefaultprivileges">
169 The possible privileges are:
176 Allows <xref linkend="sql-select"> from
177 any column, or the specific columns listed, of the specified table,
179 Also allows the use of
180 <xref linkend="sql-copy"> TO.
181 This privilege is also needed to reference existing column values in
182 <xref linkend="sql-update"> or
183 <xref linkend="sql-delete">.
184 For sequences, this privilege also allows the use of the
185 <function>currval</function> function.
186 For large objects, this privilege allows the object to be read.
195 Allows <xref linkend="sql-insert"> of a new
196 row into the specified table. If specific columns are listed,
197 only those columns may be assigned to in the <command>INSERT</>
198 command (other columns will therefore receive default values).
199 Also allows <xref linkend="sql-copy"> FROM.
208 Allows <xref linkend="sql-update"> of any
209 column, or the specific columns listed, of the specified table.
210 (In practice, any nontrivial <command>UPDATE</> command will require
211 <literal>SELECT</> privilege as well, since it must reference table
212 columns to determine which rows to update, and/or to compute new
214 <literal>SELECT ... FOR UPDATE</literal>
215 and <literal>SELECT ... FOR SHARE</literal>
216 also require this privilege on at least one column, in addition to the
217 <literal>SELECT</literal> privilege. For sequences, this
218 privilege allows the use of the <function>nextval</function> and
219 <function>setval</function> functions.
220 For large objects, this privilege allows writing or truncating the
230 Allows <xref linkend="sql-delete"> of a row
231 from the specified table.
232 (In practice, any nontrivial <command>DELETE</> command will require
233 <literal>SELECT</> privilege as well, since it must reference table
234 columns to determine which rows to delete.)
240 <term>TRUNCATE</term>
243 Allows <xref linkend="sql-truncate"> on
250 <term>REFERENCES</term>
253 To create a foreign key constraint, it is
254 necessary to have this privilege on both the referencing and
255 referenced columns. The privilege may be granted for all columns
256 of a table, or just specific columns.
265 Allows the creation of a trigger on the specified table. (See the
266 <xref linkend="sql-createtrigger"> statement.)
275 For databases, allows new schemas to be created within the database.
278 For schemas, allows new objects to be created within the schema.
279 To rename an existing object, you must own the object <emphasis>and</>
280 have this privilege for the containing schema.
283 For tablespaces, allows tables, indexes, and temporary files to be
284 created within the tablespace, and allows databases to be created that
285 have the tablespace as their default tablespace. (Note that revoking
286 this privilege will not alter the placement of existing objects.)
295 Allows the user to connect to the specified database. This
296 privilege is checked at connection startup (in addition to checking
297 any restrictions imposed by <filename>pg_hba.conf</>).
303 <term>TEMPORARY</term>
307 Allows temporary tables to be created while using the specified database.
316 Allows the use of the specified function and the use of any
317 operators that are implemented on top of the function. This is
318 the only type of privilege that is applicable to functions.
319 (This syntax works for aggregate functions, as well.)
328 For procedural languages, allows the use of the specified language for
329 the creation of functions in that language. This is the only type
330 of privilege that is applicable to procedural languages.
333 For schemas, allows access to objects contained in the specified
334 schema (assuming that the objects' own privilege requirements are
335 also met). Essentially this allows the grantee to <quote>look up</>
336 objects within the schema. Without this permission, it is still
337 possible to see the object names, e.g. by querying the system tables.
338 Also, after revoking this permission, existing backends might have
339 statements that have previously performed this lookup, so this is not
340 a completely secure way to prevent object access.
343 For sequences, this privilege allows the use of the
344 <function>currval</function> and <function>nextval</function> functions.
347 For types and domains, this privilege allow the use of the type or
348 domain in the creation of tables, functions, and other schema objects.
349 (Note that it does not control general <quote>usage</quote> of the type,
350 such as values of the type appearing in queries. It only prevents
351 objects from being created that depend on the type. The main purpose of
352 the privilege is controlling which users create dependencies on a type,
353 which could prevent the owner from changing the type later.)
356 For foreign-data wrappers, this privilege enables the grantee
357 to create new servers using that foreign-data wrapper.
360 For servers, this privilege enables the grantee to create,
361 alter, and drop his own user's user mappings associated with
362 that server. Also, it enables the grantee to query the options
363 of the server and associated user mappings.
369 <term>ALL PRIVILEGES</term>
372 Grant all of the available privileges at once.
373 The <literal>PRIVILEGES</literal> key word is optional in
374 <productname>PostgreSQL</productname>, though it is required by
381 The privileges required by other commands are listed on the
382 reference page of the respective command.
386 <refsect2 id="sql-grant-description-roles">
387 <title>GRANT on Roles</title>
390 This variant of the <command>GRANT</command> command grants membership
391 in a role to one or more other roles. Membership in a role is significant
392 because it conveys the privileges granted to a role to each of its
397 If <literal>WITH ADMIN OPTION</literal> is specified, the member can
398 in turn grant membership in the role to others, and revoke membership
399 in the role as well. Without the admin option, ordinary users cannot do
401 database superusers can grant or revoke membership in any role to anyone.
402 Roles having <literal>CREATEROLE</> privilege can grant or revoke
403 membership in any role that is not a superuser.
407 Unlike the case with privileges, membership in a role cannot be granted
408 to <literal>PUBLIC</>. Note also that this form of the command does not
409 allow the noise word <literal>GROUP</>.
415 <refsect1 id="SQL-GRANT-notes">
419 The <xref linkend="sql-revoke"> command is used
420 to revoke access privileges.
424 Since <productname>PostgreSQL</productname> 8.1, the concepts of users and
425 groups have been unified into a single kind of entity called a role.
426 It is therefore no longer necessary to use the keyword <literal>GROUP</>
427 to identify whether a grantee is a user or a group. <literal>GROUP</>
428 is still allowed in the command, but it is a noise word.
432 A user may perform <command>SELECT</>, <command>INSERT</>, etc. on a
433 column if he holds that privilege for either the specific column or
434 its whole table. Granting the privilege at the table level and then
435 revoking it for one column will not do what you might wish: the
436 table-level grant is unaffected by a column-level operation.
440 When a non-owner of an object attempts to <command>GRANT</> privileges
441 on the object, the command will fail outright if the user has no
442 privileges whatsoever on the object. As long as some privilege is
443 available, the command will proceed, but it will grant only those
444 privileges for which the user has grant options. The <command>GRANT ALL
445 PRIVILEGES</> forms will issue a warning message if no grant options are
446 held, while the other forms will issue a warning if grant options for
447 any of the privileges specifically named in the command are not held.
448 (In principle these statements apply to the object owner as well, but
449 since the owner is always treated as holding all grant options, the
450 cases can never occur.)
454 It should be noted that database superusers can access
455 all objects regardless of object privilege settings. This
456 is comparable to the rights of <literal>root</> in a Unix system.
457 As with <literal>root</>, it's unwise to operate as a superuser
458 except when absolutely necessary.
462 If a superuser chooses to issue a <command>GRANT</> or <command>REVOKE</>
463 command, the command is performed as though it were issued by the
464 owner of the affected object. In particular, privileges granted via
465 such a command will appear to have been granted by the object owner.
466 (For role membership, the membership appears to have been granted
467 by the containing role itself.)
471 <command>GRANT</> and <command>REVOKE</> can also be done by a role
472 that is not the owner of the affected object, but is a member of the role
473 that owns the object, or is a member of a role that holds privileges
474 <literal>WITH GRANT OPTION</literal> on the object. In this case the
475 privileges will be recorded as having been granted by the role that
476 actually owns the object or holds the privileges
477 <literal>WITH GRANT OPTION</literal>. For example, if table
478 <literal>t1</> is owned by role <literal>g1</>, of which role
479 <literal>u1</> is a member, then <literal>u1</> can grant privileges
480 on <literal>t1</> to <literal>u2</>, but those privileges will appear
481 to have been granted directly by <literal>g1</>. Any other member
482 of role <literal>g1</> could revoke them later.
486 If the role executing <command>GRANT</> holds the required privileges
487 indirectly via more than one role membership path, it is unspecified
488 which containing role will be recorded as having done the grant. In such
489 cases it is best practice to use <command>SET ROLE</> to become the
490 specific role you want to do the <command>GRANT</> as.
494 Granting permission on a table does not automatically extend
495 permissions to any sequences used by the table, including
496 sequences tied to <type>SERIAL</> columns. Permissions on
497 sequences must be set separately.
501 Use <xref linkend="app-psql">'s <command>\dp</command> command
502 to obtain information about existing privileges for tables and
503 columns. For example:
507 Schema | Name | Type | Access privileges | Column access privileges
508 --------+---------+-------+-----------------------+--------------------------
509 public | mytable | table | miriam=arwdDxt/miriam | col1:
510 : =r/miriam : miriam_rw=rw/miriam
514 The entries shown by <command>\dp</command> are interpreted thus:
515 <literallayout class="monospaced">
516 rolename=xxxx -- privileges granted to a role
517 =xxxx -- privileges granted to PUBLIC
520 w -- UPDATE ("write")
521 a -- INSERT ("append")
531 arwdDxt -- ALL PRIVILEGES (for tables, varies for other objects)
532 * -- grant option for preceding privilege
534 /yyyy -- role that granted this privilege
537 The above example display would be seen by user <literal>miriam</> after
538 creating table <literal>mytable</> and doing:
541 GRANT SELECT ON mytable TO PUBLIC;
542 GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
543 GRANT SELECT (col1), UPDATE (col1) ON mytable TO miriam_rw;
548 For non-table objects there are other <command>\d</> commands
549 that can display their privileges.
553 If the <quote>Access privileges</> column is empty for a given object,
554 it means the object has default privileges (that is, its privileges column
555 is null). Default privileges always include all privileges for the owner,
556 and can include some privileges for <literal>PUBLIC</> depending on the
557 object type, as explained above. The first <command>GRANT</> or
558 <command>REVOKE</> on an object
559 will instantiate the default privileges (producing, for example,
560 <literal>{miriam=arwdDxt/miriam}</>) and then modify them per the
561 specified request. Similarly, entries are shown in <quote>Column access
562 privileges</> only for columns with nondefault privileges.
563 (Note: for this purpose, <quote>default privileges</> always means the
564 built-in default privileges for the object's type. An object whose
565 privileges have been affected by an <command>ALTER DEFAULT PRIVILEGES</>
566 command will always be shown with an explicit privilege entry that
567 includes the effects of the <command>ALTER</>.)
571 Notice that the owner's implicit grant options are not marked in the
572 access privileges display. A <literal>*</> will appear only when
573 grant options have been explicitly granted to someone.
577 <refsect1 id="sql-grant-examples">
578 <title>Examples</title>
581 Grant insert privilege to all users on table <literal>films</literal>:
584 GRANT INSERT ON films TO PUBLIC;
589 Grant all available privileges to user <literal>manuel</literal> on view
590 <literal>kinds</literal>:
593 GRANT ALL PRIVILEGES ON kinds TO manuel;
596 Note that while the above will indeed grant all privileges if executed by a
597 superuser or the owner of <literal>kinds</literal>, when executed by someone
598 else it will only grant those permissions for which the someone else has
603 Grant membership in role <literal>admins</> to user <literal>joe</>:
607 </programlisting></para>
610 <refsect1 id="sql-grant-compatibility">
611 <title>Compatibility</title>
614 According to the SQL standard, the <literal>PRIVILEGES</literal>
615 key word in <literal>ALL PRIVILEGES</literal> is required. The
616 SQL standard does not support setting the privileges on more than
617 one object per command.
621 <productname>PostgreSQL</productname> allows an object owner to revoke his
622 own ordinary privileges: for example, a table owner can make the table
623 read-only to himself by revoking his own <literal>INSERT</>,
624 <literal>UPDATE</>, <literal>DELETE</>, and <literal>TRUNCATE</>
625 privileges. This is not possible according to the SQL standard. The
626 reason is that <productname>PostgreSQL</productname> treats the owner's
627 privileges as having been granted by the owner to himself; therefore he
628 can revoke them too. In the SQL standard, the owner's privileges are
629 granted by an assumed entity <quote>_SYSTEM</>. Not being
630 <quote>_SYSTEM</>, the owner cannot revoke these rights.
634 According to the SQL standard, grant options can be granted to
635 <literal>PUBLIC</literal>; PostgreSQL only supports granting grant options
640 The SQL standard provides for a <literal>USAGE</literal> privilege
641 on other kinds of objects: character sets, collations,
646 In the SQL standard, sequences only have a <literal>USAGE</literal>
647 privilege, which controls the use of the <literal>NEXT VALUE FOR</literal>
648 expression, which is equivalent to the
649 function <function>nextval</function> in PostgreSQL. The sequence
650 privileges <literal>SELECT</literal> and <literal>UPDATE</literal> are
651 PostgreSQL extensions. The application of the
652 sequence <literal>USAGE</literal> privilege to
653 the <literal>currval</literal> function is also a PostgreSQL extension (as
654 is the function itself).
658 Privileges on databases, tablespaces, schemas, and languages are
659 <productname>PostgreSQL</productname> extensions.
665 <title>See Also</title>
667 <simplelist type="inline">
668 <member><xref linkend="sql-revoke"></member>
669 <member><xref linkend="sql-alterdefaultprivileges"></member>