]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_ROLE.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_ROLE.7
1 '\" t
2 .\"     Title: CREATE ROLE
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE ROLE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_ROLE \- define a new database role
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE ROLE \fIname\fR [ [ WITH ] \fIoption\fR [ \&.\&.\&. ] ]
36
37 where \fIoption\fR can be:
38
39       SUPERUSER | NOSUPERUSER
40     | CREATEDB | NOCREATEDB
41     | CREATEROLE | NOCREATEROLE
42     | INHERIT | NOINHERIT
43     | LOGIN | NOLOGIN
44     | REPLICATION | NOREPLICATION
45     | BYPASSRLS | NOBYPASSRLS
46     | CONNECTION LIMIT \fIconnlimit\fR
47     | [ ENCRYPTED ] PASSWORD \*(Aq\fIpassword\fR\*(Aq | PASSWORD NULL
48     | VALID UNTIL \*(Aq\fItimestamp\fR\*(Aq
49     | IN ROLE \fIrole_name\fR [, \&.\&.\&.]
50     | ROLE \fIrole_name\fR [, \&.\&.\&.]
51     | ADMIN \fIrole_name\fR [, \&.\&.\&.]
52     | SYSID \fIuid\fR
53 .fi
54 .SH "DESCRIPTION"
55 .PP
56 \fBCREATE ROLE\fR
57 adds a new role to a
58 PostgreSQL
59 database cluster\&. A role is an entity that can own database objects and have database privileges; a role can be considered a
60 \(lquser\(rq, a
61 \(lqgroup\(rq, or both depending on how it is used\&. Refer to
62 Chapter\ \&21
63 and
64 Chapter\ \&20
65 for information about managing users and authentication\&. You must have
66 CREATEROLE
67 privilege or be a database superuser to use this command\&.
68 .PP
69 Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster\&.
70 .PP
71 During role creation it is possible to immediately assign the newly created role to be a member of an existing role, and also assign existing roles to be members of the newly created role\&. The rules for which initial role membership options are enabled are described below in the
72 IN ROLE,
73 ROLE, and
74 ADMIN
75 clauses\&. The
76 \fBGRANT\fR(7)
77 command has fine\-grained option control during membership creation, and the ability to modify these options after the new role is created\&.
78 .SH "PARAMETERS"
79 .PP
80 \fIname\fR
81 .RS 4
82 The name of the new role\&.
83 .RE
84 .PP
85 SUPERUSER
86 .br
87 NOSUPERUSER
88 .RS 4
89 These clauses determine whether the new role is a
90 \(lqsuperuser\(rq, who can override all access restrictions within the database\&. Superuser status is dangerous and should be used only when really needed\&. You must yourself be a superuser to create a new superuser\&. If not specified,
91 NOSUPERUSER
92 is the default\&.
93 .RE
94 .PP
95 CREATEDB
96 .br
97 NOCREATEDB
98 .RS 4
99 These clauses define a role\*(Aqs ability to create databases\&. If
100 CREATEDB
101 is specified, the role being defined will be allowed to create new databases\&. Specifying
102 NOCREATEDB
103 will deny a role the ability to create databases\&. If not specified,
104 NOCREATEDB
105 is the default\&. Only superuser roles or roles with
106 CREATEDB
107 can specify
108 CREATEDB\&.
109 .RE
110 .PP
111 CREATEROLE
112 .br
113 NOCREATEROLE
114 .RS 4
115 These clauses determine whether a role will be permitted to create, alter, drop, comment on, and change the security label for other roles\&. See
116 role creation
117 for more details about what capabilities are conferred by this privilege\&. If not specified,
118 NOCREATEROLE
119 is the default\&.
120 .RE
121 .PP
122 INHERIT
123 .br
124 NOINHERIT
125 .RS 4
126 This affects the membership inheritance status when this role is added as a member of another role, both in this and future commands\&. Specifically, it controls the inheritance status of memberships added with this command using the
127 IN ROLE
128 clause, and in later commands using the
129 ROLE
130 clause\&. It is also used as the default inheritance status when adding this role as a member using the
131 GRANT
132 command\&. If not specified,
133 INHERIT
134 is the default\&.
135 .sp
136 In
137 PostgreSQL
138 versions before 16, inheritance was a role\-level attribute that controlled all runtime membership checks for that role\&.
139 .RE
140 .PP
141 LOGIN
142 .br
143 NOLOGIN
144 .RS 4
145 These clauses determine whether a role is allowed to log in; that is, whether the role can be given as the initial session authorization name during client connection\&. A role having the
146 LOGIN
147 attribute can be thought of as a user\&. Roles without this attribute are useful for managing database privileges, but are not users in the usual sense of the word\&. If not specified,
148 NOLOGIN
149 is the default, except when
150 \fBCREATE ROLE\fR
151 is invoked through its alternative spelling
152 \fBCREATE USER\fR\&.
153 .RE
154 .PP
155 REPLICATION
156 .br
157 NOREPLICATION
158 .RS 4
159 These clauses determine whether a role is a replication role\&. A role must have this attribute (or be a superuser) in order to be able to connect to the server in replication mode (physical or logical replication) and in order to be able to create or drop replication slots\&. A role having the
160 REPLICATION
161 attribute is a very highly privileged role, and should only be used on roles actually used for replication\&. If not specified,
162 NOREPLICATION
163 is the default\&. Only superuser roles or roles with
164 REPLICATION
165 can specify
166 REPLICATION\&.
167 .RE
168 .PP
169 BYPASSRLS
170 .br
171 NOBYPASSRLS
172 .RS 4
173 These clauses determine whether a role bypasses every row\-level security (RLS) policy\&.
174 NOBYPASSRLS
175 is the default\&. Only superuser roles or roles with
176 BYPASSRLS
177 can specify
178 BYPASSRLS\&.
179 .sp
180 Note that pg_dump will set
181 row_security
182 to
183 OFF
184 by default, to ensure all contents of a table are dumped out\&. If the user running pg_dump does not have appropriate permissions, an error will be returned\&. However, superusers and the owner of the table being dumped always bypass RLS\&.
185 .RE
186 .PP
187 CONNECTION LIMIT \fIconnlimit\fR
188 .RS 4
189 If role can log in, this specifies how many concurrent connections the role can make\&. \-1 (the default) means no limit\&. Note that only normal connections are counted towards this limit\&. Neither prepared transactions nor background worker connections are counted towards this limit\&.
190 .RE
191 .PP
192 [ ENCRYPTED ] PASSWORD \*(Aq\fIpassword\fR\*(Aq
193 .br
194 PASSWORD NULL
195 .RS 4
196 Sets the role\*(Aqs password\&. (A password is only of use for roles having the
197 LOGIN
198 attribute, but you can nonetheless define one for roles without it\&.) If you do not plan to use password authentication you can omit this option\&. If no password is specified, the password will be set to null and password authentication will always fail for that user\&. A null password can optionally be written explicitly as
199 PASSWORD NULL\&.
200 .if n \{\
201 .sp
202 .\}
203 .RS 4
204 .it 1 an-trap
205 .nr an-no-space-flag 1
206 .nr an-break-flag 1
207 .br
208 .ps +1
209 \fBNote\fR
210 .ps -1
211 .br
212 Specifying an empty string will also set the password to null, but that was not the case before
213 PostgreSQL
214 version 10\&. In earlier versions, an empty string could be used, or not, depending on the authentication method and the exact version, and libpq would refuse to use it in any case\&. To avoid the ambiguity, specifying an empty string should be avoided\&.
215 .sp .5v
216 .RE
217 The password is always stored encrypted in the system catalogs\&. The
218 ENCRYPTED
219 keyword has no effect, but is accepted for backwards compatibility\&. The method of encryption is determined by the configuration parameter
220 password_encryption\&. If the presented password string is already in MD5\-encrypted or SCRAM\-encrypted format, then it is stored as\-is regardless of
221 \fIpassword_encryption\fR
222 (since the system cannot decrypt the specified encrypted password string, to encrypt it in a different format)\&. This allows reloading of encrypted passwords during dump/restore\&.
223 .if n \{\
224 .sp
225 .\}
226 .RS 4
227 .it 1 an-trap
228 .nr an-no-space-flag 1
229 .nr an-break-flag 1
230 .br
231 .ps +1
232 \fBWarning\fR
233 .ps -1
234 .br
235 Support for MD5\-encrypted passwords is deprecated and will be removed in a future release of
236 PostgreSQL\&. Refer to
237 Section\ \&20.5
238 for details about migrating to another password type\&.
239 .sp .5v
240 .RE
241 .RE
242 .PP
243 VALID UNTIL \*(Aq\fItimestamp\fR\*(Aq
244 .RS 4
245 The
246 VALID UNTIL
247 clause sets a date and time after which the role\*(Aqs password is no longer valid\&. If this clause is omitted the password will be valid for all time\&.
248 .RE
249 .PP
250 IN ROLE \fIrole_name\fR
251 .RS 4
252 The
253 IN ROLE
254 clause causes the new role to be automatically added as a member of the specified existing roles\&. The new membership will have the
255 SET
256 option enabled and the
257 ADMIN
258 option disabled\&. The
259 INHERIT
260 option will be enabled unless the
261 NOINHERIT
262 option is specified\&.
263 .RE
264 .PP
265 ROLE \fIrole_name\fR
266 .RS 4
267 The
268 ROLE
269 clause causes one or more specified existing roles to be automatically added as members, with the
270 SET
271 option enabled\&. This in effect makes the new role a
272 \(lqgroup\(rq\&. Roles named in this clause with the role\-level
273 INHERIT
274 attribute will have the
275 INHERIT
276 option enabled in the new membership\&. New memberships will have the
277 ADMIN
278 option disabled\&.
279 .RE
280 .PP
281 ADMIN \fIrole_name\fR
282 .RS 4
283 The
284 ADMIN
285 clause has the same effect as
286 ROLE, but the named roles are added as members of the new role with
287 ADMIN
288 enabled, giving them the right to grant membership in the new role to others\&.
289 .RE
290 .PP
291 SYSID \fIuid\fR
292 .RS 4
293 The
294 SYSID
295 clause is ignored, but is accepted for backwards compatibility\&.
296 .RE
297 .SH "NOTES"
298 .PP
299 Use
300 \fBALTER ROLE\fR
301 to change the attributes of a role, and
302 \fBDROP ROLE\fR
303 to remove a role\&. All the attributes specified by
304 \fBCREATE ROLE\fR
305 can be modified by later
306 \fBALTER ROLE\fR
307 commands\&.
308 .PP
309 The preferred way to add and remove members of roles that are being used as groups is to use
310 \fBGRANT\fR
311 and
312 \fBREVOKE\fR\&.
313 .PP
314 The
315 VALID UNTIL
316 clause defines an expiration time for a password only, not for the role per se\&. In particular, the expiration time is not enforced when logging in using a non\-password\-based authentication method\&.
317 .PP
318 The role attributes defined here are non\-inheritable, i\&.e\&., being a member of a role with, e\&.g\&.,
319 CREATEDB
320 will not allow the member to create new databases even if the membership grant has the
321 INHERIT
322 option\&. Of course, if the membership grant has the
323 SET
324 option the member role would be able to
325 \fBSET ROLE\fR
326 to the createdb role and then create a new database\&.
327 .PP
328 The membership grants created by the
329 IN ROLE,
330 ROLE, and
331 ADMIN
332 clauses have the role executing this command as the grantor\&.
333 .PP
334 The
335 INHERIT
336 attribute is the default for reasons of backwards compatibility: in prior releases of
337 PostgreSQL, users always had access to all privileges of groups they were members of\&. However,
338 NOINHERIT
339 provides a closer match to the semantics specified in the SQL standard\&.
340 .PP
341 PostgreSQL
342 includes a program
343 \fBcreateuser\fR(1)
344 that has the same functionality as
345 \fBCREATE ROLE\fR
346 (in fact, it calls this command) but can be run from the command shell\&.
347 .PP
348 The
349 CONNECTION LIMIT
350 option is only enforced approximately; if two new sessions start at about the same time when just one connection
351 \(lqslot\(rq
352 remains for the role, it is possible that both will fail\&. Also, the limit is never enforced for superusers\&.
353 .PP
354 Caution must be exercised when specifying an unencrypted password with this command\&. The password will be transmitted to the server in cleartext, and it might also be logged in the client\*(Aqs command history or the server log\&. The command
355 \fBcreateuser\fR(1), however, transmits the password encrypted\&. Also,
356 \fBpsql\fR(1)
357 contains a command
358 \fB\epassword\fR
359 that can be used to safely change the password later\&.
360 .SH "EXAMPLES"
361 .PP
362 Create a role that can log in, but don\*(Aqt give it a password:
363 .sp
364 .if n \{\
365 .RS 4
366 .\}
367 .nf
368 CREATE ROLE jonathan LOGIN;
369 .fi
370 .if n \{\
371 .RE
372 .\}
373 .PP
374 Create a role with a password:
375 .sp
376 .if n \{\
377 .RS 4
378 .\}
379 .nf
380 CREATE USER davide WITH PASSWORD \*(Aqjw8s0F4\*(Aq;
381 .fi
382 .if n \{\
383 .RE
384 .\}
385 .sp
386 (\fBCREATE USER\fR
387 is the same as
388 \fBCREATE ROLE\fR
389 except that it implies
390 LOGIN\&.)
391 .PP
392 Create a role with a password that is valid until the end of 2004\&. After one second has ticked in 2005, the password is no longer valid\&.
393 .sp
394 .if n \{\
395 .RS 4
396 .\}
397 .nf
398 CREATE ROLE miriam WITH LOGIN PASSWORD \*(Aqjw8s0F4\*(Aq VALID UNTIL \*(Aq2005\-01\-01\*(Aq;
399 .fi
400 .if n \{\
401 .RE
402 .\}
403 .PP
404 Create a role that can create databases and manage roles:
405 .sp
406 .if n \{\
407 .RS 4
408 .\}
409 .nf
410 CREATE ROLE admin WITH CREATEDB CREATEROLE;
411 .fi
412 .if n \{\
413 .RE
414 .\}
415 .SH "COMPATIBILITY"
416 .PP
417 The
418 \fBCREATE ROLE\fR
419 statement is in the SQL standard, but the standard only requires the syntax
420 .sp
421 .if n \{\
422 .RS 4
423 .\}
424 .nf
425 CREATE ROLE \fIname\fR [ WITH ADMIN \fIrole_name\fR ]
426 .fi
427 .if n \{\
428 .RE
429 .\}
430 .sp
431 Multiple initial administrators, and all the other options of
432 \fBCREATE ROLE\fR, are
433 PostgreSQL
434 extensions\&.
435 .PP
436 The SQL standard defines the concepts of users and roles, but it regards them as distinct concepts and leaves all commands defining users to be specified by each database implementation\&. In
437 PostgreSQL
438 we have chosen to unify users and roles into a single kind of entity\&. Roles therefore have many more optional attributes than they do in the standard\&.
439 .PP
440 The behavior specified by the SQL standard is most closely approximated creating SQL\-standard users as
441 PostgreSQL
442 roles with the
443 NOINHERIT
444 option, and SQL\-standard roles as
445 PostgreSQL
446 roles with the
447 INHERIT
448 option\&.
449 .PP
450 The
451 USER
452 clause has the same behavior as
453 ROLE
454 but has been deprecated:
455 .sp
456 .if n \{\
457 .RS 4
458 .\}
459 .nf
460 USER \fIrole_name\fR [, \&.\&.\&.]
461 .fi
462 .if n \{\
463 .RE
464 .\}
465 .PP
466 The
467 IN GROUP
468 clause has the same behavior as
469 IN ROLE
470 but has been deprecated:
471 .sp
472 .if n \{\
473 .RS 4
474 .\}
475 .nf
476 IN GROUP \fIrole_name\fR [, \&.\&.\&.]
477 .fi
478 .if n \{\
479 .RE
480 .\}
481 .sp
482 .SH "SEE ALSO"
483 SET ROLE (\fBSET_ROLE\fR(7)), ALTER ROLE (\fBALTER_ROLE\fR(7)), DROP ROLE (\fBDROP_ROLE\fR(7)), \fBGRANT\fR(7), \fBREVOKE\fR(7), \fBcreateuser\fR(1), createrole_self_grant