1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/bin/scripts/dropdb.c
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
15 #include "fe_utils/string_utils.h"
18 static void help(const char *progname);
22 main(int argc, char *argv[])
24 static int if_exists = 0;
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},
44 char *maintenance_db = NULL;
47 char *username = NULL;
48 enum trivalue prompt_password = TRI_DEFAULT;
50 bool interactive = false;
57 progname = get_progname(argv[0]);
58 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
60 handle_help_version_opts(argc, argv, "dropdb", help);
62 while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
67 host = pg_strdup(optarg);
70 port = pg_strdup(optarg);
73 username = pg_strdup(optarg);
76 prompt_password = TRI_NO;
79 prompt_password = TRI_YES;
88 /* this covers the long options */
91 maintenance_db = pg_strdup(optarg);
94 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
99 switch (argc - optind)
102 fprintf(stderr, _("%s: missing required argument database name\n"), progname);
103 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
106 dbname = argv[optind];
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);
117 printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
118 if (!yesno_prompt("Are you sure?"))
122 initPQExpBuffer(&sql);
124 appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
125 (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
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";
131 conn = connectMaintenanceDatabase(maintenance_db,
132 host, port, username, prompt_password,
136 printf("%s\n", sql.data);
137 result = PQexec(conn, sql.data);
138 if (PQresultStatus(result) != PGRES_COMMAND_OK)
140 fprintf(stderr, _("%s: database removal failed: %s"),
141 progname, PQerrorMessage(conn));
153 help(const char *progname)
155 printf(_("%s removes a PostgreSQL database.\n\n"), progname);
156 printf(_("Usage:\n"));
157 printf(_(" %s [OPTION]... DBNAME\n"), progname);
158 printf(_("\nOptions:\n"));
159 printf(_(" -e, --echo show the commands being sent to the server\n"));
160 printf(_(" -i, --interactive prompt before deleting anything\n"));
161 printf(_(" -V, --version output version information, then exit\n"));
162 printf(_(" --if-exists don't report error if database doesn't exist\n"));
163 printf(_(" -?, --help show this help, then exit\n"));
164 printf(_("\nConnection options:\n"));
165 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
166 printf(_(" -p, --port=PORT database server port\n"));
167 printf(_(" -U, --username=USERNAME user name to connect as\n"));
168 printf(_(" -w, --no-password never prompt for password\n"));
169 printf(_(" -W, --password force password prompt\n"));
170 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
171 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));