Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
5.8. Privileges
Prev UpChapter 5. Data DefinitionHome Next

5.8. Privileges#

When an object is created, it is assigned an owner. The owner is normally the role that executed the creation statement. For most kinds of objects, the initial state is that only the owner (or a superuser) can do anything with the object. To allow other roles to use it,privileges must be granted.

There are different kinds of privileges:SELECT,INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER,CREATE,CONNECT,TEMPORARY,EXECUTE,USAGE,SET,ALTER SYSTEM, andMAINTAIN. The privileges applicable to a particular object vary depending on the object's type (table, function, etc.). More detail about the meanings of these privileges appears below. The following sections and chapters will also show you how these privileges are used.

The right to modify or destroy an object is inherent in being the object's owner, and cannot be granted or revoked in itself. (However, like all privileges, that right can be inherited by members of the owning role; seeSection 20.3.)

An object can be assigned to a new owner with anALTER command of the appropriate kind for the object, for example

ALTER TABLEtable_name OWNER TOnew_owner;

Superusers can always do this; ordinary roles can only do it if they are both the current owner of the object (or inherit the privileges of the owning role) and able toSET ROLE to the new owning role.

To assign privileges, theGRANT command is used. For example, ifjoe is an existing role, andaccounts is an existing table, the privilege to update the table can be granted with:

GRANT UPDATE ON accounts TO joe;

WritingALL in place of a specific privilege grants all privileges that are relevant for the object type.

The specialrole namePUBLIC can be used to grant a privilege to every role on the system. Also,group roles can be set up to help manage privileges when there are many users of a database — for details seeChapter 20.

To revoke a previously-granted privilege, use the fittingly namedREVOKE command:

REVOKE ALL ON accounts FROM PUBLIC;

Ordinarily, only the object's owner (or a superuser) can grant or revoke privileges on an object. However, it is possible to grant a privilegewith grant option, which gives the recipient the right to grant it in turn to others. If the grant option is subsequently revoked then all who received the privilege from that recipient (directly or through a chain of grants) will lose the privilege. For details see theGRANT andREVOKE reference pages.

An object's owner can choose to revoke their own ordinary privileges, for example to make a table read-only for themselves as well as others. But owners are always treated as holding all grant options, so they can always re-grant their own privileges.

The available privileges are:

SELECT#

AllowsSELECT from any column, or specific column(s), of a table, view, materialized view, or other table-like object. Also allows use ofCOPY TO. This privilege is also needed to reference existing column values inUPDATE,DELETE, orMERGE. For sequences, this privilege also allows use of thecurrval function. For large objects, this privilege allows the object to be read.

INSERT#

AllowsINSERT of a new row into a table, view, etc. Can be granted on specific column(s), in which case only those columns may be assigned to in theINSERT command (other columns will therefore receive default values). Also allows use ofCOPY FROM.

UPDATE#

AllowsUPDATE of any column, or specific column(s), of a table, view, etc. (In practice, any nontrivialUPDATE command will requireSELECT privilege as well, since it must reference table columns to determine which rows to update, and/or to compute new values for columns.)SELECT ... FOR UPDATE andSELECT ... FOR SHARE also require this privilege on at least one column, in addition to theSELECT privilege. For sequences, this privilege allows use of thenextval andsetval functions. For large objects, this privilege allows writing or truncating the object.

DELETE#

AllowsDELETE of a row from a table, view, etc. (In practice, any nontrivialDELETE command will requireSELECT privilege as well, since it must reference table columns to determine which rows to delete.)

TRUNCATE#

AllowsTRUNCATE on a table.

REFERENCES#

Allows creation of a foreign key constraint referencing a table, or specific column(s) of a table.

TRIGGER#

Allows creation of a trigger on a table, view, etc.

CREATE#

For databases, allows new schemas and publications to be created within the database, and allows trusted extensions to be installed within the database.

For schemas, allows new objects to be created within the schema. To rename an existing object, you must own the objectand have this privilege for the containing schema.

For tablespaces, allows tables, indexes, and temporary files to be created within the tablespace, and allows databases to be created that have the tablespace as their default tablespace.

Note that revoking this privilege will not alter the existence or location of existing objects.

CONNECT#

Allows the grantee to connect to the database. This privilege is checked at connection startup (in addition to checking any restrictions imposed bypg_hba.conf).

TEMPORARY#

Allows temporary tables to be created while using the database.

EXECUTE#

Allows calling a function or procedure, including use of any operators that are implemented on top of the function. This is the only type of privilege that is applicable to functions and procedures.

USAGE#

For procedural languages, allows use of the language for the creation of functions in that language. This is the only type of privilege that is applicable to procedural languages.

