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

References _.

Referenced by main().

159 {
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"));
177 }
const char * progname
Definition: pg_standby.c:37
#define _(x)
Definition: elog.c:84

◆ main()

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

Definition at line 22 of file dropuser.c.

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

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