grant.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 384 行

7
384
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "GRANT" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEGRANT \- define access privileges.SH SYNOPSIS.sp.nfGRANT { { SELECT | INSERT | UPDATE | DELETE | REFERENCES | TRIGGER }    [,...] | ALL [ PRIVILEGES ] }    ON [ TABLE ] \fItablename\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { { USAGE | SELECT | UPDATE }    [,...] | ALL [ PRIVILEGES ] }    ON SEQUENCE \fIsequencename\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }    ON DATABASE \fIdbname\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { EXECUTE | ALL [ PRIVILEGES ] }    ON FUNCTION \fIfuncname\fR ( [ [ \fIargmode\fR ] [ \fIargname\fR ] \fIargtype\fR [, ...] ] ) [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { USAGE | ALL [ PRIVILEGES ] }    ON LANGUAGE \fIlangname\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }    ON SCHEMA \fIschemaname\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT { CREATE | ALL [ PRIVILEGES ] }    ON TABLESPACE \fItablespacename\fR [, ...]    TO { \fIusername\fR | GROUP \fIgroupname\fR | PUBLIC } [, ...] [ WITH GRANT OPTION ]GRANT \fIrole\fR [, ...] TO \fIusername\fR [, ...] [ WITH ADMIN OPTION ].sp.fi.SH "DESCRIPTION".PPThe \fBGRANT\fR command has two basic variants: onethat grants privileges on a database object (table, view, sequence,database, function, procedural language, schema, or tablespace),and one that grants membership in a role. These variants aresimilar in many ways, but they are different enough to be describedseparately..PPAs of PostgreSQL 8.1, the concepts of users andgroups have been unified into a single kind of entity called a role.It is therefore no longer necessary to use the keyword GROUPto identify whether a grantee is a user or a group. GROUPis still allowed in the command, but it is a noise word..SS "GRANT ON DATABASE OBJECTS".PPThis variant of the \fBGRANT\fR command gives specificprivileges on a database object toone or more roles. These privileges are addedto those already granted, if any..PPThe key word PUBLIC indicates that theprivileges are to be granted to all roles, including those that maybe created later. PUBLIC may be thought of as animplicitly defined group that always includes all roles.Any particular role will have the sumof privileges granted directly to it, privileges granted to any role itis presently a member of, and privileges granted toPUBLIC..PPIf WITH GRANT OPTION is specified, the recipientof the privilege may in turn grant it to others. Without a grantoption, the recipient cannot do that. Grant options cannot be grantedto PUBLIC..PPThere is no need to grant privileges to the owner of an object(usually the user that created it),as the owner has all privileges by default. (The owner could,however, choose to revoke some of his own privileges for safety.)The right to drop an object, or to alter its definition in any way isnot described by a grantable privilege; it is inherent in the owner,and cannot be granted or revoked. The owner implicitly has all grantoptions for the object, too..PPDepending on the type of object, the initial default privileges mayinclude granting some privileges to PUBLIC.The default is no public access for tables, schemas, and tablespaces;CONNECT privilege and TEMP table creation privilegefor databases;EXECUTE privilege for functions; andUSAGE privilege for languages.The object owner may of course revoke these privileges. (For maximumsecurity, issue the \fBREVOKE\fR in the same transaction thatcreates the object; then there is no window in which another usermay use the object.).PPThe possible privileges are:.TP\fBSELECT\fRAllows SELECT [\fBselect\fR(7)] from any column of thespecified table, view, or sequence. Also allows the use ofCOPY [\fBcopy\fR(7)] TO. For sequences, thisprivilege also allows the use of the \fBcurrval\fR function..TP\fBINSERT\fRAllows INSERT [\fBinsert\fR(7)] of a new row into thespecified table. Also allows COPY [\fBcopy\fR(7)] FROM..TP\fBUPDATE\fRAllows UPDATE [\fBupdate\fR(7)] of anycolumn of the specified table. SELECT ... FOR UPDATEand SELECT ... FOR SHAREalso require this privilege (besides theSELECT privilege). For sequences, thisprivilege allows the use of the \fBnextval\fR and\fBsetval\fR functions..TP\fBDELETE\fRAllows DELETE [\fBdelete\fR(7)] of a row from thespecified table..TP\fBREFERENCES\fRTo create a foreign key constraint, it isnecessary to have this privilege on both the referencing andreferenced tables..TP\fBTRIGGER\fRAllows the creation of a trigger on the specified table. (See theCREATE TRIGGER [\fBcreate_trigger\fR(7)] statement.).TP\fBCREATE\fRFor databases, allows new schemas to be created within the database.For schemas, allows new objects to be created within the schema.To rename an existing object, you must own the object \fBand\fRhave this privilege for the containing schema.For tablespaces, allows tables and indexes to be created within thetablespace, and allows databases to be created that have the tablespaceas their default tablespace. (Note that revoking this privilegewill not alter the placement of existing objects.).TP\fBCONNECT\fRAllows the user to connect to the specified database. Thisprivilege is checked at connection startup (in addition to checkingany restrictions imposed by \fIpg_hba.conf\fR)..TP\fBTEMPORARY\fR.TP\fBTEMP\fRAllows temporary tables to be created while using the database..TP\fBEXECUTE\fRAllows the use of the specified function and the use of anyoperators that are implemented on top of the function. This isthe only type of privilege that is applicable to functions.(This syntax works for aggregate functions, as well.).TP\fBUSAGE\fRFor procedural languages, allows the use of the specified language forthe creation of functions in that language. This is the only typeof privilege that is applicable to procedural languages.For schemas, allows access to objects contained in the specifiedschema (assuming that the objects' own privilege requirements arealso met). Essentially this allows the grantee to ``look up''objects within the schema. Without this permission, it is stillpossible to see the object names, e.g. by querying the system tables.Also, after revoking this permission, existing backends might havestatements that have previously performed this lookup, so this is nota completely secure way to prevent object access.For sequences, this privilege allows the use of the\fBcurrval\fR and \fBnextval\fR functions..TP\fBALL PRIVILEGES\fRGrant all of the available privileges at once.The PRIVILEGES key word is optional inPostgreSQL, though it is required bystrict SQL..PPThe privileges required by other commands are listed on thereference page of the respective command..SS "GRANT ON ROLES".PPThis variant of the \fBGRANT\fR command grants membershipin a role to one or more other roles. Membership in a role is significantbecause it conveys the privileges granted to a role to each of itsmembers..PPIf WITH ADMIN OPTION is specified, the member mayin turn grant membership in the role to others, and revoke membershipin the role as well. Without the admin option, ordinary users cannot dothat. However,database superusers can grant or revoke membership in any role to anyone.Roles having CREATEROLE privilege can grant or revokemembership in any role that is not a superuser..PPUnlike the case with privileges, membership in a role cannot be grantedto PUBLIC. Note also that this form of the command does notallow the noise word GROUP..SH "NOTES".PPThe REVOKE [\fBrevoke\fR(7)] command is usedto revoke access privileges..PPWhen a non-owner of an object attempts to \fBGRANT\fR privilegeson the object, the command will fail outright if the user has noprivileges whatsoever on the object. As long as some privilege isavailable, the command will proceed, but it will grant only thoseprivileges for which the user has grant options. The \fBGRANT ALLPRIVILEGES\fR forms will issue a warning message if no grant options areheld, while the other forms will issue a warning if grant options forany of the privileges specifically named in the command are not held.(In principle these statements apply to the object owner as well, butsince the owner is always treated as holding all grant options, thecases can never occur.).PPIt should be noted that database superusers can accessall objects regardless of object privilege settings. Thisis comparable to the rights of root in a Unix system.As with root, it's unwise to operate as a superuserexcept when absolutely necessary..PPIf a superuser chooses to issue a \fBGRANT\fR or \fBREVOKE\fRcommand, the command is performed as though it were issued by theowner of the affected object. In particular, privileges granted viasuch a command will appear to have been granted by the object owner.(For role membership, the membership appears to have been grantedby the containing role itself.).PP\fBGRANT\fR and \fBREVOKE\fR can also be done by a rolethat is not the owner of the affected object, but is a member of the rolethat owns the object, or is a member of a role that holds privilegesWITH GRANT OPTION on the object. In this case theprivileges will be recorded as having been granted by the role thatactually owns the object or holds the privilegesWITH GRANT OPTION. For example, if tablet1 is owned by role g1, of which roleu1 is a member, then u1 can grant privilegeson t1 to u2, but those privileges will appearto have been granted directly by g1. Any other memberof role g1 could revoke them later..PPIf the role executing \fBGRANT\fR holds the required privilegesindirectly via more than one role membership path, it is unspecifiedwhich containing role will be recorded as having done the grant. In suchcases it is best practice to use \fBSET ROLE\fR to become thespecific role you want to do the \fBGRANT\fR as..PPGranting permission on a table does not automatically extend permissions to any sequences used by the table, including sequences tied to \fBSERIAL\fR columns. Permissions on sequence must be set separately..PPCurrently, PostgreSQL does not supportgranting or revoking privileges for individual columns of a table.One possible workaround is to create a view having just the desiredcolumns and then grant privileges to that view..PPUse \fBpsql\fR(1)'s \fB\\z\fR commandto obtain information about existing privileges, for example:.sp.nf=> \\z mytable                        Access privileges for database "lusitania" Schema |  Name   | Type  |                     Access privileges--------+---------+-------+----------------------------------------------------------- public | mytable | table | {miriam=arwdxt/miriam,=r/miriam,"group todos=arw/miriam"}(1 row).sp.fiThe entries shown by \fB\\z\fR are interpreted thus:.sp.nf              =xxxx -- privileges granted to PUBLIC         uname=xxxx -- privileges granted to a user   group gname=xxxx -- privileges granted to a group                  r -- SELECT ("read")                  w -- UPDATE ("write")                  a -- INSERT ("append")                  d -- DELETE                  x -- REFERENCES                  t -- TRIGGER                  X -- EXECUTE                  U -- USAGE                  C -- CREATE                  c -- CONNECT                  T -- TEMPORARY             arwdxt -- ALL PRIVILEGES (for tables)                  * -- grant option for preceding privilege              /yyyy -- user who granted this privilege.sp.fiThe above example display would be seen by user miriam aftercreating table mytable and doing.sp.nfGRANT SELECT ON mytable TO PUBLIC;GRANT SELECT, UPDATE, INSERT ON mytable TO GROUP todos;.sp.fi.PPIf the ``Access privileges'' column is empty for a given object,it means the object has default privileges (that is, its privileges columnis null). Default privileges always include all privileges for the owner,and may include some privileges for PUBLIC depending on theobject type, as explained above. The first \fBGRANT\fR or\fBREVOKE\fR on an objectwill instantiate the default privileges (producing, for example,{miriam=arwdxt/miriam}) and then modify them per thespecified request..PPNotice that the owner's implicit grant options are not marked in theaccess privileges display. A * will appear only whengrant options have been explicitly granted to someone..SH "EXAMPLES".PPGrant insert privilege to all users on table films:.sp.nfGRANT INSERT ON films TO PUBLIC;.sp.fi.PPGrant all available privileges to user manuel on viewkinds:.sp.nfGRANT ALL PRIVILEGES ON kinds TO manuel;.sp.fiNote that while the above will indeed grant all privileges if executed by asuperuser or the owner of kinds, when executed by someoneelse it will only grant those permissions for which the someone else hasgrant options..PPGrant membership in role admins to user joe:.sp.nfGRANT admins TO joe;.sp.fi.SH "COMPATIBILITY".PPAccording to the SQL standard, the PRIVILEGESkey word in ALL PRIVILEGES is required. TheSQL standard does not support setting the privileges on more thanone object per command..PPPostgreSQL allows an object owner to revoke hisown ordinary privileges: for example, a table owner can make the tableread-only to himself by revoking his own INSERT, UPDATE, and DELETEprivileges. This is not possible according to the SQL standard. Thereason is that PostgreSQL treats the owner'sprivileges as having been granted by the owner to himself; therefore hecan revoke them too. In the SQL standard, the owner's privileges aregranted by an assumed entity ``_SYSTEM''. Not being``_SYSTEM'', the owner cannot revoke these rights..PPThe SQL standard allows setting privileges for individual columnswithin a table:.sp.nfGRANT \fIprivileges\fR    ON \fItable\fR [ ( \fIcolumn\fR [, ...] ) ] [, ...]    TO { PUBLIC | \fIusername\fR [, ...] } [ WITH GRANT OPTION ].sp.fi.PPThe SQL standard provides for a USAGE privilegeon other kinds of objects: character sets, collations,translations, domains..PPPrivileges on databases, tablespaces, schemas, and languages arePostgreSQL extensions..SH "SEE ALSO".PPREVOKE [\fBrevoke\fR(7)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?