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
This commit is contained in:
Peter Eisentraut 2012-02-07 14:55:34 +02:00
parent 15ad6f1510
commit a347f96b99
4 changed files with 70 additions and 23 deletions

View File

@ -102,7 +102,8 @@ PostgreSQL documentation
<term><option>--no-createdb</></term> <term><option>--no-createdb</></term>
<listitem> <listitem>
<para> <para>
The new user will not be allowed to create databases. The new user will not be allowed to create databases. This is the
default.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -152,6 +153,20 @@ PostgreSQL documentation
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--interactive</></term>
<listitem>
<para>
Prompt for the user name if none is specified on the command line, and
also prompt for whichever of the options
<option>-d</option>/<option>-D</option>,
<option>-r</option>/<option>-R</option>,
<option>-s</option>/<option>-S</option> is not specified on the command
line. (This was the default behavior up to PostgreSQL 9.1.)
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-l</></term> <term><option>-l</></term>
<term><option>--login</></term> <term><option>--login</></term>
@ -215,7 +230,8 @@ PostgreSQL documentation
<term><option>--no-createrole</></term> <term><option>--no-createrole</></term>
<listitem> <listitem>
<para> <para>
The new user will not be allowed to create new roles. The new user will not be allowed to create new roles. This is the
default.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -235,7 +251,7 @@ PostgreSQL documentation
<term><option>--no-superuser</></term> <term><option>--no-superuser</></term>
<listitem> <listitem>
<para> <para>
The new user will not be a superuser. The new user will not be a superuser. This is the default.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -286,11 +302,6 @@ PostgreSQL documentation
</variablelist> </variablelist>
</para> </para>
<para>
You will be prompted for a name and other missing information if it
is not specified on the command line.
</para>
<para> <para>
<application>createuser</application> also accepts the following <application>createuser</application> also accepts the following
command-line arguments for connection parameters: command-line arguments for connection parameters:
@ -422,6 +433,14 @@ PostgreSQL documentation
server: server:
<screen> <screen>
<prompt>$ </prompt><userinput>createuser joe</userinput> <prompt>$ </prompt><userinput>createuser joe</userinput>
</screen>
</para>
<para>
To create a user <literal>joe</literal> on the default database
server with prompting for some additional attributes:
<screen>
<prompt>$ </prompt><userinput>createuser --interactive joe</userinput>
<computeroutput>Shall the new role be a superuser? (y/n) </computeroutput><userinput>n</userinput> <computeroutput>Shall the new role be a superuser? (y/n) </computeroutput><userinput>n</userinput>
<computeroutput>Shall the new role be allowed to create databases? (y/n) </computeroutput><userinput>n</userinput> <computeroutput>Shall the new role be allowed to create databases? (y/n) </computeroutput><userinput>n</userinput>
<computeroutput>Shall the new role be allowed to create more new roles? (y/n) </computeroutput><userinput>n</userinput> <computeroutput>Shall the new role be allowed to create more new roles? (y/n) </computeroutput><userinput>n</userinput>
@ -430,7 +449,7 @@ PostgreSQL documentation
<para> <para>
To create the same user <literal>joe</literal> using the To create the same user <literal>joe</literal> using the
server on host <literal>eden</>, port 5000, avoiding the prompts and server on host <literal>eden</>, port 5000, with attributes explicitly specified,
taking a look at the underlying command: taking a look at the underlying command:
<screen> <screen>
<prompt>$ </prompt><userinput>createuser -h eden -p 5000 -S -D -R -e joe</userinput> <prompt>$ </prompt><userinput>createuser -h eden -p 5000 -S -D -R -e joe</userinput>

View File

@ -62,7 +62,9 @@ PostgreSQL documentation
<listitem> <listitem>
<para> <para>
Specifies the name of the <productname>PostgreSQL</productname> user to be removed. Specifies the name of the <productname>PostgreSQL</productname> user to be removed.
You will be prompted for a name if none is specified on the command line. You will be prompted for a name if none is specified on the command
line and the <option>-i</option>/<option>--interactive</option> option
is used.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -83,7 +85,8 @@ PostgreSQL documentation
<term><option>--interactive</></term> <term><option>--interactive</></term>
<listitem> <listitem>
<para> <para>
Prompt for confirmation before actually removing the user. Prompt for confirmation before actually removing the user, and prompt
for the user name if none is specified on the command line.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View File

