Skip to content

Commit a347f96

Browse files
committed
createuser: Disable prompting by default
Do not prompt when options were not specified. Assume --no-createdb, --no-createrole, --no-superuser by default. Also disable prompting for user name in dropdb, unless --interactive was specified. reviewed by Josh Kupershmidt
1 parent 15ad6f1 commit a347f96

File tree

4 files changed

+70
-23
lines changed

4 files changed

+70
-23
lines changed

doc/src/sgml/ref/createuser.sgml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ PostgreSQL documentation
102102
<term><option>--no-createdb</></term>
103103
<listitem>
104104
<para>
105-
The new user will not be allowed to create databases.
105+
The new user will not be allowed to create databases. This is the
106+
default.
106107
</para>
107108
</listitem>
108109
</varlistentry>
@@ -152,6 +153,20 @@ PostgreSQL documentation
152153
</listitem>
153154
</varlistentry>
154155

156+
<varlistentry>
157+
<term><option>--interactive</></term>
158+
<listitem>
159+
<para>
160+
Prompt for the user name if none is specified on the command line, and
161+
also prompt for whichever of the options
162+
<option>-d</option>/<option>-D</option>,
163+
<option>-r</option>/<option>-R</option>,
164+
<option>-s</option>/<option>-S</option> is not specified on the command
165+
line. (This was the default behavior up to PostgreSQL 9.1.)
166+
</para>
167+
</listitem>
168+
</varlistentry>
169+
155170
<varlistentry>
156171
<term><option>-l</></term>
157172
<term><option>--login</></term>
@@ -215,7 +230,8 @@ PostgreSQL documentation
215230
<term><option>--no-createrole</></term>
216231
<listitem>
217232
<para>
218-
The new user will not be allowed to create new roles.
233+
The new user will not be allowed to create new roles. This is the
234+
default.
219235
</para>
220236
</listitem>
221237
</varlistentry>
@@ -235,7 +251,7 @@ PostgreSQL documentation
235251
<term><option>--no-superuser</></term>
236252
<listitem>
237253
<para>
238-
The new user will not be a superuser.
254+
The new user will not be a superuser. This is the default.
239255
</para>
240256
</listitem>
241257
</varlistentry>
@@ -286,11 +302,6 @@ PostgreSQL documentation
286302
</variablelist>
287303
</para>
288304

289-
<para>
290-
You will be prompted for a name and other missing information if it
291-
is not specified on the command line.
292-
</para>
293-
294305
<para>
295306
<application>createuser</application> also accepts the following
296307
command-line arguments for connection parameters:
@@ -422,6 +433,14 @@ PostgreSQL documentation
422433
server:
423434
<screen>
424435
<prompt>$ </prompt><userinput>createuser joe</userinput>
436+
</screen>
437+
</para>
438+
439+
<para>
440+
To create a user <literal>joe</literal> on the default database
441+
server with prompting for some additional attributes:
442+
<screen>
443+
<prompt>$ </prompt><userinput>createuser --interactive joe</userinput>
425444
<computeroutput>Shall the new role be a superuser? (y/n) </computeroutput><userinput>n</userinput>
426445
<computeroutput>Shall the new role be allowed to create databases? (y/n) </computeroutput><userinput>n</userinput>
427446
<computeroutput>Shall the new role be allowed to create more new roles? (y/n) </computeroutput><userinput>n</userinput>
@@ -430,7 +449,7 @@ PostgreSQL documentation
430449

431450
<para>
432451
To create the same user <literal>joe</literal> using the
433-
server on host <literal>eden</>, port 5000, avoiding the prompts and
452+
server on host <literal>eden</>, port 5000, with attributes explicitly specified,
434453
taking a look at the underlying command:
435454
<screen>
436455
<prompt>$ </prompt><userinput>createuser -h eden -p 5000 -S -D -R -e joe</userinput>

doc/src/sgml/ref/dropuser.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ PostgreSQL documentation
6262
<listitem>
6363
<para>
6464
Specifies the name of the <productname>PostgreSQL</productname> user to be removed.
65-
You will be prompted for a name if none is specified on the command line.
65+
You will be prompted for a name if none is specified on the command
66+
line and the <option>-i</option>/<option>--interactive</option> option
67+
is used.
6668
</para>
6769
</listitem>
6870
</varlistentry>
@@ -83,7 +85,8 @@ PostgreSQL documentation
8385
<term><option>--interactive</></term>
8486
<listitem>
8587
<para>
86-
Prompt for confirmation before actually removing the user.
88+
Prompt for confirmation before actually removing the user, and prompt
89+
for the user name if none is specified on the command line.
8790
</para>
8891
</listitem>
8992
</varlistentry>

