1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/bin/scripts/dropuser.c
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
15 #include "common/logging.h"
16 #include "common/string.h"
17 #include "fe_utils/option_utils.h"
18 #include "fe_utils/string_utils.h"
21 static void help(const char *progname
);
25 main(int argc
, char *argv
[])
27 static int if_exists
= 0;
29 static struct option long_options
[] = {
30 {"host", required_argument
, NULL
, 'h'},
31 {"port", required_argument
, NULL
, 'p'},
32 {"username", required_argument
, NULL
, 'U'},
33 {"no-password", no_argument
, NULL
, 'w'},
34 {"password", no_argument
, NULL
, 'W'},
35 {"echo", no_argument
, NULL
, 'e'},
36 {"interactive", no_argument
, NULL
, 'i'},
37 {"if-exists", no_argument
, &if_exists
, 1},
45 char *dropuser
= NULL
;
48 char *username
= NULL
;
49 enum trivalue prompt_password
= TRI_DEFAULT
;
52 bool interactive
= false;
59 pg_logging_init(argv
[0]);
60 progname
= get_progname(argv
[0]);
61 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
63 handle_help_version_opts(argc
, argv
, "dropuser", help
);
65 while ((c
= getopt_long(argc
, argv
, "h:p:U:wWei", long_options
, &optindex
)) != -1)
70 host
= pg_strdup(optarg
);
73 port
= pg_strdup(optarg
);
76 username
= pg_strdup(optarg
);
79 prompt_password
= TRI_NO
;
82 prompt_password
= TRI_YES
;
91 /* this covers the long options */
94 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
99 switch (argc
- optind
)
104 dropuser
= argv
[optind
];
107 pg_log_error("too many command-line arguments (first is \"%s\")",
109 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
113 if (dropuser
== NULL
)
117 dropuser
= simple_prompt("Enter name of role to drop: ", true);
121 pg_log_error("missing required argument role name");
122 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
129 printf(_("Role \"%s\" will be permanently removed.\n"), dropuser
);
130 if (!yesno_prompt("Are you sure?"))
134 cparams
.dbname
= NULL
; /* this program lacks any dbname option... */
135 cparams
.pghost
= host
;
136 cparams
.pgport
= port
;
137 cparams
.pguser
= username
;
138 cparams
.prompt_password
= prompt_password
;
139 cparams
.override_dbname
= NULL
;
141 conn
= connectMaintenanceDatabase(&cparams
, progname
, echo
);
143 initPQExpBuffer(&sql
);
144 appendPQExpBuffer(&sql
, "DROP ROLE %s%s;",
145 (if_exists
? "IF EXISTS " : ""), fmtId(dropuser
));
148 printf("%s\n", sql
.data
);
149 result
= PQexec(conn
, sql
.data
);
151 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
153 pg_log_error("removal of role \"%s\" failed: %s",
154 dropuser
, PQerrorMessage(conn
));
166 help(const char *progname
)
168 printf(_("%s removes a PostgreSQL role.\n\n"), progname
);
169 printf(_("Usage:\n"));
170 printf(_(" %s [OPTION]... [ROLENAME]\n"), progname
);
171 printf(_("\nOptions:\n"));
172 printf(_(" -e, --echo show the commands being sent to the server\n"));
173 printf(_(" -i, --interactive prompt before deleting anything, and prompt for\n"
174 " role name if not specified\n"));
175 printf(_(" -V, --version output version information, then exit\n"));
176 printf(_(" --if-exists don't report error if user doesn't exist\n"));
177 printf(_(" -?, --help show this help, then exit\n"));
178 printf(_("\nConnection options:\n"));
179 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
180 printf(_(" -p, --port=PORT database server port\n"));
181 printf(_(" -U, --username=USERNAME user name to connect as (not the one to drop)\n"));
182 printf(_(" -w, --no-password never prompt for password\n"));
183 printf(_(" -W, --password force password prompt\n"));
184 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
185 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);