@ -39,6 +39,7 @@ main(int argc, char *argv[])
{"no-login", no_argument, NULL, 'L'}, {"no-login", no_argument, NULL, 'L'},
{"replication", no_argument, NULL, 1}, {"replication", no_argument, NULL, 1},
{"no-replication", no_argument, NULL, 2}, {"no-replication", no_argument, NULL, 2},
{"interactive", no_argument, NULL, 3},
/* adduser is obsolete, undocumented spelling of superuser */ /* adduser is obsolete, undocumented spelling of superuser */
{"adduser", no_argument, NULL, 'a'}, {"adduser", no_argument, NULL, 'a'},
{"no-adduser", no_argument, NULL, 'A'}, {"no-adduser", no_argument, NULL, 'A'},
@ -52,12 +53,13 @@ main(int argc, char *argv[])
const char *progname; const char *progname;
int optindex; int optindex;
int c; int c;
char *newuser = NULL; const char *newuser = NULL;
char *host = NULL; char *host = NULL;
char *port = NULL; char *port = NULL;
char *username = NULL; char *username = NULL;
enum trivalue prompt_password = TRI_DEFAULT; enum trivalue prompt_password = TRI_DEFAULT;
bool echo = false; bool echo = false;
bool interactive = false;
char *conn_limit = NULL; char *conn_limit = NULL;
bool pwprompt = false; bool pwprompt = false;
char *newpassword = NULL; char *newpassword = NULL;
@ -154,6 +156,9 @@ main(int argc, char *argv[])
case 2: case 2:
replication = TRI_NO; replication = TRI_NO;
break; break;
case 3:
interactive = true;
break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1); exit(1);
@ -175,7 +180,17 @@ main(int argc, char *argv[])
} }
if (newuser == NULL) if (newuser == NULL)
newuser = simple_prompt("Enter name of role to add: ", 128, true); {
if (interactive)
newuser = simple_prompt("Enter name of role to add: ", 128, true);
else
{
if (getenv("PGUSER"))
newuser = getenv("PGUSER");
else
newuser = get_user_name(progname);
}
}
if (pwprompt) if (pwprompt)
{ {
@ -195,7 +210,7 @@ main(int argc, char *argv[])
if (superuser == 0) if (superuser == 0)
{ {
if (yesno_prompt("Shall the new role be a superuser?")) if (interactive && yesno_prompt("Shall the new role be a superuser?"))
superuser = TRI_YES; superuser = TRI_YES;
else else
superuser = TRI_NO; superuser = TRI_NO;
@ -210,7 +225,7 @@ main(int argc, char *argv[])
if (createdb == 0) if (createdb == 0)
{ {
if (yesno_prompt("Shall the new role be allowed to create databases?")) if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
createdb = TRI_YES; createdb = TRI_YES;
else else
createdb = TRI_NO; createdb = TRI_NO;
@ -218,7 +233,7 @@ main(int argc, char *argv[])
if (createrole == 0) if (createrole == 0)
{ {
if (yesno_prompt("Shall the new role be allowed to create more new roles?")) if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
createrole = TRI_YES; createrole = TRI_YES;
else else
createrole = TRI_NO; createrole = TRI_NO;
@ -316,7 +331,7 @@ help(const char *progname)
printf(_("\nOptions:\n")); printf(_("\nOptions:\n"));
printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n")); printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n"));
printf(_(" -d, --createdb role can create new databases\n")); printf(_(" -d, --createdb role can create new databases\n"));
printf(_(" -D, --no-createdb role cannot create databases\n")); printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -E, --encrypted encrypt stored password\n")); printf(_(" -E, --encrypted encrypt stored password\n"));
printf(_(" -i, --inherit role inherits privileges of roles it is a\n" printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
@ -327,9 +342,11 @@ help(const char *progname)
printf(_(" -N, --unencrypted do not encrypt stored password\n")); printf(_(" -N, --unencrypted do not encrypt stored password\n"));
printf(_(" -P, --pwprompt assign a password to new role\n")); printf(_(" -P, --pwprompt assign a password to new role\n"));
printf(_(" -r, --createrole role can create new roles\n")); printf(_(" -r, --createrole role can create new roles\n"));
printf(_(" -R, --no-createrole role cannot create roles\n")); printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
printf(_(" -s, --superuser role will be superuser\n")); printf(_(" -s, --superuser role will be superuser\n"));
printf(_(" -S, --no-superuser role will not be superuser\n")); printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
printf(_(" --interactive prompt for missing role name and attributes rather\n"
" than using defaults\n"));
printf(_(" --replication role can initiate replication\n")); printf(_(" --replication role can initiate replication\n"));
printf(_(" --no-replication role cannot initiate replication\n")); printf(_(" --no-replication role cannot initiate replication\n"));
printf(_(" --help show this help, then exit\n")); printf(_(" --help show this help, then exit\n"));
@ -340,7 +357,5 @@ help(const char *progname)
printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n")); printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n"));
printf(_(" -w, --no-password never prompt for password\n")); printf(_(" -w, --no-password never prompt for password\n"));
printf(_(" -W, --password force password prompt\n")); printf(_(" -W, --password force password prompt\n"));
printf(_("\nIf one of -d, -D, -r, -R, -s, -S, and ROLENAME is not specified, you will\n"
"be prompted interactively.\n"));
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n")); printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
} }

View File

@ -106,7 +106,16 @@ main(int argc, char *argv[])
} }
if (dropuser == NULL) if (dropuser == NULL)
dropuser = simple_prompt("Enter name of role to drop: ", 128, true); {
if (interactive)
dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
else
{
fprintf(stderr, _("%s: missing required argument role name\n"), progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
}
if (interactive) if (interactive)
{ {
@ -148,7 +157,8 @@ help(const char *progname)
printf(_(" %s [OPTION]... [ROLENAME]\n"), progname); printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
printf(_("\nOptions:\n")); printf(_("\nOptions:\n"));
printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -i, --interactive prompt before deleting anything\n")); printf(_(" -i, --interactive prompt before deleting anything, and prompt for\n"
" role name if not specified\n"));
printf(_(" --if-exists don't report error if user doesn't exist\n")); printf(_(" --if-exists don't report error if user doesn't exist\n"));
printf(_(" --help show this help, then exit\n")); printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n")); printf(_(" --version output version information, then exit\n"));