PostgreSQL Source Code  git master
dropdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dropdb
4  *
5  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * src/bin/scripts/dropdb.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres_fe.h"
14 #include "common.h"
15 #include "common/logging.h"
16 #include "fe_utils/string_utils.h"
17 
18 
19 static void help(const char *progname);
20 
21 
22 int
23 main(int argc, char *argv[])
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  {"maintenance-db", required_argument, NULL, 2},
37  {"force", no_argument, NULL, 'f'},
38  {NULL, 0, NULL, 0}
39  };
40 
41  const char *progname;
42  int optindex;
43  int c;
44 
45  char *dbname = NULL;
46  char *maintenance_db = NULL;
47  char *host = NULL;
48  char *port = NULL;
49  char *username = NULL;
50  enum trivalue prompt_password = TRI_DEFAULT;
51  bool echo = false;
52  bool interactive = false;
53  bool force = false;
54 
55  PQExpBufferData sql;
56 
57  PGconn *conn;
58  PGresult *result;
59 
60  pg_logging_init(argv[0]);
61  progname = get_progname(argv[0]);
62  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
63 
64  handle_help_version_opts(argc, argv, "dropdb", help);
65 
66  while ((c = getopt_long(argc, argv, "h:p:U:wWeif", long_options, &optindex)) != -1)
67  {
68  switch (c)
69  {
70  case 'h':
71  host = pg_strdup(optarg);
72  break;
73  case 'p':
74  port = pg_strdup(optarg);
75  break;
76  case 'U':
77  username = pg_strdup(optarg);
78  break;
79  case 'w':
80  prompt_password = TRI_NO;
81  break;
82  case 'W':
83  prompt_password = TRI_YES;
84  break;
85  case 'e':
86  echo = true;
87  break;
88  case 'i':
89  interactive = true;
90  break;
91  case 'f':
92  force = true;
93  break;
94  case 0:
95  /* this covers the long options */
96  break;
97  case 2:
98  maintenance_db = pg_strdup(optarg);
99  break;
100  default:
101  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
102  exit(1);
103  }
104  }
105 
106  switch (argc - optind)
107  {
108  case 0:
109  pg_log_error("missing required argument database name");
110  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
111  exit(1);
112  case 1:
113  dbname = argv[optind];
114  break;
115  default:
116  pg_log_error("too many command-line arguments (first is \"%s\")",
117  argv[optind + 1]);
118  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
119  exit(1);
120  }
121 
122  if (interactive)
123  {
124  printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
125  if (!yesno_prompt("Are you sure?"))
126  exit(0);
127  }
128 
129  initPQExpBuffer(&sql);
130 
131  appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
132  (if_exists ? "IF EXISTS " : ""),
133  fmtId(dbname),
134  force ? " WITH (FORCE)" : "");
135 
136  /* Avoid trying to drop postgres db while we are connected to it. */
137  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
138  maintenance_db = "template1";
139 
140  conn = connectMaintenanceDatabase(maintenance_db,
141  host, port, username, prompt_password,
142  progname, echo);
143 
144  if (echo)
145  printf("%s\n", sql.data);
146  result = PQexec(conn, sql.data);
147  if (PQresultStatus(result) != PGRES_COMMAND_OK)
148  {
149  pg_log_error("database removal failed: %s", PQerrorMessage(conn));
150  PQfinish(conn);
151  exit(1);
152  }
153 
154  PQclear(result);
155  PQfinish(conn);
156  exit(0);
157 }
158 
159 
160 static void
161 help(const char *progname)
162 {
163  printf(_("%s removes a PostgreSQL database.\n\n"), progname);
164  printf(_("Usage:\n"));
165  printf(_(" %s [OPTION]... DBNAME\n"), progname);
166  printf(_("\nOptions:\n"));
167  printf(_(" -e, --echo show the commands being sent to the server\n"));
168  printf(_(" -i, --interactive prompt before deleting anything\n"));
169  printf(_(" -f, --force try to terminate other connections before dropping\n"));
170  printf(_(" -V, --version output version information, then exit\n"));
171  printf(_(" --if-exists don't report error if database doesn't exist\n"));
172  printf(_(" -?, --help show this help, then exit\n"));
173  printf(_("\nConnection options:\n"));
174  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
175  printf(_(" -p, --port=PORT database server port\n"));
176  printf(_(" -U, --username=USERNAME user name to connect as\n"));
177  printf(_(" -w, --no-password never prompt for password\n"));
178  printf(_(" -W, --password force password prompt\n"));
179  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
180  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
181 }
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo)
Definition: common.c:153
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6623
bool yesno_prompt(const char *question)
Definition: common.c:435
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:4125
#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
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:33
static void help(const char *progname)
Definition: dropdb.c:161
trivalue
Definition: vacuumlo.c:33
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1166
static char * username
Definition: initdb.c:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int if_exists
Definition: pg_dumpall.c:71
char * dbname
Definition: streamutil.c:50
int main(int argc, char *argv[])
Definition: dropdb.c:23
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