PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dropdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dropdb
4  *
5  * Portions Copyright (c) 1996-2017, 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 "fe_utils/string_utils.h"
16 
17 
18 static void help(const char *progname);
19 
20 
21 int
22 main(int argc, char *argv[])
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  {"maintenance-db", required_argument, NULL, 2},
36  {NULL, 0, NULL, 0}
37  };
38 
39  const char *progname;
40  int optindex;
41  int c;
42 
43  char *dbname = NULL;
44  char *maintenance_db = NULL;
45  char *host = NULL;
46  char *port = NULL;
47  char *username = NULL;
48  enum trivalue prompt_password = TRI_DEFAULT;
49  bool echo = false;
50  bool interactive = false;
51 
52  PQExpBufferData sql;
53 
54  PGconn *conn;
55  PGresult *result;
56 
57  progname = get_progname(argv[0]);
58  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
59 
60  handle_help_version_opts(argc, argv, "dropdb", help);
61 
62  while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
63  {
64  switch (c)
65  {
66  case 'h':
67  host = pg_strdup(optarg);
68  break;
69  case 'p':
70  port = pg_strdup(optarg);
71  break;
72  case 'U':
73  username = pg_strdup(optarg);
74  break;
75  case 'w':
76  prompt_password = TRI_NO;
77  break;
78  case 'W':
79  prompt_password = TRI_YES;
80  break;
81  case 'e':
82  echo = true;
83  break;
84  case 'i':
85  interactive = true;
86  break;
87  case 0:
88  /* this covers the long options */
89  break;
90  case 2:
91  maintenance_db = pg_strdup(optarg);
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  fprintf(stderr, _("%s: missing required argument database name\n"), progname);
103  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
104  exit(1);
105  case 1:
106  dbname = argv[optind];
107  break;
108  default:
109  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
110  progname, argv[optind + 1]);
111  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
112  exit(1);
113  }
114 
115  if (interactive)
116  {
117  printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
118  if (!yesno_prompt("Are you sure?"))
119  exit(0);
120  }
121 
122  initPQExpBuffer(&sql);
123 
124  appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
125  (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
126 
127  /* Avoid trying to drop postgres db while we are connected to it. */
128  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
129  maintenance_db = "template1";
130 
131  conn = connectMaintenanceDatabase(maintenance_db,
132  host, port, username, prompt_password, progname);
133 
134  if (echo)
135  printf("%s\n", sql.data);
136  result = PQexec(conn, sql.data);
137  if (PQresultStatus(result) != PGRES_COMMAND_OK)
138  {
139  fprintf(stderr, _("%s: database removal failed: %s"),
140  progname, PQerrorMessage(conn));
141  PQfinish(conn);
142  exit(1);
143  }
144 
145  PQclear(result);
146  PQfinish(conn);
147  exit(0);
148 }
149 
150 
151 static void
152 help(const char *progname)
153 {
154  printf(_("%s removes a PostgreSQL database.\n\n"), progname);
155  printf(_("Usage:\n"));
156  printf(_(" %s [OPTION]... DBNAME\n"), progname);
157  printf(_("\nOptions:\n"));
158  printf(_(" -e, --echo show the commands being sent to the server\n"));
159  printf(_(" -i, --interactive prompt before deleting anything\n"));
160  printf(_(" -V, --version output version information, then exit\n"));
161  printf(_(" --if-exists don't report error if database doesn't exist\n"));
162  printf(_(" -?, --help show this help, then exit\n"));
163  printf(_("\nConnection options:\n"));
164  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
165  printf(_(" -p, --port=PORT database server port\n"));
166  printf(_(" -U, --username=USERNAME user name to connect as\n"));
167  printf(_(" -w, --no-password never prompt for password\n"));
168  printf(_(" -W, --password force password prompt\n"));
169  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
170  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
171 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
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:3516
const char * progname
Definition: pg_standby.c:37
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
PGconn * conn
Definition: streamutil.c:42
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int port
Definition: pg_regress.c:87
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:35
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname)
Definition: common.c:152
static void help(const char *progname)
Definition: dropdb.c:152
trivalue
Definition: vacuumlo.c:31
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
static char * username
Definition: initdb.c:129
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
static int if_exists
Definition: pg_dumpall.c:71
char * dbname
Definition: streamutil.c:38
int main(int argc, char *argv[])
Definition: dropdb.c:22
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:1846
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89