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.

159 line
11KB

  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>ALTER DEFAULT PRIVILEGES</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-alterdatabase.html" title="ALTER DATABASE" /><link rel="next" href="sql-alterdomain.html" title="ALTER DOMAIN" /></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">ALTER DEFAULT PRIVILEGES</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterdatabase.html" title="ALTER DATABASE">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-alterdomain.html" title="ALTER DOMAIN">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-ALTERDEFAULTPRIVILEGES"><div class="titlepage"></div><a id="id-1.9.3.8.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER DEFAULT PRIVILEGES</span></h2><p>ALTER DEFAULT PRIVILEGES — define default access privileges</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. ALTER DEFAULT PRIVILEGES
  4. [ FOR { ROLE | USER } <em class="replaceable"><code>target_role</code></em> [, ...] ]
  5. [ IN SCHEMA <em class="replaceable"><code>schema_name</code></em> [, ...] ]
  6. <em class="replaceable"><code>abbreviated_grant_or_revoke</code></em>
  7. <span class="phrase">where <em class="replaceable"><code>abbreviated_grant_or_revoke</code></em> is one of:</span>
  8. GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
  9. [, ...] | ALL [ PRIVILEGES ] }
  10. ON TABLES
  11. TO { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
  12. GRANT { { USAGE | SELECT | UPDATE }
  13. [, ...] | ALL [ PRIVILEGES ] }
  14. ON SEQUENCES
  15. TO { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
  16. GRANT { EXECUTE | ALL [ PRIVILEGES ] }
  17. ON { FUNCTIONS | ROUTINES }
  18. TO { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
  19. GRANT { USAGE | ALL [ PRIVILEGES ] }
  20. ON TYPES
  21. TO { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
  22. GRANT { USAGE | CREATE | ALL [ PRIVILEGES ] }
  23. ON SCHEMAS
  24. TO { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...] [ WITH GRANT OPTION ]
  25. REVOKE [ GRANT OPTION FOR ]
  26. { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
  27. [, ...] | ALL [ PRIVILEGES ] }
  28. ON TABLES
  29. FROM { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...]
  30. [ CASCADE | RESTRICT ]
  31. REVOKE [ GRANT OPTION FOR ]
  32. { { USAGE | SELECT | UPDATE }
  33. [, ...] | ALL [ PRIVILEGES ] }
  34. ON SEQUENCES
  35. FROM { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...]
  36. [ CASCADE | RESTRICT ]
  37. REVOKE [ GRANT OPTION FOR ]
  38. { EXECUTE | ALL [ PRIVILEGES ] }
  39. ON { FUNCTIONS | ROUTINES }
  40. FROM { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...]
  41. [ CASCADE | RESTRICT ]
  42. REVOKE [ GRANT OPTION FOR ]
  43. { USAGE | ALL [ PRIVILEGES ] }
  44. ON TYPES
  45. FROM { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...]
  46. [ CASCADE | RESTRICT ]
  47. REVOKE [ GRANT OPTION FOR ]
  48. { USAGE | CREATE | ALL [ PRIVILEGES ] }
  49. ON SCHEMAS
  50. FROM { [ GROUP ] <em class="replaceable"><code>role_name</code></em> | PUBLIC } [, ...]
  51. [ CASCADE | RESTRICT ]
  52. </pre></div><div class="refsect1" id="SQL-ALTERDEFAULTPRIVILEGES-DESCRIPTION"><h2>Description</h2><p>
  53. <code class="command">ALTER DEFAULT PRIVILEGES</code> allows you to set the privileges
  54. that will be applied to objects created in the future. (It does not
  55. affect privileges assigned to already-existing objects.) Currently,
  56. only the privileges for schemas, tables (including views and foreign
  57. tables), sequences, functions, and types (including domains) can be
  58. altered. For this command, functions include aggregates and procedures.
  59. The words <code class="literal">FUNCTIONS</code> and <code class="literal">ROUTINES</code> are
  60. equivalent in this command. (<code class="literal">ROUTINES</code> is preferred
  61. going forward as the standard term for functions and procedures taken
  62. together. In earlier PostgreSQL releases, only the
  63. word <code class="literal">FUNCTIONS</code> was allowed. It is not possible to set
  64. default privileges for functions and procedures separately.)
  65. </p><p>
  66. You can change default privileges only for objects that will be created by
  67. yourself or by roles that you are a member of. The privileges can be set
  68. globally (i.e., for all objects created in the current database),
  69. or just for objects created in specified schemas.
  70. </p><p>
  71. As explained in <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>,
  72. the default privileges for any object type normally grant all grantable
  73. permissions to the object owner, and may grant some privileges to
  74. <code class="literal">PUBLIC</code> as well. However, this behavior can be changed by
  75. altering the global default privileges with
  76. <code class="command">ALTER DEFAULT PRIVILEGES</code>.
  77. </p><p>
  78. Default privileges that are specified per-schema are added to whatever
  79. the global default privileges are for the particular object type.
  80. This means you cannot revoke privileges per-schema if they are granted
  81. globally (either by default, or according to a previous <code class="command">ALTER
  82. DEFAULT PRIVILEGES</code> command that did not specify a schema).
  83. Per-schema <code class="literal">REVOKE</code> is only useful to reverse the
  84. effects of a previous per-schema <code class="literal">GRANT</code>.
  85. </p><div class="refsect2" id="id-1.9.3.8.5.6"><h3>Parameters</h3><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>target_role</code></em></span></dt><dd><p>
  86. The name of an existing role of which the current role is a member.
  87. If <code class="literal">FOR ROLE</code> is omitted, the current role is assumed.
  88. </p></dd><dt><span class="term"><em class="replaceable"><code>schema_name</code></em></span></dt><dd><p>
  89. The name of an existing schema. If specified, the default privileges
  90. are altered for objects later created in that schema.
  91. If <code class="literal">IN SCHEMA</code> is omitted, the global default privileges
  92. are altered.
  93. <code class="literal">IN SCHEMA</code> is not allowed when setting privileges
  94. for schemas, since schemas can't be nested.
  95. </p></dd><dt><span class="term"><em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
  96. The name of an existing role to grant or revoke privileges for.
  97. This parameter, and all the other parameters in
  98. <em class="replaceable"><code>abbreviated_grant_or_revoke</code></em>,
  99. act as described under
  100. <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a> or
  101. <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a>,
  102. except that one is setting permissions for a whole class of objects
  103. rather than specific named objects.
  104. </p></dd></dl></div></div></div><div class="refsect1" id="SQL-ALTERDEFAULTPRIVILEGES-NOTES"><h2>Notes</h2><p>
  105. Use <a class="xref" href="app-psql.html" title="psql"><span class="refentrytitle"><span class="application">psql</span></span></a>'s <code class="command">\ddp</code> command
  106. to obtain information about existing assignments of default privileges.
  107. The meaning of the privilege display is the same as explained for
  108. <code class="command">\dp</code> in <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
  109. </p><p>
  110. If you wish to drop a role for which the default privileges have been
  111. altered, it is necessary to reverse the changes in its default privileges
  112. or use <code class="command">DROP OWNED BY</code> to get rid of the default privileges entry
  113. for the role.
  114. </p></div><div class="refsect1" id="SQL-ALTERDEFAULTPRIVILEGES-EXAMPLES"><h2>Examples</h2><p>
  115. Grant SELECT privilege to everyone for all tables (and views) you
  116. subsequently create in schema <code class="literal">myschema</code>, and allow
  117. role <code class="literal">webuser</code> to INSERT into them too:
  118. </p><pre class="programlisting">
  119. ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO PUBLIC;
  120. ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT INSERT ON TABLES TO webuser;
  121. </pre><p>
  122. </p><p>
  123. Undo the above, so that subsequently-created tables won't have any
  124. more permissions than normal:
  125. </p><pre class="programlisting">
  126. ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE SELECT ON TABLES FROM PUBLIC;
  127. ALTER DEFAULT PRIVILEGES IN SCHEMA myschema REVOKE INSERT ON TABLES FROM webuser;
  128. </pre><p>
  129. </p><p>
  130. Remove the public EXECUTE permission that is normally granted on functions,
  131. for all functions subsequently created by role <code class="literal">admin</code>:
  132. </p><pre class="programlisting">
  133. ALTER DEFAULT PRIVILEGES FOR ROLE admin REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
  134. </pre><p>
  135. Note however that you <span class="emphasis"><em>cannot</em></span> accomplish that effect
  136. with a command limited to a single schema. This command has no effect,
  137. unless it is undoing a matching <code class="literal">GRANT</code>:
  138. </p><pre class="programlisting">
  139. ALTER DEFAULT PRIVILEGES IN SCHEMA public REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
  140. </pre><p>
  141. That's because per-schema default privileges can only add privileges to
  142. the global setting, not remove privileges granted by it.
  143. </p></div><div class="refsect1" id="id-1.9.3.8.8"><h2>Compatibility</h2><p>
  144. There is no <code class="command">ALTER DEFAULT PRIVILEGES</code> statement in the SQL
  145. standard.
  146. </p></div><div class="refsect1" id="id-1.9.3.8.9"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a>, <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</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-alterdatabase.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-alterdomain.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER DATABASE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> ALTER DOMAIN</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1