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

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 23 of file dropuser.c.

References _, appendPQExpBuffer(), conn, connectDatabase(), PQExpBufferData::data, fmtId(), fprintf, get_progname(), getopt_long(), handle_help_version_opts(), help(), if_exists, initPQExpBuffer(), no_argument, optarg, optind, pg_log_error, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, PGRES_COMMAND_OK, port, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), printf, progname, required_argument, set_pglocale_pgservice(), simple_prompt(), TRI_DEFAULT, TRI_NO, TRI_YES, username, and yesno_prompt().

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