gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
21KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>GRANT</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-fetch.html" title="FETCH" /><link rel="next" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">GRANT</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-fetch.html" title="FETCH">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-GRANT"><div class="titlepage"></div><a id="id-1.9.3.150.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">GRANT</span></h2><p>GRANT — define access privileges</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
  4. [, ...] | ALL [ PRIVILEGES ] }
  5. ON { [ TABLE ] <em class="replaceable"><code>table_name</code></em> [, ...]
  6. | ALL TABLES IN SCHEMA <em class="replaceable"><code>schema_name</code></em> [, ...] }
  7. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  8. GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( <em class="replaceable"><code>column_name</code></em> [, ...] )
  9. [, ...] | ALL [ PRIVILEGES ] ( <em class="replaceable"><code>column_name</code></em> [, ...] ) }
  10. ON [ TABLE ] <em class="replaceable"><code>table_name</code></em> [, ...]
  11. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  12. GRANT { { USAGE | SELECT | UPDATE }
  13. [, ...] | ALL [ PRIVILEGES ] }
  14. ON { SEQUENCE <em class="replaceable"><code>sequence_name</code></em> [, ...]
  15. | ALL SEQUENCES IN SCHEMA <em class="replaceable"><code>schema_name</code></em> [, ...] }
  16. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  17. GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] }
  18. ON DATABASE <em class="replaceable"><code>database_name</code></em> [, ...]
  19. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  20. GRANT { USAGE | ALL [ PRIVILEGES ] }
  21. ON DOMAIN <em class="replaceable"><code>domain_name</code></em> [, ...]
  22. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  23. GRANT { USAGE | ALL [ PRIVILEGES ] }
  24. ON FOREIGN DATA WRAPPER <em class="replaceable"><code>fdw_name</code></em> [, ...]
  25. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  26. GRANT { USAGE | ALL [ PRIVILEGES ] }
  27. ON FOREIGN SERVER <em class="replaceable"><code>server_name</code></em> [, ...]
  28. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  29. GRANT { EXECUTE | ALL [ PRIVILEGES ] }
  30. ON { { FUNCTION | PROCEDURE | ROUTINE } <em class="replaceable"><code>routine_name</code></em> [ ( [ [ <em class="replaceable"><code>argmode</code></em> ] [ <em class="replaceable"><code>arg_name</code></em> ] <em class="replaceable"><code>arg_type</code></em> [, ...] ] ) ] [, ...]
  31. | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA <em class="replaceable"><code>schema_name</code></em> [, ...] }
  32. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  33. GRANT { USAGE | ALL [ PRIVILEGES ] }
  34. ON LANGUAGE <em class="replaceable"><code>lang_name</code></em> [, ...]
  35. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  36. GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] }
  37. ON LARGE OBJECT <em class="replaceable"><code>loid</code></em> [, ...]
  38. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  39. GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] }
  40. ON SCHEMA <em class="replaceable"><code>schema_name</code></em> [, ...]
  41. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  42. GRANT { CREATE | ALL [ PRIVILEGES ] }
  43. ON TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> [, ...]
  44. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  45. GRANT { USAGE | ALL [ PRIVILEGES ] }
  46. ON TYPE <em class="replaceable"><code>type_name</code></em> [, ...]
  47. TO <em class="replaceable"><code>role_specification</code></em> [, ...] [ WITH GRANT OPTION ]
  48. GRANT <em class="replaceable"><code>role_name</code></em> [, ...] TO <em class="replaceable"><code>role_specification</code></em> [, ...]
  49. [ WITH ADMIN OPTION ]
  50. [ GRANTED BY <em class="replaceable"><code>role_specification</code></em> ]
  51. <span class="phrase">where <em class="replaceable"><code>role_specification</code></em> can be:</span>
  52. [ GROUP ] <em class="replaceable"><code>role_name</code></em>
  53. | PUBLIC
  54. | CURRENT_USER
  55. | SESSION_USER
  56. </pre></div><div class="refsect1" id="SQL-GRANT-DESCRIPTION"><h2>Description</h2><p>
  57. The <code class="command">GRANT</code> command has two basic variants: one
  58. that grants privileges on a database object (table, column, view, foreign
  59. table, sequence, database, foreign-data wrapper, foreign server, function, procedure,
  60. procedural language, schema, or tablespace), and one that grants
  61. membership in a role. These variants are similar in many ways, but
  62. they are different enough to be described separately.
  63. </p><div class="refsect2" id="SQL-GRANT-DESCRIPTION-OBJECTS"><h3>GRANT on Database Objects</h3><p>
  64. This variant of the <code class="command">GRANT</code> command gives specific
  65. privileges on a database object to
  66. one or more roles. These privileges are added
  67. to those already granted, if any.
  68. </p><p>
  69. The key word <code class="literal">PUBLIC</code> indicates that the
  70. privileges are to be granted to all roles, including those that might
  71. be created later. <code class="literal">PUBLIC</code> can be thought of as an
  72. implicitly defined group that always includes all roles.
  73. Any particular role will have the sum
  74. of privileges granted directly to it, privileges granted to any role it
  75. is presently a member of, and privileges granted to
  76. <code class="literal">PUBLIC</code>.
  77. </p><p>
  78. If <code class="literal">WITH GRANT OPTION</code> is specified, the recipient
  79. of the privilege can in turn grant it to others. Without a grant
  80. option, the recipient cannot do that. Grant options cannot be granted
  81. to <code class="literal">PUBLIC</code>.
  82. </p><p>
  83. There is no need to grant privileges to the owner of an object
  84. (usually the user that created it),
  85. as the owner has all privileges by default. (The owner could,
  86. however, choose to revoke some of their own privileges for safety.)
  87. </p><p>
  88. The right to drop an object, or to alter its definition in any way, is
  89. not treated as a grantable privilege; it is inherent in the owner,
  90. and cannot be granted or revoked. (However, a similar effect can be
  91. obtained by granting or revoking membership in the role that owns
  92. the object; see below.) The owner implicitly has all grant
  93. options for the object, too.
  94. </p><p>
  95. The possible privileges are:
  96. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">SELECT</code><br /></span><span class="term"><code class="literal">INSERT</code><br /></span><span class="term"><code class="literal">UPDATE</code><br /></span><span class="term"><code class="literal">DELETE</code><br /></span><span class="term"><code class="literal">TRUNCATE</code><br /></span><span class="term"><code class="literal">REFERENCES</code><br /></span><span class="term"><code class="literal">TRIGGER</code><br /></span><span class="term"><code class="literal">CREATE</code><br /></span><span class="term"><code class="literal">CONNECT</code><br /></span><span class="term"><code class="literal">TEMPORARY</code><br /></span><span class="term"><code class="literal">EXECUTE</code><br /></span><span class="term"><code class="literal">USAGE</code></span></dt><dd><p>
  97. Specific types of privileges, as defined in <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
  98. </p></dd><dt><span class="term"><code class="literal">TEMP</code></span></dt><dd><p>
  99. Alternative spelling for <code class="literal">TEMPORARY</code>.
  100. </p></dd><dt><span class="term"><code class="literal">ALL PRIVILEGES</code></span></dt><dd><p>
  101. Grant all of the privileges available for the object's type.
  102. The <code class="literal">PRIVILEGES</code> key word is optional in
  103. <span class="productname">PostgreSQL</span>, though it is required by
  104. strict SQL.
  105. </p></dd></dl></div><p>
  106. </p><p>
  107. The <code class="literal">FUNCTION</code> syntax works for plain functions,
  108. aggregate functions, and window functions, but not for procedures;
  109. use <code class="literal">PROCEDURE</code> for those.
  110. Alternatively, use <code class="literal">ROUTINE</code> to refer to a function,
  111. aggregate function, window function, or procedure regardless of its
  112. precise type.
  113. </p><p>
  114. There is also an option to grant privileges on all objects of the same
  115. type within one or more schemas. This functionality is currently supported
  116. only for tables, sequences, functions, and procedures. <code class="literal">ALL
  117. TABLES</code> also affects views and foreign tables, just like the
  118. specific-object <code class="command">GRANT</code> command. <code class="literal">ALL
  119. FUNCTIONS</code> also affects aggregate and window functions, but not
  120. procedures, again just like the specific-object <code class="command">GRANT</code>
  121. command. Use <code class="literal">ALL ROUTINES</code> to include procedures.
  122. </p></div><div class="refsect2" id="SQL-GRANT-DESCRIPTION-ROLES"><h3>GRANT on Roles</h3><p>
  123. This variant of the <code class="command">GRANT</code> command grants membership
  124. in a role to one or more other roles. Membership in a role is significant
  125. because it conveys the privileges granted to a role to each of its
  126. members.
  127. </p><p>
  128. If <code class="literal">WITH ADMIN OPTION</code> is specified, the member can
  129. in turn grant membership in the role to others, and revoke membership
  130. in the role as well. Without the admin option, ordinary users cannot
  131. do that. A role is not considered to hold <code class="literal">WITH ADMIN
  132. OPTION</code> on itself, but it may grant or revoke membership in
  133. itself from a database session where the session user matches the
  134. role. Database superusers can grant or revoke membership in any role
  135. to anyone. Roles having <code class="literal">CREATEROLE</code> privilege can grant
  136. or revoke membership in any role that is not a superuser.
  137. </p><p>
  138. If <code class="literal">GRANTED BY</code> is specified, the grant is recorded as
  139. having been done by the specified role. Only database superusers may
  140. use this option, except when it names the same role executing the command.
  141. </p><p>
  142. Unlike the case with privileges, membership in a role cannot be granted
  143. to <code class="literal">PUBLIC</code>. Note also that this form of the command
  144. does not allow the noise word <code class="literal">GROUP</code>
  145. in <em class="replaceable"><code>role_specification</code></em>.
  146. </p></div></div><div class="refsect1" id="SQL-GRANT-NOTES"><h2>Notes</h2><p>
  147. The <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a> command is used
  148. to revoke access privileges.
  149. </p><p>
  150. Since <span class="productname">PostgreSQL</span> 8.1, the concepts of users and
  151. groups have been unified into a single kind of entity called a role.
  152. It is therefore no longer necessary to use the keyword <code class="literal">GROUP</code>
  153. to identify whether a grantee is a user or a group. <code class="literal">GROUP</code>
  154. is still allowed in the command, but it is a noise word.
  155. </p><p>
  156. A user may perform <code class="command">SELECT</code>, <code class="command">INSERT</code>, etc. on a
  157. column if they hold that privilege for either the specific column or
  158. its whole table. Granting the privilege at the table level and then
  159. revoking it for one column will not do what one might wish: the
  160. table-level grant is unaffected by a column-level operation.
  161. </p><p>
  162. When a non-owner of an object attempts to <code class="command">GRANT</code> privileges
  163. on the object, the command will fail outright if the user has no
  164. privileges whatsoever on the object. As long as some privilege is
  165. available, the command will proceed, but it will grant only those
  166. privileges for which the user has grant options. The <code class="command">GRANT ALL
  167. PRIVILEGES</code> forms will issue a warning message if no grant options are
  168. held, while the other forms will issue a warning if grant options for
  169. any of the privileges specifically named in the command are not held.
  170. (In principle these statements apply to the object owner as well, but
  171. since the owner is always treated as holding all grant options, the
  172. cases can never occur.)
  173. </p><p>
  174. It should be noted that database superusers can access
  175. all objects regardless of object privilege settings. This
  176. is comparable to the rights of <code class="literal">root</code> in a Unix system.
  177. As with <code class="literal">root</code>, it's unwise to operate as a superuser
  178. except when absolutely necessary.
  179. </p><p>
  180. If a superuser chooses to issue a <code class="command">GRANT</code> or <code class="command">REVOKE</code>
  181. command, the command is performed as though it were issued by the
  182. owner of the affected object. In particular, privileges granted via
  183. such a command will appear to have been granted by the object owner.
  184. (For role membership, the membership appears to have been granted
  185. by the containing role itself.)
  186. </p><p>
  187. <code class="command">GRANT</code> and <code class="command">REVOKE</code> can also be done by a role
  188. that is not the owner of the affected object, but is a member of the role
  189. that owns the object, or is a member of a role that holds privileges
  190. <code class="literal">WITH GRANT OPTION</code> on the object. In this case the
  191. privileges will be recorded as having been granted by the role that
  192. actually owns the object or holds the privileges
  193. <code class="literal">WITH GRANT OPTION</code>. For example, if table
  194. <code class="literal">t1</code> is owned by role <code class="literal">g1</code>, of which role
  195. <code class="literal">u1</code> is a member, then <code class="literal">u1</code> can grant privileges
  196. on <code class="literal">t1</code> to <code class="literal">u2</code>, but those privileges will appear
  197. to have been granted directly by <code class="literal">g1</code>. Any other member
  198. of role <code class="literal">g1</code> could revoke them later.
  199. </p><p>
  200. If the role executing <code class="command">GRANT</code> holds the required privileges
  201. indirectly via more than one role membership path, it is unspecified
  202. which containing role will be recorded as having done the grant. In such
  203. cases it is best practice to use <code class="command">SET ROLE</code> to become the
  204. specific role you want to do the <code class="command">GRANT</code> as.
  205. </p><p>
  206. Granting permission on a table does not automatically extend
  207. permissions to any sequences used by the table, including
  208. sequences tied to <code class="type">SERIAL</code> columns. Permissions on
  209. sequences must be set separately.
  210. </p><p>
  211. See <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a> for more information about specific
  212. privilege types, as well as how to inspect objects' privileges.
  213. </p></div><div class="refsect1" id="SQL-GRANT-EXAMPLES"><h2>Examples</h2><p>
  214. Grant insert privilege to all users on table <code class="literal">films</code>:
  215. </p><pre class="programlisting">
  216. GRANT INSERT ON films TO PUBLIC;
  217. </pre><p>
  218. </p><p>
  219. Grant all available privileges to user <code class="literal">manuel</code> on view
  220. <code class="literal">kinds</code>:
  221. </p><pre class="programlisting">
  222. GRANT ALL PRIVILEGES ON kinds TO manuel;
  223. </pre><p>
  224. Note that while the above will indeed grant all privileges if executed by a
  225. superuser or the owner of <code class="literal">kinds</code>, when executed by someone
  226. else it will only grant those permissions for which the someone else has
  227. grant options.
  228. </p><p>
  229. Grant membership in role <code class="literal">admins</code> to user <code class="literal">joe</code>:
  230. </p><pre class="programlisting">
  231. GRANT admins TO joe;
  232. </pre></div><div class="refsect1" id="SQL-GRANT-COMPATIBILITY"><h2>Compatibility</h2><p>
  233. According to the SQL standard, the <code class="literal">PRIVILEGES</code>
  234. key word in <code class="literal">ALL PRIVILEGES</code> is required. The
  235. SQL standard does not support setting the privileges on more than
  236. one object per command.
  237. </p><p>
  238. <span class="productname">PostgreSQL</span> allows an object owner to revoke their
  239. own ordinary privileges: for example, a table owner can make the table
  240. read-only to themselves by revoking their own <code class="literal">INSERT</code>,
  241. <code class="literal">UPDATE</code>, <code class="literal">DELETE</code>, and <code class="literal">TRUNCATE</code>
  242. privileges. This is not possible according to the SQL standard. The
  243. reason is that <span class="productname">PostgreSQL</span> treats the owner's
  244. privileges as having been granted by the owner to themselves; therefore they
  245. can revoke them too. In the SQL standard, the owner's privileges are
  246. granted by an assumed entity <span class="quote">“<span class="quote">_SYSTEM</span>”</span>. Not being
  247. <span class="quote">“<span class="quote">_SYSTEM</span>”</span>, the owner cannot revoke these rights.
  248. </p><p>
  249. According to the SQL standard, grant options can be granted to
  250. <code class="literal">PUBLIC</code>; PostgreSQL only supports granting grant options
  251. to roles.
  252. </p><p>
  253. The SQL standard allows the <code class="literal">GRANTED BY</code> option to
  254. be used in all forms of <code class="command">GRANT</code>. PostgreSQL only
  255. supports it when granting role membership, and even then only superusers
  256. may use it in nontrivial ways.
  257. </p><p>
  258. The SQL standard provides for a <code class="literal">USAGE</code> privilege
  259. on other kinds of objects: character sets, collations,
  260. translations.
  261. </p><p>
  262. In the SQL standard, sequences only have a <code class="literal">USAGE</code>
  263. privilege, which controls the use of the <code class="literal">NEXT VALUE FOR</code>
  264. expression, which is equivalent to the
  265. function <code class="function">nextval</code> in PostgreSQL. The sequence
  266. privileges <code class="literal">SELECT</code> and <code class="literal">UPDATE</code> are
  267. PostgreSQL extensions. The application of the
  268. sequence <code class="literal">USAGE</code> privilege to
  269. the <code class="literal">currval</code> function is also a PostgreSQL extension (as
  270. is the function itself).
  271. </p><p>
  272. Privileges on databases, tablespaces, schemas, and languages are
  273. <span class="productname">PostgreSQL</span> extensions.
  274. </p></div><div class="refsect1" id="id-1.9.3.150.9"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a>, <a class="xref" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES"><span class="refentrytitle">ALTER DEFAULT PRIVILEGES</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-fetch.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-importforeignschema.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">FETCH </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> IMPORT FOREIGN SCHEMA</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1