]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-createrole.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / sql-createrole.html
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 Vsnapshot" /><link rel="prev" href="sql-createpublication.html" title="CREATE PUBLICATION" /><link rel="next" href="sql-createrule.html" title="CREATE RULE" /></head><body id="docContent" class="container-fluid col-10"><div 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 18.0 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 /></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
5 <span class="phrase">where <em class="replaceable"><code>option</code></em> can be:</span>
6
7       SUPERUSER | NOSUPERUSER
8     | CREATEDB | NOCREATEDB
9     | CREATEROLE | NOCREATEROLE
10     | INHERIT | NOINHERIT
11     | LOGIN | NOLOGIN
12     | REPLICATION | NOREPLICATION
13     | BYPASSRLS | NOBYPASSRLS
14     | CONNECTION LIMIT <em class="replaceable"><code>connlimit</code></em>
15     | [ ENCRYPTED ] PASSWORD '<em class="replaceable"><code>password</code></em>' | PASSWORD NULL
16     | VALID UNTIL '<em class="replaceable"><code>timestamp</code></em>'
17     | IN ROLE <em class="replaceable"><code>role_name</code></em> [, ...]
18     | ROLE <em class="replaceable"><code>role_name</code></em> [, ...]
19     | ADMIN <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><p>
34    During role creation it is possible to immediately assign the newly created
35    role to be a member of an existing role, and also assign existing roles
36    to be members of the newly created role.  The rules for which initial
37    role membership options are enabled are described below in the
38    <code class="literal">IN ROLE</code>, <code class="literal">ROLE</code>, and
39    <code class="literal">ADMIN</code> clauses.  The <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a>
40    command has fine-grained option control during membership creation,
41    and the ability to modify these options after the new role is created.
42   </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>
43         The name of the new role.
44        </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>
45         These clauses determine whether the new role is a <span class="quote">“<span class="quote">superuser</span>”</span>,
46         who can override all access restrictions within the database.
47         Superuser status is dangerous and should be used only when really
48         needed.  You must yourself be a superuser to create a new superuser.
49         If not specified,
50         <code class="literal">NOSUPERUSER</code> is the default.
51        </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>
52         These clauses define a role's ability to create databases.  If
53         <code class="literal">CREATEDB</code> is specified, the role being
54         defined will be allowed to create new databases. Specifying
55         <code class="literal">NOCREATEDB</code> will deny a role the ability to
56         create databases. If not specified,
57         <code class="literal">NOCREATEDB</code> is the default.
58         Only superuser roles or roles with <code class="literal">CREATEDB</code>
59         can specify <code class="literal">CREATEDB</code>.
60        </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>
61         These clauses determine whether a role will be permitted to
62         create, alter, drop, comment on, and change the security label for
63         other roles.
64         See <a class="xref" href="role-attributes.html#ROLE-CREATION">role creation</a> for more details about what
65         capabilities are conferred by this privilege.
66         If not specified, <code class="literal">NOCREATEROLE</code> is the default.
67        </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>
68         This affects the membership inheritance status when this
69         role is added as a member of another role, both in this and
70         future commands.  Specifically, it controls the inheritance
71         status of memberships added with this command using the
72         <code class="literal">IN ROLE</code> clause, and in later commands using
73         the <code class="literal">ROLE</code> clause.  It is also used as the
74         default inheritance status when adding this role as a member
75         using the <code class="literal">GRANT</code> command.  If not specified,
76         <code class="literal">INHERIT</code> is the default.
77        </p><p>
78         In <span class="productname">PostgreSQL</span> versions before 16,
79         inheritance was a role-level attribute that controlled all runtime
80         membership checks for that role.
81        </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>
82         These clauses determine whether a role is allowed to log in;
83         that is, whether the role can be given as the initial session
84         authorization name during client connection.  A role having
85         the <code class="literal">LOGIN</code> attribute can be thought of as a user.
86         Roles without this attribute are useful for managing database
87         privileges, but are not users in the usual sense of the word.
88         If not specified,
89         <code class="literal">NOLOGIN</code> is the default, except when
90         <code class="command">CREATE ROLE</code> is invoked through its alternative spelling
91         <a class="link" href="sql-createuser.html" title="CREATE USER"><code class="command">CREATE USER</code></a>.
92        </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>
93         These clauses determine whether a role is a replication role.  A role
94         must have this attribute (or be a superuser) in order to be able to
95         connect to the server in replication mode (physical or logical
96         replication) and in order to be able to create or drop replication
97         slots.
98         A role having the <code class="literal">REPLICATION</code> attribute is a very
99         highly privileged role, and should only be used on roles actually
100         used for replication. If not specified,
101         <code class="literal">NOREPLICATION</code> is the default.
102         Only superuser roles or roles with <code class="literal">REPLICATION</code>
103         can specify <code class="literal">REPLICATION</code>.
104        </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>
105         These clauses determine whether a role bypasses every row-level
106         security (RLS) policy.  <code class="literal">NOBYPASSRLS</code> is the default.
107         Only superuser roles or roles with <code class="literal">BYPASSRLS</code>
108         can specify <code class="literal">BYPASSRLS</code>.
109        </p><p>
110         Note that pg_dump will set <code class="literal">row_security</code> to
111         <code class="literal">OFF</code> by default, to ensure all contents of a table are
112         dumped out.  If the user running pg_dump does not have appropriate
113         permissions, an error will be returned.  However, superusers and the
114         owner of the table being dumped always bypass RLS.
115        </p></dd><dt><span class="term"><code class="literal">CONNECTION LIMIT</code> <em class="replaceable"><code>connlimit</code></em></span></dt><dd><p>
116         If role can log in, this specifies how many concurrent connections
117         the role can make.  -1 (the default) means no limit. Note that only
118         normal connections are counted towards this limit. Neither prepared
119         transactions nor background worker connections are counted towards
120         this limit.
121        </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>
122         Sets the role's password.  (A password is only of use for
123         roles having the <code class="literal">LOGIN</code> attribute, but you
124         can nonetheless define one for roles without it.)  If you do
125         not plan to use password authentication you can omit this
126         option.  If no password is specified, the password will be set
127         to null and password authentication will always fail for that
128         user.  A null password can optionally be written explicitly as
129         <code class="literal">PASSWORD NULL</code>.
130        </p><div class="note"><h3 class="title">Note</h3><p>
131            Specifying an empty string will also set the password to null,
132            but that was not the case before <span class="productname">PostgreSQL</span>
133            version 10. In earlier versions, an empty string could be used,
134            or not, depending on the authentication method and the exact
135            version, and libpq would refuse to use it in any case.
136            To avoid the ambiguity, specifying an empty string should be
137            avoided.
138          </p></div><p>
139         The password is always stored encrypted in the system catalogs. The
140         <code class="literal">ENCRYPTED</code> keyword has no effect, but is accepted for
141         backwards compatibility. The method of encryption is determined
142         by the configuration parameter <a class="xref" href="runtime-config-connection.html#GUC-PASSWORD-ENCRYPTION">password_encryption</a>.
143         If the presented password string is already in MD5-encrypted or
144         SCRAM-encrypted format, then it is stored as-is regardless of
145         <code class="varname">password_encryption</code> (since the system cannot decrypt
146         the specified encrypted password string, to encrypt it in a
147         different format).  This allows reloading of encrypted passwords
148         during dump/restore.
149        </p><div class="warning"><h3 class="title">Warning</h3><p>
150          Support for MD5-encrypted passwords is deprecated and will be removed
151          in a future release of <span class="productname">PostgreSQL</span>.  Refer
152          to <a class="xref" href="auth-password.html" title="20.5. Password Authentication">Section 20.5</a> for details about migrating to
153          another password type.
154         </p></div></dd><dt><span class="term"><code class="literal">VALID UNTIL</code> '<em class="replaceable"><code>timestamp</code></em>'</span></dt><dd><p>
155         The <code class="literal">VALID UNTIL</code> clause sets a date and
156         time after which the role's password is no longer valid.  If
157         this clause is omitted the password will be valid for all time.
158        </p></dd><dt><span class="term"><code class="literal">IN ROLE</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
159         The <code class="literal">IN ROLE</code> clause causes the new role to
160         be automatically added as a member of the specified existing
161         roles. The new membership will have the <code class="literal">SET</code>
162         option enabled and the <code class="literal">ADMIN</code> option disabled.
163         The <code class="literal">INHERIT</code> option will be enabled unless the
164         <code class="literal">NOINHERIT</code> option is specified.
165        </p></dd><dt><span class="term"><code class="literal">ROLE</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
166         The <code class="literal">ROLE</code> clause causes one or more specified
167         existing roles to be automatically added as members, with the
168         <code class="literal">SET</code> option enabled. This in effect makes the
169         new role a <span class="quote">“<span class="quote">group</span>”</span>.  Roles named in this clause
170         with the role-level <code class="literal">INHERIT</code> attribute will have
171         the <code class="literal">INHERIT</code> option enabled in the new membership.
172         New memberships will have the <code class="literal">ADMIN</code> option disabled.
173        </p></dd><dt><span class="term"><code class="literal">ADMIN</code> <em class="replaceable"><code>role_name</code></em></span></dt><dd><p>
174         The <code class="literal">ADMIN</code> clause has the same effect as
175         <code class="literal">ROLE</code>, but the named roles are added as members
176         of the new role with <code class="literal">ADMIN</code> enabled, giving
177         them the right to grant membership in the new role to others.
178        </p></dd><dt><span class="term"><code class="literal">SYSID</code> <em class="replaceable"><code>uid</code></em></span></dt><dd><p>
179         The <code class="literal">SYSID</code> clause is ignored, but is accepted
180         for backwards compatibility.
181        </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.78.7"><h2>Notes</h2><p>
182    Use <a class="link" href="sql-alterrole.html" title="ALTER ROLE"><code class="command">ALTER ROLE</code></a> to
183    change the attributes of a role, and <a class="link" href="sql-droprole.html" title="DROP ROLE"><code class="command">DROP ROLE</code></a>
184    to remove a role.  All the attributes
185    specified by <code class="command">CREATE ROLE</code> can be modified by later
186    <code class="command">ALTER ROLE</code> commands.
187   </p><p>
188    The preferred way to add and remove members of roles that are being
189    used as groups is to use
190    <a class="link" href="sql-grant.html" title="GRANT"><code class="command">GRANT</code></a> and
191    <a class="link" href="sql-revoke.html" title="REVOKE"><code class="command">REVOKE</code></a>.
192   </p><p>
193    The <code class="literal">VALID UNTIL</code> clause defines an expiration time for a
194    password only, not for the role per se.  In
195    particular, the expiration time is not enforced when logging in using
196    a non-password-based authentication method.
197   </p><p>
198    The role attributes defined here are non-inheritable, i.e., being a
199    member of a role with, e.g., <code class="literal">CREATEDB</code> will not
200    allow the member to create new databases even if the membership grant
201    has the <code class="literal">INHERIT</code> option.  Of course, if the membership
202    grant has the <code class="literal">SET</code> option the member role would be able to
203    <a class="link" href="sql-set-role.html" title="SET ROLE"><code class="command">SET ROLE</code></a> to the
204    createdb role and then create a new database.
205   </p><p>
206    The membership grants created by the
207    <code class="literal">IN ROLE</code>, <code class="literal">ROLE</code>, and <code class="literal">ADMIN</code>
208    clauses have the role executing this command as the grantor.
209   </p><p>
210    The <code class="literal">INHERIT</code> attribute is the default for reasons of backwards
211    compatibility: in prior releases of <span class="productname">PostgreSQL</span>,
212    users always had access to all privileges of groups they were members of.
213    However, <code class="literal">NOINHERIT</code> provides a closer match to the semantics
214    specified in the SQL standard.
215   </p><p>
216    <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
217    the same functionality as <code class="command">CREATE ROLE</code> (in fact,
218    it calls this command) but can be run from the command shell.
219   </p><p>
220    The <code class="literal">CONNECTION LIMIT</code> option is only enforced approximately;
221    if two new sessions start at about the same time when just one
222    connection <span class="quote">“<span class="quote">slot</span>”</span> remains for the role, it is possible that
223    both will fail.  Also, the limit is never enforced for superusers.
224   </p><p>
225    Caution must be exercised when specifying an unencrypted password
226    with this command.  The password will be transmitted to the server
227    in cleartext, and it might also be logged in the client's command
228    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
229    the password encrypted.  Also, <a class="xref" href="app-psql.html" title="psql"><span class="refentrytitle"><span class="application">psql</span></span></a>
230    contains a command
231    <code class="command">\password</code> that can be used to safely change the
232    password later.
233   </p></div><div class="refsect1" id="id-1.9.3.78.8"><h2>Examples</h2><p>
234    Create a role that can log in, but don't give it a password:
235 </p><pre class="programlisting">
236 CREATE ROLE jonathan LOGIN;
237 </pre><p>
238   </p><p>
239    Create a role with a password:
240 </p><pre class="programlisting">
241 CREATE USER davide WITH PASSWORD 'jw8s0F4';
242 </pre><p>
243    (<code class="command">CREATE USER</code> is the same as <code class="command">CREATE ROLE</code> except
244    that it implies <code class="literal">LOGIN</code>.)
245   </p><p>
246    Create a role with a password that is valid until the end of 2004.
247    After one second has ticked in 2005, the password is no longer
248    valid.
249
250 </p><pre class="programlisting">
251 CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
252 </pre><p>
253   </p><p>
254    Create a role that can create databases and manage roles:
255 </p><pre class="programlisting">
256 CREATE ROLE admin WITH CREATEDB CREATEROLE;
257 </pre></div><div class="refsect1" id="id-1.9.3.78.9"><h2>Compatibility</h2><p>
258    The <code class="command">CREATE ROLE</code> statement is in the SQL standard,
259    but the standard only requires the syntax
260 </p><pre class="synopsis">
261 CREATE ROLE <em class="replaceable"><code>name</code></em> [ WITH ADMIN <em class="replaceable"><code>role_name</code></em> ]
262 </pre><p>
263    Multiple initial administrators, and all the other options of
264    <code class="command">CREATE ROLE</code>, are
265    <span class="productname">PostgreSQL</span> extensions.
266   </p><p>
267    The SQL standard defines the concepts of users and roles, but it
268    regards them as distinct concepts and leaves all commands defining
269    users to be specified by each database implementation.  In
270    <span class="productname">PostgreSQL</span> we have chosen to unify
271    users and roles into a single kind of entity.  Roles therefore
272    have many more optional attributes than they do in the standard.
273   </p><p>
274    The behavior specified by the SQL standard is most closely approximated
275    creating SQL-standard users as <span class="productname">PostgreSQL</span>
276    roles with the <code class="literal">NOINHERIT</code> option, and SQL-standard
277    roles as <span class="productname">PostgreSQL</span> roles with the
278    <code class="literal">INHERIT</code> option.
279   </p><p>
280    The <code class="literal">USER</code> clause has the same behavior as
281    <code class="literal">ROLE</code> but has been deprecated:
282 </p><pre class="synopsis">
283 USER <em class="replaceable"><code>role_name</code></em> [, ...]
284 </pre><p>
285   </p><p>
286    The <code class="literal">IN GROUP</code> clause has the same behavior as <code class="literal">IN
287    ROLE</code> but has been deprecated:
288 </p><pre class="synopsis">
289 IN GROUP <em class="replaceable"><code>role_name</code></em> [, ...]
290 </pre><p>
291   </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>, <a class="xref" href="runtime-config-client.html#GUC-CREATEROLE-SELF-GRANT">createrole_self_grant</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" title="CREATE PUBLICATION">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createrule.html" title="CREATE RULE">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" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE RULE</td></tr></table></div></body></html>