]> begriffs open source - ai-pg/blob - full-docs/txt/ddl-priv.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ddl-priv.txt
1
2 5.8. Privileges #
3
4    When an object is created, it is assigned an owner. The owner is
5    normally the role that executed the creation statement. For most kinds
6    of objects, the initial state is that only the owner (or a superuser)
7    can do anything with the object. To allow other roles to use it,
8    privileges must be granted.
9
10    There are different kinds of privileges: SELECT, INSERT, UPDATE,
11    DELETE, TRUNCATE, REFERENCES, TRIGGER, CREATE, CONNECT, TEMPORARY,
12    EXECUTE, USAGE, SET, ALTER SYSTEM, and MAINTAIN. The privileges
13    applicable to a particular object vary depending on the object's type
14    (table, function, etc.). More detail about the meanings of these
15    privileges appears below. The following sections and chapters will also
16    show you how these privileges are used.
17
18    The right to modify or destroy an object is inherent in being the
19    object's owner, and cannot be granted or revoked in itself. (However,
20    like all privileges, that right can be inherited by members of the
21    owning role; see Section 21.3.)
22
23    An object can be assigned to a new owner with an ALTER command of the
24    appropriate kind for the object, for example
25 ALTER TABLE table_name OWNER TO new_owner;
26
27    Superusers can always do this; ordinary roles can only do it if they
28    are both the current owner of the object (or inherit the privileges of
29    the owning role) and able to SET ROLE to the new owning role.
30
31    To assign privileges, the GRANT command is used. For example, if joe is
32    an existing role, and accounts is an existing table, the privilege to
33    update the table can be granted with:
34 GRANT UPDATE ON accounts TO joe;
35
36    Writing ALL in place of a specific privilege grants all privileges that
37    are relevant for the object type.
38
39    The special “role” name PUBLIC can be used to grant a privilege to
40    every role on the system. Also, “group” roles can be set up to help
41    manage privileges when there are many users of a database — for details
42    see Chapter 21.
43
44    To revoke a previously-granted privilege, use the fittingly named
45    REVOKE command:
46 REVOKE ALL ON accounts FROM PUBLIC;
47
48    Ordinarily, only the object's owner (or a superuser) can grant or
49    revoke privileges on an object. However, it is possible to grant a
50    privilege “with grant option”, which gives the recipient the right to
51    grant it in turn to others. If the grant option is subsequently revoked
52    then all who received the privilege from that recipient (directly or
53    through a chain of grants) will lose the privilege. For details see the
54    GRANT and REVOKE reference pages.
55
56    An object's owner can choose to revoke their own ordinary privileges,
57    for example to make a table read-only for themselves as well as others.
58    But owners are always treated as holding all grant options, so they can
59    always re-grant their own privileges.
60
61    The available privileges are:
62
63    SELECT #
64           Allows SELECT from any column, or specific column(s), of a
65           table, view, materialized view, or other table-like object. Also
66           allows use of COPY TO. This privilege is also needed to
67           reference existing column values in UPDATE, DELETE, or MERGE.
68           For sequences, this privilege also allows use of the currval
69           function. For large objects, this privilege allows the object to
70           be read.
71
72    INSERT #
73           Allows INSERT of a new row into a table, view, etc. Can be
74           granted on specific column(s), in which case only those columns
75           may be assigned to in the INSERT command (other columns will
76           therefore receive default values). Also allows use of COPY FROM.
77
78    UPDATE #
79           Allows UPDATE of any column, or specific column(s), of a table,
80           view, etc. (In practice, any nontrivial UPDATE command will
81           require SELECT privilege as well, since it must reference table
82           columns to determine which rows to update, and/or to compute new
83           values for columns.) SELECT ... FOR UPDATE and SELECT ... FOR
84           SHARE also require this privilege on at least one column, in
85           addition to the SELECT privilege. For sequences, this privilege
86           allows use of the nextval and setval functions. For large
87           objects, this privilege allows writing or truncating the object.
88
89    DELETE #
90           Allows DELETE of a row from a table, view, etc. (In practice,
91           any nontrivial DELETE command will require SELECT privilege as
92           well, since it must reference table columns to determine which
93           rows to delete.)
94
95    TRUNCATE #
96           Allows TRUNCATE on a table.
97
98    REFERENCES #
99           Allows creation of a foreign key constraint referencing a table,
100           or specific column(s) of a table.
101
102    TRIGGER #
103           Allows creation of a trigger on a table, view, etc.
104
105    CREATE #
106           For databases, allows new schemas and publications to be created
107           within the database, and allows trusted extensions to be
108           installed within the database.
109
110           For schemas, allows new objects to be created within the schema.
111           To rename an existing object, you must own the object and have
112           this privilege for the containing schema.
113
114           For tablespaces, allows tables, indexes, and temporary files to
115           be created within the tablespace, and allows databases to be
116           created that have the tablespace as their default tablespace.
117
118           Note that revoking this privilege will not alter the existence
119           or location of existing objects.
120
121    CONNECT #
122           Allows the grantee to connect to the database. This privilege is
123           checked at connection startup (in addition to checking any
124           restrictions imposed by pg_hba.conf).
125
126    TEMPORARY #
127           Allows temporary tables to be created while using the database.
128
129    EXECUTE #
130           Allows calling a function or procedure, including use of any
131           operators that are implemented on top of the function. This is
132           the only type of privilege that is applicable to functions and
133           procedures.
134
135    USAGE #
136           For procedural languages, allows use of the language for the
137           creation of functions in that language. This is the only type of
138           privilege that is applicable to procedural languages.
139
140           For schemas, allows access to objects contained in the schema
141           (assuming that the objects' own privilege requirements are also
142           met). Essentially this allows the grantee to “look up” objects
143           within the schema. Without this permission, it is still possible
144           to see the object names, e.g., by querying system catalogs.
145           Also, after revoking this permission, existing sessions might
146           have statements that have previously performed this lookup, so
147           this is not a completely secure way to prevent object access.
148
149           For sequences, allows use of the currval and nextval functions.
150
151           For types and domains, allows use of the type or domain in the
152           creation of tables, functions, and other schema objects. (Note
153           that this privilege does not control all “usage” of the type,
154           such as values of the type appearing in queries. It only
155           prevents objects from being created that depend on the type. The
156           main purpose of this privilege is controlling which users can
157           create dependencies on a type, which could prevent the owner
158           from changing the type later.)
159
160           For foreign-data wrappers, allows creation of new servers using
161           the foreign-data wrapper.
162
163           For foreign servers, allows creation of foreign tables using the
164           server. Grantees may also create, alter, or drop their own user
165           mappings associated with that server.
166
167    SET #
168           Allows a server configuration parameter to be set to a new value
169           within the current session. (While this privilege can be granted
170           on any parameter, it is meaningless except for parameters that
171           would normally require superuser privilege to set.)
172
173    ALTER SYSTEM #
174           Allows a server configuration parameter to be configured to a
175           new value using the ALTER SYSTEM command.
176
177    MAINTAIN #
178           Allows VACUUM, ANALYZE, CLUSTER, REFRESH MATERIALIZED VIEW,
179           REINDEX, LOCK TABLE, and database object statistics manipulation
180           functions (see Table 9.105) on a relation.
181
182    The privileges required by other commands are listed on the reference
183    page of the respective command.
184
185    PostgreSQL grants privileges on some types of objects to PUBLIC by
186    default when the objects are created. No privileges are granted to
187    PUBLIC by default on tables, table columns, sequences, foreign data
188    wrappers, foreign servers, large objects, schemas, tablespaces, or
189    configuration parameters. For other types of objects, the default
190    privileges granted to PUBLIC are as follows: CONNECT and TEMPORARY
191    (create temporary tables) privileges for databases; EXECUTE privilege
192    for functions and procedures; and USAGE privilege for languages and
193    data types (including domains). The object owner can, of course, REVOKE
194    both default and expressly granted privileges. (For maximum security,
195    issue the REVOKE in the same transaction that creates the object; then
196    there is no window in which another user can use the object.) Also,
197    these default privilege settings can be overridden using the ALTER
198    DEFAULT PRIVILEGES command.
199
200    Table 5.1 shows the one-letter abbreviations that are used for these
201    privilege types in ACL values. You will see these letters in the output
202    of the psql commands listed below, or when looking at ACL columns of
203    system catalogs.
204
205    Table 5.1. ACL Privilege Abbreviations
206    Privilege Abbreviation Applicable Object Types
207    SELECT r (“read”) LARGE OBJECT, SEQUENCE, TABLE (and table-like
208    objects), table column
209    INSERT a (“append”) TABLE, table column
210    UPDATE w (“write”) LARGE OBJECT, SEQUENCE, TABLE, table column
211    DELETE d TABLE
212    TRUNCATE D TABLE
213    REFERENCES x TABLE, table column
214    TRIGGER t TABLE
215    CREATE C DATABASE, SCHEMA, TABLESPACE
216    CONNECT c DATABASE
217    TEMPORARY T DATABASE
218    EXECUTE X FUNCTION, PROCEDURE
219    USAGE U DOMAIN, FOREIGN DATA WRAPPER, FOREIGN SERVER, LANGUAGE, SCHEMA,
220    SEQUENCE, TYPE
221    SET s PARAMETER
222    ALTER SYSTEM A PARAMETER
223    MAINTAIN m TABLE
224
225    Table 5.2 summarizes the privileges available for each type of SQL
226    object, using the abbreviations shown above. It also shows the psql
227    command that can be used to examine privilege settings for each object
228    type.
229
230    Table 5.2. Summary of Access Privileges
231    Object Type All Privileges Default PUBLIC Privileges psql Command
232    DATABASE CTc Tc \l
233    DOMAIN U U \dD+
234    FUNCTION or PROCEDURE X X \df+
235    FOREIGN DATA WRAPPER U none \dew+
236    FOREIGN SERVER U none \des+
237    LANGUAGE U U \dL+
238    LARGE OBJECT rw none \dl+
239    PARAMETER sA none \dconfig+
240    SCHEMA UC none \dn+
241    SEQUENCE rwU none \dp
242    TABLE (and table-like objects) arwdDxtm none \dp
243    Table column arwx none \dp
244    TABLESPACE C none \db+
245    TYPE U U \dT+
246
247    The privileges that have been granted for a particular object are
248    displayed as a list of aclitem entries, each having the format:
249 grantee=privilege-abbreviation[*].../grantor
250
251    Each aclitem lists all the permissions of one grantee that have been
252    granted by a particular grantor. Specific privileges are represented by
253    one-letter abbreviations from Table 5.1, with * appended if the
254    privilege was granted with grant option. For example, calvin=r*w/hobbes
255    specifies that the role calvin has the privilege SELECT (r) with grant
256    option (*) as well as the non-grantable privilege UPDATE (w), both
257    granted by the role hobbes. If calvin also has some privileges on the
258    same object granted by a different grantor, those would appear as a
259    separate aclitem entry. An empty grantee field in an aclitem stands for
260    PUBLIC.
261
262    As an example, suppose that user miriam creates table mytable and does:
263 GRANT SELECT ON mytable TO PUBLIC;
264 GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
265 GRANT SELECT (col1), UPDATE (col1) ON mytable TO miriam_rw;
266
267    Then psql's \dp command would show:
268 => \dp mytable
269                                   Access privileges
270  Schema |  Name   | Type  |   Access privileges    |   Column privileges   | Pol
271 icies
272 --------+---------+-------+------------------------+-----------------------+----
273 ------
274  public | mytable | table | miriam=arwdDxtm/miriam+| col1:                +|
275         |         |       | =r/miriam             +|   miriam_rw=rw/miriam |
276         |         |       | admin=arw/miriam       |                       |
277 (1 row)
278
279    If the “Access privileges” column is empty for a given object, it means
280    the object has default privileges (that is, its privileges entry in the
281    relevant system catalog is null). Default privileges always include all
282    privileges for the owner, and can include some privileges for PUBLIC
283    depending on the object type, as explained above. The first GRANT or
284    REVOKE on an object will instantiate the default privileges (producing,
285    for example, miriam=arwdDxt/miriam) and then modify them per the
286    specified request. Similarly, entries are shown in “Column privileges”
287    only for columns with nondefault privileges. (Note: for this purpose,
288    “default privileges” always means the built-in default privileges for
289    the object's type. An object whose privileges have been affected by an
290    ALTER DEFAULT PRIVILEGES command will always be shown with an explicit
291    privilege entry that includes the effects of the ALTER.)
292
293    Notice that the owner's implicit grant options are not marked in the
294    access privileges display. A * will appear only when grant options have
295    been explicitly granted to someone.
296
297    The “Access privileges” column shows (none) when the object's
298    privileges entry is non-null but empty. This means that no privileges
299    are granted at all, even to the object's owner — a rare situation. (The
300    owner still has implicit grant options in this case, and so could
301    re-grant her own privileges; but she has none at the moment.)