Skip to content

Commit 6bea96d

Browse files
committed
Add a new option, -g, to createuser, to add membership in a role.
Chistopher Browne, reviewed by Sameer Thakur, Amit Kapila, and Peter Eisentraut.
1 parent a06af43 commit 6bea96d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

doc/src/sgml/ref/createuser.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ PostgreSQL documentation
130130
</listitem>
131131
</varlistentry>
132132

133+
<varlistentry>
134+
<term><option>-g <replaceable class="parameter">role</replaceable></></term>
135+
<term><option>--role=<replaceable class="parameter">role</replaceable></></term>
136+
<listitem>
137+
<para>
138+
Indicates role to which this role will be added immediately as a new
139+
member. Multiple roles to which this role will be added as a member
140+
can be specified by writing multiple
141+
<option>-g</> switches.
142+
</para>
143+
</listitem>
144+
</varlistentry>
145+
133146
<varlistentry>
134147
<term><option>-i</></term>
135148
<term><option>--inherit</></term>

src/bin/scripts/createuser.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ main(int argc, char *argv[])
2424
{"host", required_argument, NULL, 'h'},
2525
{"port", required_argument, NULL, 'p'},
2626
{"username", required_argument, NULL, 'U'},
27+
{"role", required_argument, NULL, 'g'},
2728
{"no-password", no_argument, NULL, 'w'},
2829
{"password", no_argument, NULL, 'W'},
2930
{"echo", no_argument, NULL, 'e'},
@@ -57,6 +58,7 @@ main(int argc, char *argv[])
5758
char *host = NULL;
5859
char *port = NULL;
5960
char *username = NULL;
61+
SimpleStringList roles = {NULL, NULL};
6062
enum trivalue prompt_password = TRI_DEFAULT;
6163
bool echo = false;
6264
bool interactive = false;
@@ -83,7 +85,7 @@ main(int argc, char *argv[])
8385

8486
handle_help_version_opts(argc, argv, "createuser", help);
8587

86-
while ((c = getopt_long(argc, argv, "h:p:U:wWedDsSaArRiIlLc:PEN",
88+
while ((c = getopt_long(argc, argv, "h:p:U:g:wWedDsSaArRiIlLc:PEN",
8789
long_options, &optindex)) != -1)
8890
{
8991
switch (c)
@@ -97,6 +99,9 @@ main(int argc, char *argv[])
9799
case 'U':
98100
username = pg_strdup(optarg);
99101
break;
102+
case 'g':
103+
simple_string_list_append(&roles, optarg);
104+
break;
100105
case 'w':
101106
prompt_password = TRI_NO;
102107
break;
@@ -302,6 +307,19 @@ main(int argc, char *argv[])
302307
appendPQExpBufferStr(&sql, " NOREPLICATION");
303308
if (conn_limit != NULL)
304309
appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit);
310+
if (roles.head != NULL)
311+
{
312+
SimpleStringListCell *cell;
313+
appendPQExpBufferStr(&sql, " IN ROLE ");
314+
315+
for (cell = roles.head; cell; cell = cell->next)
316+
{
317+
if (cell->next)
318+
appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
319+
else
320+
appendPQExpBuffer(&sql, "%s", fmtId(cell->val));
321+
}
322+
}
305323
appendPQExpBufferStr(&sql, ";\n");
306324

307325
if (echo)
@@ -334,6 +352,7 @@ help(const char *progname)
334352
printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
335353
printf(_(" -e, --echo show the commands being sent to the server\n"));
336354
printf(_(" -E, --encrypted encrypt stored password\n"));
355+
printf(_(" -g, --role=ROLE new role will be a member of this role\n"));
337356
printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
338357
" member of (default)\n"));
339358
printf(_(" -I, --no-inherit role does not inherit privileges\n"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy