PostgreSQL Source Code  git master
dropdb.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
Include dependency graph for dropdb.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 169 of file dropdb.c.

170 {
171  printf(_("%s removes a PostgreSQL database.\n\n"), progname);
172  printf(_("Usage:\n"));
173  printf(_(" %s [OPTION]... DBNAME\n"), progname);
174  printf(_("\nOptions:\n"));
175  printf(_(" -e, --echo show the commands being sent to the server\n"));
176  printf(_(" -f, --force try to terminate other connections before dropping\n"));
177  printf(_(" -i, --interactive prompt before deleting anything\n"));
178  printf(_(" -V, --version output version information, then exit\n"));
179  printf(_(" --if-exists don't report error if database doesn't exist\n"));
180  printf(_(" -?, --help show this help, then exit\n"));
181  printf(_("\nConnection options:\n"));
182  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
183  printf(_(" -p, --port=PORT database server port\n"));
184  printf(_(" -U, --username=USERNAME user name to connect as\n"));
185  printf(_(" -w, --no-password never prompt for password\n"));
186  printf(_(" -W, --password force password prompt\n"));
187  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
188  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
189  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
190 }
#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 24 of file dropdb.c.

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  {"maintenance-db", required_argument, NULL, 2},
38  {"force", no_argument, NULL, 'f'},
39  {NULL, 0, NULL, 0}
40  };
41 
42  const char *progname;
43  int optindex;
44  int c;
45 
46  char *dbname = NULL;
47  char *maintenance_db = NULL;
48  char *host = NULL;
49  char *port = NULL;
50  char *username = NULL;
51  enum trivalue prompt_password = TRI_DEFAULT;
52  ConnParams cparams;
53  bool echo = false;
54  bool interactive = false;
55  bool force = false;
56 
57  PQExpBufferData sql;
58 
59  PGconn *conn;
60  PGresult *result;
61 
62  pg_logging_init(argv[0]);
63  progname = get_progname(argv[0]);
64  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
65 
66  handle_help_version_opts(argc, argv, "dropdb", help);
67 
68  while ((c = getopt_long(argc, argv, "efh:ip:U:wW", long_options, &optindex)) != -1)
69  {
70  switch (c)
71  {
72  case 'e':
73  echo = true;
74  break;
75  case 'f':
76  force = true;
77  break;
78  case 'h':
79  host = pg_strdup(optarg);
80  break;
81  case 'i':
82  interactive = true;
83  break;
84  case 'p':
86  break;
87  case 'U':
89  break;
90  case 'w':
91  prompt_password = TRI_NO;
92  break;
93  case 'W':
94  prompt_password = TRI_YES;
95  break;
96  case 0:
97  /* this covers the long options */
98  break;
99  case 2:
100  maintenance_db = pg_strdup(optarg);
101  break;
102  default:
103  /* getopt_long already emitted a complaint */
104  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
105  exit(1);
106  }
107  }
108 
109  switch (argc - optind)
110  {
111  case 0:
112  pg_log_error("missing required argument database name");
113  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
114  exit(1);
115  case 1:
116  dbname = argv[optind];
117  break;
118  default:
119  pg_log_error("too many command-line arguments (first is \"%s\")",
120  argv[optind + 1]);
121  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
122  exit(1);
123  }
124 
125  if (interactive)
126  {
127  printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
128  if (!yesno_prompt("Are you sure?"))
129  exit(0);
130  }
131 
132  initPQExpBuffer(&sql);
133 
134  appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
135  (if_exists ? "IF EXISTS " : ""),
136  fmtId(dbname),
137  force ? " WITH (FORCE)" : "");
138 
139  /* Avoid trying to drop postgres db while we are connected to it. */
140  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
141  maintenance_db = "template1";
142 
143  cparams.dbname = maintenance_db;
144  cparams.pghost = host;
145  cparams.pgport = port;
146  cparams.pguser = username;
147  cparams.prompt_password = prompt_password;
148  cparams.override_dbname = NULL;
149 
150  conn = connectMaintenanceDatabase(&cparams, progname, echo);
151 
152  if (echo)
153  printf("%s\n", sql.data);
154  result = PQexec(conn, sql.data);
155  if (PQresultStatus(result) != PGRES_COMMAND_OK)
156  {
157  pg_log_error("database removal failed: %s", PQerrorMessage(conn));
158  PQfinish(conn);
159  exit(1);
160  }
161 
162  PQclear(result);
163  PQfinish(conn);
164  exit(0);
165 }
bool yesno_prompt(const char *question)
Definition: common.c:136
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1217
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: dropdb.c:169
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7212
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4893
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:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static int port
Definition: pg_regress.c:116
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 * dbname
Definition: streamutil.c:52
PGconn * conn
Definition: streamutil.c:55
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, dbname, _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(), TRI_DEFAULT, TRI_NO, TRI_YES, username, and yesno_prompt().