src/bin/scripts/createuser.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ main(int argc, char *argv[])
3939
{"no-login", no_argument, NULL, 'L'},
4040
{"replication", no_argument, NULL, 1},
4141
{"no-replication", no_argument, NULL, 2},
42+
{"interactive", no_argument, NULL, 3},
4243
/* adduser is obsolete, undocumented spelling of superuser */
4344
{"adduser", no_argument, NULL, 'a'},
4445
{"no-adduser", no_argument, NULL, 'A'},
@@ -52,12 +53,13 @@ main(int argc, char *argv[])
5253
const char *progname;
5354
int optindex;
5455
int c;
55-
char *newuser = NULL;
56+
const char *newuser = NULL;
5657
char *host = NULL;
5758
char *port = NULL;
5859
char *username = NULL;
5960
enum trivalue prompt_password = TRI_DEFAULT;
6061
bool echo = false;
62+
bool interactive = false;
6163
char *conn_limit = NULL;
6264
bool pwprompt = false;
6365
char *newpassword = NULL;
@@ -154,6 +156,9 @@ main(int argc, char *argv[])
154156
case 2:
155157
replication = TRI_NO;
156158
break;
159+
case 3:
160+
interactive = true;
161+
break;
157162
default:
158163
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
159164
exit(1);
@@ -175,7 +180,17 @@ main(int argc, char *argv[])
175180
}
176181

177182
if (newuser == NULL)
178-
newuser = simple_prompt("Enter name of role to add: ", 128, true);
183+
{
184+
if (interactive)
185+
newuser = simple_prompt("Enter name of role to add: ", 128, true);
186+
else
187+
{
188+
if (getenv("PGUSER"))
189+
newuser = getenv("PGUSER");
190+
else
191+
newuser = get_user_name(progname);
192+
}
193+
}
179194

180195
if (pwprompt)
181196
{
@@ -195,7 +210,7 @@ main(int argc, char *argv[])
195210

196211
if (superuser == 0)
197212
{
198-
if (yesno_prompt("Shall the new role be a superuser?"))
213+
if (interactive && yesno_prompt("Shall the new role be a superuser?"))
199214
superuser = TRI_YES;
200215
else
201216
superuser = TRI_NO;
@@ -210,15 +225,15 @@ main(int argc, char *argv[])
210225

211226
if (createdb == 0)
212227
{
213-
if (yesno_prompt("Shall the new role be allowed to create databases?"))
228+
if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
214229
createdb = TRI_YES;
215230
else
216231
createdb = TRI_NO;
217232
}
218233

219234
if (createrole == 0)
220235
{
221-
if (yesno_prompt("Shall the new role be allowed to create more new roles?"))
236+
if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
222237
createrole = TRI_YES;
223238
else
224239
createrole = TRI_NO;
@@ -316,7 +331,7 @@ help(const char *progname)
316331
printf(_("\nOptions:\n"));
317332
printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n"));
318333
printf(_(" -d, --createdb role can create new databases\n"));
319-
printf(_(" -D, --no-createdb role cannot create databases\n"));
334+
printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
320335
printf(_(" -e, --echo show the commands being sent to the server\n"));
321336
printf(_(" -E, --encrypted encrypt stored password\n"));
322337
printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
@@ -327,9 +342,11 @@ help(const char *progname)
327342
printf(_(" -N, --unencrypted do not encrypt stored password\n"));
328343
printf(_(" -P, --pwprompt assign a password to new role\n"));
329344
printf(_(" -r, --createrole role can create new roles\n"));
330-
printf(_(" -R, --no-createrole role cannot create roles\n"));
345+
printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
331346
printf(_(" -s, --superuser role will be superuser\n"));
332-
printf(_(" -S, --no-superuser role will not be superuser\n"));
347+
printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
348+
printf(_(" --interactive prompt for missing role name and attributes rather\n"
349+
" than using defaults\n"));
333350
printf(_(" --replication role can initiate replication\n"));
334351
printf(_(" --no-replication role cannot initiate replication\n"));
335352
printf(_(" --help show this help, then exit\n"));
@@ -340,7 +357,5 @@ help(const char *progname)
340357
printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n"));
341358
printf(_(" -w, --no-password never prompt for password\n"));
342359
printf(_(" -W, --password force password prompt\n"));
343-
printf(_("\nIf one of -d, -D, -r, -R, -s, -S, and ROLENAME is not specified, you will\n"
344-
"be prompted interactively.\n"));
345360
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
346361
}

src/bin/scripts/dropuser.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,16 @@ main(int argc, char *argv[])
106106
}
107107

108108
if (dropuser == NULL)
109-
dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
109+
{
110+
if (interactive)
111+
dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
112+
else
113+
{
114+
fprintf(stderr, _("%s: missing required argument role name\n"), progname);
115+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
116+
exit(1);
117+
}
118+
}
110119

111120
if (interactive)
112121
{
@@ -148,7 +157,8 @@ help(const char *progname)
148157
printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
149158
printf(_("\nOptions:\n"));
150159
printf(_(" -e, --echo show the commands being sent to the server\n"));
151-
printf(_(" -i, --interactive prompt before deleting anything\n"));
160+
printf(_(" -i, --interactive prompt before deleting anything, and prompt for\n"
161+
" role name if not specified\n"));
152162
printf(_(" --if-exists don't report error if user doesn't exist\n"));
153163
printf(_(" --help show this help, then exit\n"));
154164
printf(_(" --version output version information, then exit\n"));

0 commit comments

Comments
 (0)