gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

264 rindas
22KB

  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>CREATE ROLE</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-createpublication.html" title="CREATE PUBLICATION" /><link rel="next" href="sql-createrule.html" title="CREATE RULE" /></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">CREATE ROLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createpublication.html" title="CREATE PUBLICATION">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-createrule.html" title="CREATE RULE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEROLE"><div class="titlepage"></div><a id="id-1.9.3.78.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE ROLE</span></h2><p>CREATE ROLE — define a new database role</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE ROLE <em class="replaceable"><code>name</code></em> [ [ WITH ] <em class="replaceable"><code>option</code></em> [ ... ] ]
  4. <span class="phrase">where <em class="replaceable"><code>option</code></em> can be:</span>
  5. SUPERUSER | NOSUPERUSER
  6. | CREATEDB | NOCREATEDB
  7. | CREATEROLE | NOCREATEROLE
  8. | INHERIT | NOINHERIT
  9. | LOGIN | NOLOGIN
  10. | REPLICATION | NOREPLICATION
  11. | BYPASSRLS | NOBYPASSRLS
  12. | CONNECTION LIMIT <em class="replaceable"><code>connlimit</code></em>
  13. | [ ENCRYPTED ] PASSWORD '<em class="replaceable"><code>password</code></em>' | PASSWORD NULL
  14. | VALID UNTIL '<em class="replaceable"><code>timestamp</code></em>'
  15. | IN ROLE <em class="replaceable"><code>role_name</code></em> [, ...]
  16. | IN GROUP <em class="replaceable"><code>role_name</code></em> [, ...]
  17. | ROLE <em class="replaceable"><code>role_name</code></em> [, ...]
  18. | ADMIN <em class="replaceable"><code>role_name</code></em> [, ...]
  19. | USER <em class="replaceable"><code>role_name</code></em> [, ...]
  20. | SYSID <em class="replaceable"><code>uid</code></em>
  21. </pre></div><div class="refsect1" id="id-1.9.3.78.5"><h2>Description</h2><p>
  22. <code class="command">CREATE ROLE</code> adds a new role to a
  23. <span class="productname">PostgreSQL</span> database cluster. A role is
  24. an entity that can own database objects and have database privileges;
  25. a role can be considered a <span class="quote">“<span class="quote">user</span>”</span>, a <span class="quote">“<span class="quote">group</span>”</span>, or both
  26. depending on how it is used. Refer to
  27. <a class="xref" href="user-manag.html" title="Chapter 21. Database Roles">Chapter 21</a> and <a class="xref" href="client-authentication.html" title="Chapter 20. Client Authentication">Chapter 20</a> for information about managing
  28. users and authentication. You must have <code class="literal">CREATEROLE</code>
  29. privilege or be a database superuser to use this command.
  30. </p><p>
  31. Note that roles are defined at the database cluster
  32. level, and so are valid in all databases in the cluster.
  33. </p></div><div class="refsect1" id="id-1.9.3.78.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  34. The name of the new role.
  35. </p></dd><dt><span class="term"><code class="literal">SUPERUSER</code><br /></span><span class="term"><code class="literal">NOSUPERUSER</code></span></dt><dd><p>
  36. These clauses determine whether the new role is a <span class="quote">“<span class="quote">superuser</span>”</span>,
  37. who can override all access restrictions within the database.
  38. Superuser status is dangerous and should be used only when really
  39. needed. You must yourself be a superuser to create a new superuser.
  40. If not specified,
  41. <code class="literal">NOSUPERUSER</code> is the default.
  42. </p></dd><dt><span class="term"><code class="literal">CREATEDB</code><br /></span><span class="term"><code class="literal">NOCREATEDB</code></span></dt><dd><p>
  43. These clauses define a role's ability to create databases. If
  44. <code class="literal">CREATEDB</code> is specified, the role being
  45. defined will be allowed to create new databases. Specifying
  46. <code class="literal">NOCREATEDB</code> will deny a role the ability to
  47. create databases. If not specified,
  48. <code class="literal">NOCREATEDB</code> is the default.
  49. </p></dd><dt><span class="term"><code class="literal">CREATEROLE</code><br /></span><span class="term"><code class="literal">NOCREATEROLE</code></span></dt><dd><p>
  50. These clauses determine whether a role will be permitted to
  51. create new roles (that is, execute <code class="command">CREATE ROLE</code>).
  52. A role with <code class="literal">CREATEROLE</code> privilege can also alter
  53. and drop other roles.
  54. If not specified,
  55. <code class="literal">NOCREATEROLE</code> is the default.
  56. </p></dd><dt><span class="term"><code class="literal">INHERIT</code><br /></span><span class="term"><code class="literal">NOINHERIT</code></span></dt><dd><p>
  57. These clauses determine whether a role <span class="quote">“<span class="quote">inherits</span>”</span> the
  58. privileges of roles it is a member of.
  59. A role with the <code class="literal">INHERIT</code> attribute can automatically
  60. use whatever database privileges have been granted to all roles
  61. it is directly or indirectly a member of.
  62. Without <code class="literal">INHERIT</code>, membership in another role
  63. only grants the ability to <code class="command">SET ROLE</code> to that other role;
  64. the privileges of the other role are only available after having
  65. done so.
  66. If not specified,
  67. <code class="literal">INHERIT</code> is the default.
  68. </p></dd><dt><span class="term"><code class="literal">LOGIN</code><br /></span><span class="term"><code class="literal">NOLOGIN</code></span></dt><dd><p>
  69. These clauses determine whether a role is allowed to log in;
  70. that is, whether the role can be given as the initial session
  71. authorization name during client connection. A role having
  72. the <code class="literal">LOGIN</code> attribute can be thought of as a user.
  73. Roles without this attribute are useful for managing database
  74. privileges, but are not users in the usual sense of the word.
  75. If not specified,
  76. <code class="literal">NOLOGIN</code> is the default, except when
  77. <code class="command">CREATE ROLE</code> is invoked through its alternative spelling
  78. <a class="xref" href="sql-createuser.html" title="CREATE USER"><span class="refentrytitle">CREATE USER</span></a>.
  79. </p></dd><dt><span class="term"><code class="literal">REPLICATION</code><br /></span><span class="term"><code class="literal">NOREPLICATION</code></span></dt><dd><p>
  80. These clauses determine whether a role is a replication role. A role
  81. must have this attribute (or be a superuser) in order to be able to
  82. connect to the server in replication mode (physical or logical
  83. replication) and in order to be able to create or drop replication
  84. slots.
  85. A role having the <code class="literal">REPLICATION</code> attribute is a very
  86. highly privileged role, and should only be used on roles actually
  87. used for replication. If not specified,
  88. <code class="literal">NOREPLICATION</code> is the default.
  89. </p></dd><dt><span class="term"><code class="literal">BYPASSRLS</code><br /></span><span class="term"><code class="literal">NOBYPASSRLS</code></span></dt><dd><p>
  90. These clauses determine whether a role bypasses every row-level
  91. security (RLS) policy. <code class="literal">NOBYPASSRLS</code> is the default.
  92. Note that pg_dump will set <code class="literal">row_security</code> to
  93. <code class="literal">OFF</code> by default, to ensure all contents of a table are
  94. dumped out. If the user running pg_dump does not have appropriate
  95. permissions, an error will be returned. The superuser and owner of the
  96. table being dumped always bypass RLS.
  97. </p></dd><dt><span class="term"><code class="literal">CONNECTION LIMIT</code> <em class="replaceable"><code>connlimit</code></em></span></dt><dd><p>
  98. If role can log in, this specifies how many concurrent connections
  99. the role can make. -1 (the default) means no limit. Note that only
  100. normal connections are counted towards this limit. Neither prepared
  101. transactions nor background worker connections are counted towards
  102. this limit.
  103. </p></dd><dt><span class="term">[ <code class="literal">ENCRYPTED</code> ] <code class="literal">PASSWORD</code> '<em class="replaceable"><code>password</code></em>'<br /></span><span class="term"><code class="literal">PASSWORD NULL</code></span></dt><dd><p>
  104. Sets the role's password. (A password is only of use for
  105. roles having the <code class="literal">LOGIN</code> attribute, but you
  106. can nonetheless define one for roles without it.) If you do
  107. not plan to use password authentication you can omit this
  108. option. If no password is specified, the password will be set
  109. to null and password authentication will always fail for that
  110. user. A null password can optionally be written explicitly as
  111. <code class="literal">PASSWORD NULL</code>.
  112. </p><div class="note"><h3 class="title">Note</h3><p>
  113. Specifying an empty string will also set the password to null,
  114. but that was not the case before <span class="productname">PostgreSQL</span>
  115. version 10. In earlier versions, an empty string could be used,
  116. or not, depending on the authentication method and the exact
  117. version, and libpq would refuse to use it in any case.
  118. To avoid the ambiguity, specifying an empty string should be
  119. avoided.
  120. </p></div><p>
  121. The password is always stored encrypted in the system catalogs. The
  122. <code class="literal">ENCRYPTED</code> keyword has no effect, but is accepted for
  123. backwards compatibility. The method of encryption is determined
  124. by the configuration parameter <a class="xref" href="runtime-config-connection.html#GUC-PASSWORD-ENCRYPTION">password_encryption</a>.
  125. If the presented password string is already in MD5-encrypted or
  126. SCRAM-encrypted format, then it is stored as-is regardless of
  127. <code class="varname">password_encryption</code> (since the system cannot decrypt
  128. the specified encrypted password string, to encrypt it in a
  129. different format). This allows reloading of encrypted passwords
  130. during dump/restore.
  131. </p></dd><dt><span class="term"><code class="literal">VALID UNTIL</code> '<em class="replaceable"><code>timestamp</code></em>'</span></dt><dd><p>
  132. The <code class="literal">VALID UNTIL</code> clause sets a date and
  133. time after which the role's password is no longer valid. If
  134. this clause is omitted the password will be valid for all time.
  135. </p></dd><dt><span class="term"><code class="literal">IN ROLE</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
  136. The <code class="literal">IN ROLE</code> clause lists one or more existing
  137. roles to which the new role will be immediately added as a new
  138. member. (Note that there is no option to add the new role as an
  139. administrator; use a separate <code class="command">GRANT</code> command to do that.)
  140. </p></dd><dt><span class="term"><code class="literal">IN GROUP</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p><code class="literal">IN GROUP</code> is an obsolete spelling of
  141. <code class="literal">IN ROLE</code>.
  142. </p></dd><dt><span class="term"><code class="literal">ROLE</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
  143. The <code class="literal">ROLE</code> clause lists one or more existing
  144. roles which are automatically added as members of the new role.
  145. (This in effect makes the new role a <span class="quote">“<span class="quote">group</span>”</span>.)
  146. </p></dd><dt><span class="term"><code class="literal">ADMIN</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
  147. The <code class="literal">ADMIN</code> clause is like <code class="literal">ROLE</code>,
  148. but the named roles are added to the new role <code class="literal">WITH ADMIN
  149. OPTION</code>, giving them the right to grant membership in this role
  150. to others.
  151. </p></dd><dt><span class="term"><code class="literal">USER</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
  152. The <code class="literal">USER</code> clause is an obsolete spelling of
  153. the <code class="literal">ROLE</code> clause.
  154. </p></dd><dt><span class="term"><code class="literal">SYSID</code> <em class="replaceable"><code>uid</code></em></span></dt><dd><p>
  155. The <code class="literal">SYSID</code> clause is ignored, but is accepted
  156. for backwards compatibility.
  157. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.78.7"><h2>Notes</h2><p>
  158. Use <a class="xref" href="sql-alterrole.html" title="ALTER ROLE"><span class="refentrytitle">ALTER ROLE</span></a> to
  159. change the attributes of a role, and <a class="xref" href="sql-droprole.html" title="DROP ROLE"><span class="refentrytitle">DROP ROLE</span></a>
  160. to remove a role. All the attributes
  161. specified by <code class="command">CREATE ROLE</code> can be modified by later
  162. <code class="command">ALTER ROLE</code> commands.
  163. </p><p>
  164. The preferred way to add and remove members of roles that are being
  165. used as groups is to use
  166. <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a> and
  167. <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a>.
  168. </p><p>
  169. The <code class="literal">VALID UNTIL</code> clause defines an expiration time for a
  170. password only, not for the role <span class="foreignphrase"><em class="foreignphrase">per se</em></span>. In
  171. particular, the expiration time is not enforced when logging in using
  172. a non-password-based authentication method.
  173. </p><p>
  174. The <code class="literal">INHERIT</code> attribute governs inheritance of grantable
  175. privileges (that is, access privileges for database objects and role
  176. memberships). It does not apply to the special role attributes set by
  177. <code class="command">CREATE ROLE</code> and <code class="command">ALTER ROLE</code>. For example, being
  178. a member of a role with <code class="literal">CREATEDB</code> privilege does not immediately
  179. grant the ability to create databases, even if <code class="literal">INHERIT</code> is set;
  180. it would be necessary to become that role via
  181. <a class="xref" href="sql-set-role.html" title="SET ROLE"><span class="refentrytitle">SET ROLE</span></a> before
  182. creating a database.
  183. </p><p>
  184. The <code class="literal">INHERIT</code> attribute is the default for reasons of backwards
  185. compatibility: in prior releases of <span class="productname">PostgreSQL</span>,
  186. users always had access to all privileges of groups they were members of.
  187. However, <code class="literal">NOINHERIT</code> provides a closer match to the semantics
  188. specified in the SQL standard.
  189. </p><p>
  190. Be careful with the <code class="literal">CREATEROLE</code> privilege. There is no concept of
  191. inheritance for the privileges of a <code class="literal">CREATEROLE</code>-role. That
  192. means that even if a role does not have a certain privilege but is allowed
  193. to create other roles, it can easily create another role with different
  194. privileges than its own (except for creating roles with superuser
  195. privileges). For example, if the role <span class="quote">“<span class="quote">user</span>”</span> has the
  196. <code class="literal">CREATEROLE</code> privilege but not the <code class="literal">CREATEDB</code> privilege,
  197. nonetheless it can create a new role with the <code class="literal">CREATEDB</code>
  198. privilege. Therefore, regard roles that have the <code class="literal">CREATEROLE</code>
  199. privilege as almost-superuser-roles.
  200. </p><p>
  201. <span class="productname">PostgreSQL</span> includes a program <a class="xref" href="app-createuser.html" title="createuser"><span class="refentrytitle"><span class="application">createuser</span></span></a> that has
  202. the same functionality as <code class="command">CREATE ROLE</code> (in fact,
  203. it calls this command) but can be run from the command shell.
  204. </p><p>
  205. The <code class="literal">CONNECTION LIMIT</code> option is only enforced approximately;
  206. if two new sessions start at about the same time when just one
  207. connection <span class="quote">“<span class="quote">slot</span>”</span> remains for the role, it is possible that
  208. both will fail. Also, the limit is never enforced for superusers.
  209. </p><p>
  210. Caution must be exercised when specifying an unencrypted password
  211. with this command. The password will be transmitted to the server
  212. in cleartext, and it might also be logged in the client's command
  213. history or the server log. The command <a class="xref" href="app-createuser.html" title="createuser"><span class="refentrytitle"><span class="application">createuser</span></span></a>, however, transmits
  214. the password encrypted. Also, <a class="xref" href="app-psql.html" title="psql"><span class="refentrytitle"><span class="application">psql</span></span></a>
  215. contains a command
  216. <code class="command">\password</code> that can be used to safely change the
  217. password later.
  218. </p></div><div class="refsect1" id="id-1.9.3.78.8"><h2>Examples</h2><p>
  219. Create a role that can log in, but don't give it a password:
  220. </p><pre class="programlisting">
  221. CREATE ROLE jonathan LOGIN;
  222. </pre><p>
  223. </p><p>
  224. Create a role with a password:
  225. </p><pre class="programlisting">
  226. CREATE USER davide WITH PASSWORD 'jw8s0F4';
  227. </pre><p>
  228. (<code class="command">CREATE USER</code> is the same as <code class="command">CREATE ROLE</code> except
  229. that it implies <code class="literal">LOGIN</code>.)
  230. </p><p>
  231. Create a role with a password that is valid until the end of 2004.
  232. After one second has ticked in 2005, the password is no longer
  233. valid.
  234. </p><pre class="programlisting">
  235. CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
  236. </pre><p>
  237. </p><p>
  238. Create a role that can create databases and manage roles:
  239. </p><pre class="programlisting">
  240. CREATE ROLE admin WITH CREATEDB CREATEROLE;
  241. </pre></div><div class="refsect1" id="id-1.9.3.78.9"><h2>Compatibility</h2><p>
  242. The <code class="command">CREATE ROLE</code> statement is in the SQL standard,
  243. but the standard only requires the syntax
  244. </p><pre class="synopsis">
  245. CREATE ROLE <em class="replaceable"><code>name</code></em> [ WITH ADMIN <em class="replaceable"><code>role_name</code></em> ]
  246. </pre><p>
  247. Multiple initial administrators, and all the other options of
  248. <code class="command">CREATE ROLE</code>, are
  249. <span class="productname">PostgreSQL</span> extensions.
  250. </p><p>
  251. The SQL standard defines the concepts of users and roles, but it
  252. regards them as distinct concepts and leaves all commands defining
  253. users to be specified by each database implementation. In
  254. <span class="productname">PostgreSQL</span> we have chosen to unify
  255. users and roles into a single kind of entity. Roles therefore
  256. have many more optional attributes than they do in the standard.
  257. </p><p>
  258. The behavior specified by the SQL standard is most closely approximated
  259. by giving users the <code class="literal">NOINHERIT</code> attribute, while roles are
  260. given the <code class="literal">INHERIT</code> attribute.
  261. </p></div><div class="refsect1" id="id-1.9.3.78.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-set-role.html" title="SET ROLE"><span class="refentrytitle">SET ROLE</span></a>, <a class="xref" href="sql-alterrole.html" title="ALTER ROLE"><span class="refentrytitle">ALTER ROLE</span></a>, <a class="xref" href="sql-droprole.html" title="DROP ROLE"><span class="refentrytitle">DROP ROLE</span></a>, <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>, <a class="xref" href="app-createuser.html" title="createuser"><span class="refentrytitle"><span class="application">createuser</span></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-createpublication.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-createrule.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE PUBLICATION </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE RULE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1