PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dropuser.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
Include dependency graph for dropuser.c:

Go to the source code of this file.

Functions

static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ help()

static void help ( const char *  progname)
static

Definition at line 167 of file dropuser.c.

168{
169 printf(_("%s removes a PostgreSQL role.\n\n"), progname);
170 printf(_("Usage:\n"));
171 printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
172 printf(_("\nOptions:\n"));
173 printf(_(" -e, --echo show the commands being sent to the server\n"));
174 printf(_(" -i, --interactive prompt before deleting anything, and prompt for\n"
175 " role name if not specified\n"));
176 printf(_(" -V, --version output version information, then exit\n"));
177 printf(_(" --if-exists don't report error if user doesn't exist\n"));
178 printf(_(" -?, --help show this help, then exit\n"));
179 printf(_("\nConnection options:\n"));
180 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
181 printf(_(" -p, --port=PORT database server port\n"));
182 printf(_(" -U, --username=USERNAME user name to connect as (not the one to drop)\n"));
183 printf(_(" -w, --no-password never prompt for password\n"));
184 printf(_(" -W, --password force password prompt\n"));
185 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
186 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
187}
#define _(x)
Definition: elog.c:90
const char * progname
Definition: main.c:44
#define printf(...)
Definition: port.h:244

References _, printf, and progname.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 25 of file dropuser.c.

26{
27 static int if_exists = 0;
28
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},
38 {NULL, 0, NULL, 0}
39 };
40
41 const char *progname;
42 int optindex;
43 int c;
44
45 char *dropuser = NULL;
46 char *host = NULL;
47 char *port = NULL;
48 char *username = NULL;
49 enum trivalue prompt_password = TRI_DEFAULT;
50 ConnParams cparams;
51 bool echo = false;
52 bool interactive = false;
53
55
56 PGconn *conn;
57 PGresult *result;
58
59 pg_logging_init(argv[0]);
60 progname = get_progname(argv[0]);
61 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
62
63 handle_help_version_opts(argc, argv, "dropuser", help);
64
65 while ((c = getopt_long(argc, argv, "eh:ip:U:wW", long_options, &optindex)) != -1)
66 {
67 switch (c)
68 {
69 case 'e':
70 echo = true;
71 break;
72 case 'h':
73 host = pg_strdup(optarg);
74 break;
75 case 'i':
76 interactive = true;
77 break;
78 case 'p':
80 break;
81 case 'U':
83 break;
84 case 'w':
85 prompt_password = TRI_NO;
86 break;
87 case 'W':
88 prompt_password = TRI_YES;
89 break;
90 case 0:
91 /* this covers the long options */
92 break;
93 default:
94 /* getopt_long already emitted a complaint */
95 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
96 exit(1);
97 }
98 }
99
100 switch (argc - optind)
101 {
102 case 0:
103 break;
104 case 1:
105 dropuser = argv[optind];
106 break;
107 default:
108 pg_log_error("too many command-line arguments (first is \"%s\")",
109 argv[optind + 1]);
110 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
111 exit(1);
112 }
113
114 if (dropuser == NULL)
115 {
116 if (interactive)
117 {
118 dropuser = simple_prompt("Enter name of role to drop: ", true);
119 }
120 else
121 {
122 pg_log_error("missing required argument role name");
123 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
124 exit(1);
125 }
126 }
127
128 if (interactive)
129 {
130 printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
131 if (!yesno_prompt("Are you sure?"))
132 exit(0);
133 }
134
135 cparams.dbname = NULL; /* this program lacks any dbname option... */
136 cparams.pghost = host;
137 cparams.pgport = port;
138 cparams.pguser = username;
139 cparams.prompt_password = prompt_password;
140 cparams.override_dbname = NULL;
141
142 conn = connectMaintenanceDatabase(&cparams, progname, echo);
143
144 initPQExpBuffer(&sql);
145 appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
146 (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
147
148 if (echo)
149 printf("%s\n", sql.data);
150 result = PQexec(conn, sql.data);
151
152 if (PQresultStatus(result) != PGRES_COMMAND_OK)
153 {
154 pg_log_error("removal of role \"%s\" failed: %s",
155 dropuser, PQerrorMessage(conn));
156 PQfinish(conn);
157 exit(1);
158 }
159
160 PQclear(result);
161 PQfinish(conn);
162 exit(0);
163}
bool yesno_prompt(const char *question)
Definition: common.c:135
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1168
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void help(const char *progname)
Definition: dropuser.c:167
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4880
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7209
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
static char * username
Definition: initdb.c:153
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
static int if_exists
Definition: pg_dumpall.c:98
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static int port
Definition: pg_regress.c:115
const char * get_progname(const char *argv0)
Definition: path.c:575
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
char * c
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
PGconn * conn
Definition: streamutil.c:53
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:92
char * pgport
Definition: pg_backup.h:86
char * pghost
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:85
enum trivalue prompt_password
Definition: connect_utils.h:32
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37

References _, appendPQExpBuffer(), conn, connectMaintenanceDatabase(), PQExpBufferData::data, _connParams::dbname, exit(), fmtId(), get_progname(), getopt_long(), handle_help_version_opts(), help(), if_exists, initPQExpBuffer(), no_argument, optarg, optind, _connParams::override_dbname, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, _connParams::pghost, _connParams::pgport, PGRES_COMMAND_OK, _connParams::pguser, port, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), printf, progname, _connParams::prompt_password, required_argument, set_pglocale_pgservice(), simple_prompt(), TRI_DEFAULT, TRI_NO, TRI_YES, username, and yesno_prompt().