PostgreSQL Source Code  git master
dropuser.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.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 165 of file dropuser.c.

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 24 of file dropuser.c.

References _, appendPQExpBuffer(), conn, connectMaintenanceDatabase(), PQExpBufferData::data, _connParams::dbname, fmtId(), fprintf, get_progname(), getopt_long(), handle_help_version_opts(), help(), if_exists, initPQExpBuffer(), no_argument, optarg, optind, _connParams::override_dbname, pg_log_error, 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().

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