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/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 166 of file dropuser.c.

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 25 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().

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 
54  PQExpBufferData sql;
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, "h:p:U:wWei", long_options, &optindex)) != -1)
66  {
67  switch (c)
68  {
69  case 'h':
70  host = pg_strdup(optarg);
71  break;
72  case 'p':
73  port = pg_strdup(optarg);
74  break;
75  case 'U':
76  username = pg_strdup(optarg);
77  break;
78  case 'w':
79  prompt_password = TRI_NO;
80  break;
81  case 'W':
82  prompt_password = TRI_YES;
83  break;
84  case 'e':
85  echo = true;
86  break;
87  case 'i':
88  interactive = true;
89  break;
90  case 0:
91  /* this covers the long options */
92  break;
93  default:
94  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
95  exit(1);
96  }
97  }
98 
99  switch (argc - optind)
100  {
101  case 0:
102  break;
103  case 1:
104  dropuser = argv[optind];
105  break;
106  default:
107  pg_log_error("too many command-line arguments (first is \"%s\")",
108  argv[optind + 1]);
109  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
110  exit(1);
111  }
112 
113  if (dropuser == NULL)
114  {
115  if (interactive)
116  {
117  dropuser = simple_prompt("Enter name of role to drop: ", true);
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  cparams.dbname = NULL; /* this program lacks any dbname option... */
135  cparams.pghost = host;
136  cparams.pgport = port;
137  cparams.pguser = username;
138  cparams.prompt_password = prompt_password;
139  cparams.override_dbname = NULL;
140 
141  conn = connectMaintenanceDatabase(&cparams, progname, echo);
142 
143  initPQExpBuffer(&sql);
144  appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
145  (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
146 
147  if (echo)
148  printf("%s\n", sql.data);
149  result = PQexec(conn, sql.data);
150 
151  if (PQresultStatus(result) != PGRES_COMMAND_OK)
152  {
153  pg_log_error("removal of role \"%s\" failed: %s",
154  dropuser, PQerrorMessage(conn));
155  PQfinish(conn);
156  exit(1);
157  }
158 
159  PQclear(result);
160  PQfinish(conn);
161  exit(0);
162 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6727
bool yesno_prompt(const char *question)
Definition: common.c:136
const char * progname
Definition: main.c:46
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:4221
#define printf(...)
Definition: port.h:222
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
#define fprintf
Definition: port.h:220
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3097
char * dbname
Definition: pg_backup.h:65
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:22
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
const char * username
Definition: pgbench.c:280
char * override_dbname
Definition: pg_backup.h:72
static int port
Definition: pg_regress.c:92
char * pghost
Definition: pg_backup.h:67
enum trivalue prompt_password
Definition: connect_utils.h:32
trivalue
Definition: vacuumlo.c:34
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1215
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void help(const char *progname)
Definition: dropuser.c:166
void PQclear(PGresult *res)
Definition: fe-exec.c:680
static int if_exists
Definition: pg_dumpall.c:72
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
char * optarg
Definition: getopt.c:52
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2142
#define _(x)
Definition: elog.c:89
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
const char * pguser
Definition: connect_utils.h:31