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_name</replaceable> [, ...] )
31 [, ...] | ALL [ PRIVILEGES ] ( <replaceable class="PARAMETER">column_name</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 foreign
361 tables using the server, and also to create, alter, or drop his own
362 user's user mappings associated with that server.
368 <term>ALL PRIVILEGES</term>
371 Grant all of the available privileges at once.
372 The <literal>PRIVILEGES</literal> key word is optional in
373 <productname>PostgreSQL</productname>, though it is required by
380 The privileges required by other commands are listed on the
381 reference page of the respective command.
385 <refsect2 id="sql-grant-description-roles">
386 <title>GRANT on Roles</title>
389 This variant of the <command>GRANT</command> command grants membership
390 in a role to one or more other roles. Membership in a role is significant
391 because it conveys the privileges granted to a role to each of its
396 If <literal>WITH ADMIN OPTION</literal> is specified, the member can
397 in turn grant membership in the role to others, and revoke membership
398 in the role as well. Without the admin option, ordinary users cannot do
400 database superusers can grant or revoke membership in any role to anyone.
401 Roles having <literal>CREATEROLE</> privilege can grant or revoke
402 membership in any role that is not a superuser.
406 Unlike the case with privileges, membership in a role cannot be granted
407 to <literal>PUBLIC</>. Note also that this form of the command does not
408 allow the noise word <literal>GROUP</>.
414 <refsect1 id="SQL-GRANT-notes">
418 The <xref linkend="sql-revoke"> command is used
419 to revoke access privileges.
423 Since <productname>PostgreSQL</productname> 8.1, the concepts of users and
424 groups have been unified into a single kind of entity called a role.
425 It is therefore no longer necessary to use the keyword <literal>GROUP</>
426 to identify whether a grantee is a user or a group. <literal>GROUP</>
427 is still allowed in the command, but it is a noise word.
431 A user may perform <command>SELECT</>, <command>INSERT</>, etc. on a
432 column if he holds that privilege for either the specific column or
433 its whole table. Granting the privilege at the table level and then
434 revoking it for one column will not do what you might wish: the
435 table-level grant is unaffected by a column-level operation.
439 When a non-owner of an object attempts to <command>GRANT</> privileges
440 on the object, the command will fail outright if the user has no
441 privileges whatsoever on the object. As long as some privilege is
442 available, the command will proceed, but it will grant only those
443 privileges for which the user has grant options. The <command>GRANT ALL
444 PRIVILEGES</> forms will issue a warning message if no grant options are
445 held, while the other forms will issue a warning if grant options for
446 any of the privileges specifically named in the command are not held.
447 (In principle these statements apply to the object owner as well, but
448 since the owner is always treated as holding all grant options, the
449 cases can never occur.)
453 It should be noted that database superusers can access
454 all objects regardless of object privilege settings. This
455 is comparable to the rights of <literal>root</> in a Unix system.
456 As with <literal>root</>, it's unwise to operate as a superuser
457 except when absolutely necessary.
461 If a superuser chooses to issue a <command>GRANT</> or <command>REVOKE</>
462 command, the command is performed as though it were issued by the
463 owner of the affected object. In particular, privileges granted via
464 such a command will appear to have been granted by the object owner.
465 (For role membership, the membership appears to have been granted
466 by the containing role itself.)
470 <command>GRANT</> and <command>REVOKE</> can also be done by a role
471 that is not the owner of the affected object, but is a member of the role
472 that owns the object, or is a member of a role that holds privileges
473 <literal>WITH GRANT OPTION</literal> on the object. In this case the
474 privileges will be recorded as having been granted by the role that
475 actually owns the object or holds the privileges
476 <literal>WITH GRANT OPTION</literal>. For example, if table
477 <literal>t1</> is owned by role <literal>g1</>, of which role
478 <literal>u1</> is a member, then <literal>u1</> can grant privileges
479 on <literal>t1</> to <literal>u2</>, but those privileges will appear
480 to have been granted directly by <literal>g1</>. Any other member
481 of role <literal>g1</> could revoke them later.
485 If the role executing <command>GRANT</> holds the required privileges
486 indirectly via more than one role membership path, it is unspecified
487 which containing role will be recorded as having done the grant. In such
488 cases it is best practice to use <command>SET ROLE</> to become the
489 specific role you want to do the <command>GRANT</> as.
493 Granting permission on a table does not automatically extend
494 permissions to any sequences used by the table, including
495 sequences tied to <type>SERIAL</> columns. Permissions on
496 sequences must be set separately.
500 Use <xref linkend="app-psql">'s <command>\dp</command> command
501 to obtain information about existing privileges for tables and
502 columns. For example:
506 Schema | Name | Type | Access privileges | Column access privileges
507 --------+---------+-------+-----------------------+--------------------------
508 public | mytable | table | miriam=arwdDxt/miriam | col1:
509 : =r/miriam : miriam_rw=rw/miriam
513 The entries shown by <command>\dp</command> are interpreted thus:
514 <literallayout class="monospaced">
515 rolename=xxxx -- privileges granted to a role
516 =xxxx -- privileges granted to PUBLIC
519 w -- UPDATE ("write")
520 a -- INSERT ("append")
530 arwdDxt -- ALL PRIVILEGES (for tables, varies for other objects)
531 * -- grant option for preceding privilege
533 /yyyy -- role that granted this privilege
536 The above example display would be seen by user <literal>miriam</> after
537 creating table <literal>mytable</> and doing:
540 GRANT SELECT ON mytable TO PUBLIC;
541 GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
542 GRANT SELECT (col1), UPDATE (col1) ON mytable TO miriam_rw;
547 For non-table objects there are other <command>\d</> commands
548 that can display their privileges.
552 If the <quote>Access privileges</> column is empty for a given object,
553 it means the object has default privileges (that is, its privileges column
554 is null). Default privileges always include all privileges for the owner,
555 and can include some privileges for <literal>PUBLIC</> depending on the
556 object type, as explained above. The first <command>GRANT</> or
557 <command>REVOKE</> on an object
558 will instantiate the default privileges (producing, for example,
559 <literal>{miriam=arwdDxt/miriam}</>) and then modify them per the
560 specified request. Similarly, entries are shown in <quote>Column access
561 privileges</> only for columns with nondefault privileges.
562 (Note: for this purpose, <quote>default privileges</> always means the
563 built-in default privileges for the object's type. An object whose
564 privileges have been affected by an <command>ALTER DEFAULT PRIVILEGES</>
565 command will always be shown with an explicit privilege entry that
566 includes the effects of the <command>ALTER</>.)
570 Notice that the owner's implicit grant options are not marked in the
571 access privileges display. A <literal>*</> will appear only when
572 grant options have been explicitly granted to someone.
576 <refsect1 id="sql-grant-examples">
577 <title>Examples</title>
580 Grant insert privilege to all users on table <literal>films</literal>:
583 GRANT INSERT ON films TO PUBLIC;
588 Grant all available privileges to user <literal>manuel</literal> on view
589 <literal>kinds</literal>:
592 GRANT ALL PRIVILEGES ON kinds TO manuel;
595 Note that while the above will indeed grant all privileges if executed by a
596 superuser or the owner of <literal>kinds</literal>, when executed by someone
597 else it will only grant those permissions for which the someone else has
602 Grant membership in role <literal>admins</> to user <literal>joe</>:
606 </programlisting></para>
609 <refsect1 id="sql-grant-compatibility">
610 <title>Compatibility</title>
613 According to the SQL standard, the <literal>PRIVILEGES</literal>
614 key word in <literal>ALL PRIVILEGES</literal> is required. The
615 SQL standard does not support setting the privileges on more than
616 one object per command.
620 <productname>PostgreSQL</productname> allows an object owner to revoke his
621 own ordinary privileges: for example, a table owner can make the table
622 read-only to himself by revoking his own <literal>INSERT</>,
623 <literal>UPDATE</>, <literal>DELETE</>, and <literal>TRUNCATE</>
624 privileges. This is not possible according to the SQL standard. The
625 reason is that <productname>PostgreSQL</productname> treats the owner's
626 privileges as having been granted by the owner to himself; therefore he
627 can revoke them too. In the SQL standard, the owner's privileges are
628 granted by an assumed entity <quote>_SYSTEM</>. Not being
629 <quote>_SYSTEM</>, the owner cannot revoke these rights.
633 According to the SQL standard, grant options can be granted to
634 <literal>PUBLIC</literal>; PostgreSQL only supports granting grant options
639 The SQL standard provides for a <literal>USAGE</literal> privilege
640 on other kinds of objects: character sets, collations,
645 In the SQL standard, sequences only have a <literal>USAGE</literal>
646 privilege, which controls the use of the <literal>NEXT VALUE FOR</literal>
647 expression, which is equivalent to the
648 function <function>nextval</function> in PostgreSQL. The sequence
649 privileges <literal>SELECT</literal> and <literal>UPDATE</literal> are
650 PostgreSQL extensions. The application of the
651 sequence <literal>USAGE</literal> privilege to
652 the <literal>currval</literal> function is also a PostgreSQL extension (as
653 is the function itself).
657 Privileges on databases, tablespaces, schemas, and languages are
658 <productname>PostgreSQL</productname> extensions.
664 <title>See Also</title>
666 <simplelist type="inline">
667 <member><xref linkend="sql-revoke"></member>
668 <member><xref linkend="sql-alterdefaultprivileges"></member>