switch (argc - optind)
{ case0: break; case1:
newuser = argv[optind]; break; default:
pg_log_error("too many command-line arguments (first is \"%s\")",
argv[optind + 1]);
pg_log_error_hint("Try \"%s --help\" for more information.", progname); exit(1);
}
if (newuser == NULL)
{ if (interactive)
{
newuser = simple_prompt("Enter name of role to add: ", true);
} else
{ if (getenv("PGUSER"))
newuser = getenv("PGUSER"); else
newuser = get_user_name_or_exit(progname);
}
}
if (pwprompt)
{ char *pw2;
newpassword = simple_prompt("Enter password for new role: ", false);
pw2 = simple_prompt("Enter it again: ", false); if (strcmp(newpassword, pw2) != 0)
{
fprintf(stderr, _("Passwords didn't match.\n")); exit(1);
}
free(pw2);
}
if (superuser == TRI_DEFAULT)
{ if (interactive && yesno_prompt("Shall the new role be a superuser?"))
superuser = TRI_YES; else
superuser = TRI_NO;
}
if (superuser == TRI_YES)
{ /* Not much point in trying to restrict a superuser */
createdb = TRI_YES;
createrole = TRI_YES;
}
if (createdb == TRI_DEFAULT)
{ if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
createdb = TRI_YES; else
createdb = TRI_NO;
}
if (createrole == TRI_DEFAULT)
{ if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
createrole = TRI_YES; else
createrole = TRI_NO;
}
if (bypassrls == TRI_DEFAULT)
bypassrls = TRI_NO;
if (replication == TRI_DEFAULT)
replication = TRI_NO;
if (inherit == TRI_DEFAULT)
inherit = TRI_YES;
if (login == TRI_DEFAULT)
login = TRI_YES;
cparams.dbname = NULL; /* this program lacks any dbname option... */
cparams.pghost = host;
cparams.pgport = port;
cparams.pguser = username;
cparams.prompt_password = prompt_password;
cparams.override_dbname = NULL;
printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser)); if (newpassword)
{ char *encrypted_password;
appendPQExpBufferStr(&sql, " PASSWORD ");
encrypted_password = PQencryptPasswordConn(conn,
newpassword,
newuser,
NULL); if (!encrypted_password)
pg_fatal("password encryption failed: %s",
PQerrorMessage(conn));
appendStringLiteralConn(&sql, encrypted_password, conn);
PQfreemem(encrypted_password);
} if (superuser == TRI_YES)
appendPQExpBufferStr(&sql, " SUPERUSER"); if (superuser == TRI_NO)
appendPQExpBufferStr(&sql, " NOSUPERUSER"); if (createdb == TRI_YES)
appendPQExpBufferStr(&sql, " CREATEDB"); if (createdb == TRI_NO)
appendPQExpBufferStr(&sql, " NOCREATEDB"); if (createrole == TRI_YES)
appendPQExpBufferStr(&sql, " CREATEROLE"); if (createrole == TRI_NO)
appendPQExpBufferStr(&sql, " NOCREATEROLE"); if (inherit == TRI_YES)
appendPQExpBufferStr(&sql, " INHERIT"); if (inherit == TRI_NO)
appendPQExpBufferStr(&sql, " NOINHERIT"); if (login == TRI_YES)
appendPQExpBufferStr(&sql, " LOGIN"); if (login == TRI_NO)
appendPQExpBufferStr(&sql, " NOLOGIN"); if (replication == TRI_YES)
appendPQExpBufferStr(&sql, " REPLICATION"); if (replication == TRI_NO)
appendPQExpBufferStr(&sql, " NOREPLICATION"); if (bypassrls == TRI_YES)
appendPQExpBufferStr(&sql, " BYPASSRLS"); if (bypassrls == TRI_NO)
appendPQExpBufferStr(&sql, " NOBYPASSRLS"); if (conn_limit >= -1)
appendPQExpBuffer(&sql, " CONNECTION LIMIT %d", conn_limit); if (pwexpiry != NULL)
{
appendPQExpBufferStr(&sql, " VALID UNTIL ");
appendStringLiteralConn(&sql, pwexpiry, conn);
} if (roles.head != NULL)
{
SimpleStringListCell *cell;
appendPQExpBufferStr(&sql, " IN ROLE ");
for (cell = roles.head; cell; cell = cell->next)
{ if (cell->next)
appendPQExpBuffer(&sql, "%s,", fmtId(cell->val)); else
appendPQExpBufferStr(&sql, fmtId(cell->val));
}
} if (members.head != NULL)
{
SimpleStringListCell *cell;
appendPQExpBufferStr(&sql, " ROLE ");
for (cell = members.head; cell; cell = cell->next)
{ if (cell->next)
appendPQExpBuffer(&sql, "%s,", fmtId(cell->val)); else
appendPQExpBufferStr(&sql, fmtId(cell->val));
}
} if (admins.head != NULL)
{
SimpleStringListCell *cell;
appendPQExpBufferStr(&sql, " ADMIN ");
for (cell = admins.head; cell; cell = cell->next)
{ if (cell->next)
appendPQExpBuffer(&sql, "%s,", fmtId(cell->val)); else
appendPQExpBufferStr(&sql, fmtId(cell->val));
}
}
appendPQExpBufferChar(&sql, ';');
if (echo)
printf("%s\n", sql.data);
result = PQexec(conn, sql.data);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
pg_log_error("creation of new role failed: %s", PQerrorMessage(conn));
PQfinish(conn); exit(1);
}
PQclear(result);
PQfinish(conn); exit(0);
}
staticvoid
help(constchar *progname)
{
printf(_("%s creates a new PostgreSQL role.\n\n"), progname);
printf(_("Usage:\n"));
printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
printf(_("\nOptions:\n"));
printf(_(" -a, --with-admin=ROLE ROLE will be a member of new role with admin\n" " option\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, --no-createdb role cannot create databases (default)\n"));
printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -g, --member-of=ROLE new role will be a member of ROLE\n"));
printf(_(" --role=ROLE (same as --member-of, deprecated)\n"));
printf(_(" -i, --inherit role inherits privileges of roles it is a\n" " member of (default)\n"));
printf(_(" -I, --no-inherit role does not inherit privileges\n"));
printf(_(" -l, --login role can login (default)\n"));
printf(_(" -L, --no-login role cannot login\n"));
printf(_(" -m, --with-member=ROLE ROLE will be a member of new role\n"));
printf(_(" -P, --pwprompt assign a password to new role\n"));
printf(_(" -r, --createrole role can create new roles\n"));
printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
printf(_(" -s, --superuser role will be superuser\n"));
printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
printf(_(" -v, --valid-until=TIMESTAMP\n" " password expiration date and time for role\n"));
printf(_(" -V, --version output version information, then exit\n"));
printf(_(" --interactive prompt for missing role name and attributes rather\n" " than using defaults\n"));
printf(_(" --bypassrls role can bypass row-level security (RLS) policy\n"));
printf(_(" --no-bypassrls role cannot bypass row-level security (RLS) policy\n" " (default)\n"));
printf(_(" --replication role can initiate replication\n"));
printf(_(" --no-replication role cannot initiate replication (default)\n"));
printf(_(" -?, --help show this help, then exit\n"));
printf(_("\nConnection options:\n"));
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
printf(_(" -p, --port=PORT database server port\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, --password force password prompt\n"));
printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-08-07)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.