1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2024, 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 "common/logging.h"
16 #include "fe_utils/option_utils.h"
17 #include "fe_utils/string_utils.h"
20 static void help(const char *progname
);
24 main(int argc
, char *argv
[])
26 static int if_exists
= 0;
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'},
47 char *maintenance_db
= NULL
;
50 char *username
= NULL
;
51 enum trivalue prompt_password
= TRI_DEFAULT
;
54 bool interactive
= false;
62 pg_logging_init(argv
[0]);
63 progname
= get_progname(argv
[0]);
64 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pgscripts"));
66 handle_help_version_opts(argc
, argv
, "dropdb", help
);
68 while ((c
= getopt_long(argc
, argv
, "efh:ip:U:wW", long_options
, &optindex
)) != -1)
79 host
= pg_strdup(optarg
);
85 port
= pg_strdup(optarg
);
88 username
= pg_strdup(optarg
);
91 prompt_password
= TRI_NO
;
94 prompt_password
= TRI_YES
;
97 /* this covers the long options */
100 maintenance_db
= pg_strdup(optarg
);
103 /* getopt_long already emitted a complaint */
104 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
109 switch (argc
- optind
)
112 pg_log_error("missing required argument database name");
113 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
116 dbname
= argv
[optind
];
119 pg_log_error("too many command-line arguments (first is \"%s\")",
121 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
127 printf(_("Database \"%s\" will be permanently removed.\n"), dbname
);
128 if (!yesno_prompt("Are you sure?"))
132 initPQExpBuffer(&sql
);
134 appendPQExpBuffer(&sql
, "DROP DATABASE %s%s%s;",
135 (if_exists
? "IF EXISTS " : ""),
137 force
? " WITH (FORCE)" : "");
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";
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
;
150 conn
= connectMaintenanceDatabase(&cparams
, progname
, echo
);
153 printf("%s\n", sql
.data
);
154 result
= PQexec(conn
, sql
.data
);
155 if (PQresultStatus(result
) != PGRES_COMMAND_OK
)
157 pg_log_error("database removal failed: %s", PQerrorMessage(conn
));
169 help(const char *progname
)
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
);