For schemas, allows access to objects contained in the schema (assuming that the objects' own privilege requirements are also met). Essentially this allows the grantee tolook up objects within the schema. Without this permission, it is still possible to see the object names, e.g., by querying system catalogs. Also, after revoking this permission, existing sessions might have statements that have previously performed this lookup, so this is not a completely secure way to prevent object access.

For sequences, allows use of thecurrval andnextval functions.

For types and domains, allows use of the type or domain in the creation of tables, functions, and other schema objects. (Note that this privilege does not control allusage of the type, such as values of the type appearing in queries. It only prevents objects from being created that depend on the type. The main purpose of this privilege is controlling which users can create dependencies on a type, which could prevent the owner from changing the type later.)

For foreign-data wrappers, allows creation of new servers using the foreign-data wrapper.

For foreign servers, allows creation of foreign tables using the server. Grantees may also create, alter, or drop their own user mappings associated with that server.

SET#

Allows a server configuration parameter to be set to a new value within the current session. (While this privilege can be granted on any parameter, it is meaningless except for parameters that would normally require superuser privilege to set.)

ALTER SYSTEM#

Allows a server configuration parameter to be configured to a new value using theALTER SYSTEM command.

MAINTAIN#

AllowsVACUUM,ANALYZE,CLUSTER,REFRESH MATERIALIZED VIEW,REINDEX, andLOCK TABLE on a relation.

The privileges required by other commands are listed on the reference page of the respective command.

Postgres Pro grants privileges on some types of objects toPUBLIC by default when the objects are created. No privileges are granted toPUBLIC by default on tables, table columns, sequences, foreign data wrappers, foreign servers, large objects, schemas, tablespaces, or configuration parameters. For other types of objects, the default privileges granted toPUBLIC are as follows:CONNECT andTEMPORARY (create temporary tables) privileges for databases;EXECUTE privilege for functions and procedures; andUSAGE privilege for languages and data types (including domains). The object owner can, of course,REVOKE both default and expressly granted privileges. (For maximum security, issue theREVOKE in the same transaction that creates the object; then there is no window in which another user can use the object.) Also, these default privilege settings can be overridden using theALTER DEFAULT PRIVILEGES command.

Table 5.1 shows the one-letter abbreviations that are used for these privilege types inACL (Access Control List) values. You will see these letters in the output of thepsql commands listed below, or when looking at ACL columns of system catalogs.

Table 5.1. ACL Privilege Abbreviations

PrivilegeAbbreviationApplicable Object Types
SELECTr (read)LARGE OBJECT,SEQUENCE,TABLE (and table-like objects), table column
INSERTa (append)TABLE, table column
UPDATEw (write)LARGE OBJECT,SEQUENCE,TABLE, table column
DELETEdTABLE
TRUNCATEDTABLE
REFERENCESxTABLE, table column
TRIGGERtTABLE
CREATECDATABASE,SCHEMA,TABLESPACE
CONNECTcDATABASE
TEMPORARYTDATABASE
EXECUTEXFUNCTION,PROCEDURE
USAGEUDOMAIN,FOREIGN DATA WRAPPER,FOREIGN SERVER,LANGUAGE,SCHEMA,SEQUENCE,TYPE
SETsPARAMETER
ALTER SYSTEMAPARAMETER
MAINTAINmTABLE

Table 5.2 summarizes the privileges available for each type of SQL object, using the abbreviations shown above. It also shows thepsql command that can be used to examine privilege settings for each object type.

Table 5.2. Summary of Access Privileges

Object TypeAll PrivilegesDefaultPUBLIC Privilegespsql Command
DATABASECTcTc\l
DOMAINUU\dD+
FUNCTION orPROCEDUREXX\df+
FOREIGN DATA WRAPPERUnone\dew+
FOREIGN SERVERUnone\des+
LANGUAGEUU\dL+
LARGE OBJECTrwnone\dl+
PARAMETERsAnone\dconfig+
SCHEMAUCnone\dn+
SEQUENCErwUnone\dp
TABLE (and table-like objects)arwdDxtmnone\dp
Table columnarwxnone\dp
TABLESPACECnone\db+
TYPEUU\dT+

The privileges that have been granted for a particular object are displayed as a list ofaclitem entries, each having the format:

grantee=privilege-abbreviation[*].../grantor

Eachaclitem lists all the permissions of one grantee that have been granted by a particular grantor. Specific privileges are represented by one-letter abbreviations fromTable 5.1, with* appended if the privilege was granted with grant option. For example,calvin=r*w/hobbes specifies that the rolecalvin has the privilegeSELECT (r) with grant option (*) as well as the non-grantable privilegeUPDATE (w), both granted by the rolehobbes. Ifcalvin also has some privileges on the same object granted by a different grantor, those would appear as a separateaclitem entry. An empty grantee field in anaclitem stands forPUBLIC.

As an example, suppose that usermiriam creates tablemytable and does:

GRANT SELECT ON mytable TO PUBLIC;GRANT SELECT, UPDATE, INSERT ON mytable TO admin;GRANT SELECT (col1), UPDATE (col1) ON mytable TO miriam_rw;

Thenpsql's\dp command would show:

=> \dp mytable                                  Access privileges Schema |  Name   | Type  |   Access privileges    |   Column privileges   | Policies--------+---------+-------+------------------------+-----------------------+---------- public | mytable | table | miriam=arwdDxtm/miriam+| col1:                +|        |         |       | =r/miriam             +|   miriam_rw=rw/miriam |        |         |       | admin=arw/miriam       |                       |(1 row)

If theAccess privileges column is empty for a given object, it means the object has default privileges (that is, its privileges entry in the relevant system catalog is null). Default privileges always include all privileges for the owner, and can include some privileges forPUBLIC depending on the object type, as explained above. The firstGRANT orREVOKE on an object will instantiate the default privileges (producing, for example,miriam=arwdDxt/miriam) and then modify them per the specified request. Similarly, entries are shown inColumn privileges only for columns with nondefault privileges. (Note: for this purpose,default privileges always means the built-in default privileges for the object's type. An object whose privileges have been affected by anALTER DEFAULT PRIVILEGES command will always be shown with an explicit privilege entry that includes the effects of theALTER.)

Notice that the owner's implicit grant options are not marked in the access privileges display. A* will appear only when grant options have been explicitly granted to someone.

TheAccess privileges column shows(none) when the object's privileges entry is non-null but empty. This means that no privileges are granted at all, even to the object's owner — a rare situation. (The owner still has implicit grant options in this case, and so could re-grant her own privileges; but she has none at the moment.)


Prev Up Next
5.7. Modifying Tables Home 5.9. Row Security Policies
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp