1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2016, 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 "fe_utils/string_utils.h"
18 static void help(const char *progname);
22 main(int argc, char *argv[])
24 static int if_exists = 0;
26 static struct option long_options[] = {
27 {"host", required_argument, NULL, 'h'},
28 {"port", required_argument, NULL, 'p'},
29 {"username", required_argument, NULL, 'U'},
30 {"no-password", no_argument, NULL, 'w'},
31 {"password", no_argument, NULL, 'W'},
32 {"echo", no_argument, NULL, 'e'},
33 {"interactive", no_argument, NULL, 'i'},
34 {"if-exists", no_argument, &if_exists, 1},
42 char *dropuser = NULL;
45 char *username = NULL;
46 enum trivalue prompt_password = TRI_DEFAULT;
48 bool interactive = false;
49 char dropuser_buf[128];
56 progname = get_progname(argv[0]);
57 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
59 handle_help_version_opts(argc, argv, "dropuser", help);
61 while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
66 host = pg_strdup(optarg);
69 port = pg_strdup(optarg);
72 username = pg_strdup(optarg);
75 prompt_password = TRI_NO;
78 prompt_password = TRI_YES;
87 /* this covers the long options */
90 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
95 switch (argc - optind)
100 dropuser = argv[optind];
103 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
104 progname, argv[optind + 1]);
105 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
109 if (dropuser == NULL)
113 simple_prompt("Enter name of role to drop: ",
114 dropuser_buf, sizeof(dropuser_buf), true);
115 dropuser = dropuser_buf;
119 fprintf(stderr, _("%s: missing required argument role name\n"), progname);
120 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
127 printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
128 if (!yesno_prompt("Are you sure?"))
132 initPQExpBuffer(&sql);
133 appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
134 (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
136 conn = connectDatabase("postgres", host, port, username, prompt_password,
137 progname, false, false);
140 printf("%s\n", sql.data);
141 result = PQexec(conn, sql.data);
143 if (PQresultStatus(result) != PGRES_COMMAND_OK)
145 fprintf(stderr, _("%s: removal of role \"%s\" failed: %s"),
146 progname, dropuser, PQerrorMessage(conn));
158 help(const char *progname)
160 printf(_("%s removes a PostgreSQL role.\n\n"), progname);
161 printf(_("Usage:\n"));
162 printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
163 printf(_("\nOptions:\n"));
164 printf(_(" -e, --echo show the commands being sent to the server\n"));
165 printf(_(" -i, --interactive prompt before deleting anything, and prompt for\n"
166 " role name if not specified\n"));
167 printf(_(" -V, --version output version information, then exit\n"));
168 printf(_(" --if-exists don't report error if user doesn't exist\n"));
169 printf(_(" -?, --help show this help, then exit\n"));
170 printf(_("\nConnection options:\n"));
171 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
172 printf(_(" -p, --port=PORT database server port\n"));
173 printf(_(" -U, --username=USERNAME user name to connect as (not the one to drop)\n"));
174 printf(_(" -w, --no-password never prompt for password\n"));
175 printf(_(" -W, --password force password prompt\n"));
176 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));