Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / scripts / dropdb.c
blobb154ed1bb6dcfa0e484e1e6b5d4e76637bf0b0cf
1 /*-------------------------------------------------------------------------
3 * dropdb
5 * Portions Copyright (c) 1996-2021, 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"
14 #include "common.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);
23 int
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'},
39 {NULL, 0, NULL, 0}
42 const char *progname;
43 int optindex;
44 int c;
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;
57 PQExpBufferData sql;
59 PGconn *conn;
60 PGresult *result;
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, "h:p:U:wWeif", long_options, &optindex)) != -1)
70 switch (c)
72 case 'h':
73 host = pg_strdup(optarg);
74 break;
75 case 'p':
76 port = pg_strdup(optarg);
77 break;
78 case 'U':
79 username = pg_strdup(optarg);
80 break;
81 case 'w':
82 prompt_password = TRI_NO;
83 break;
84 case 'W':
85 prompt_password = TRI_YES;
86 break;
87 case 'e':
88 echo = true;
89 break;
90 case 'i':
91 interactive = true;
92 break;
93 case 'f':
94 force = true;
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 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
104 exit(1);
108 switch (argc - optind)
110 case 0:
111 pg_log_error("missing required argument database name");
112 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
113 exit(1);
114 case 1:
115 dbname = argv[optind];
116 break;
117 default:
118 pg_log_error("too many command-line arguments (first is \"%s\")",
119 argv[optind + 1]);
120 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
121 exit(1);
124 if (interactive)
126 printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
127 if (!yesno_prompt("Are you sure?"))
128 exit(0);
131 initPQExpBuffer(&sql);
133 appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
134 (if_exists ? "IF EXISTS " : ""),
135 fmtId(dbname),
136 force ? " WITH (FORCE)" : "");
138 /* Avoid trying to drop postgres db while we are connected to it. */
139 if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
140 maintenance_db = "template1";
142 cparams.dbname = maintenance_db;
143 cparams.pghost = host;
144 cparams.pgport = port;
145 cparams.pguser = username;
146 cparams.prompt_password = prompt_password;
147 cparams.override_dbname = NULL;
149 conn = connectMaintenanceDatabase(&cparams, progname, echo);
151 if (echo)
152 printf("%s\n", sql.data);
153 result = PQexec(conn, sql.data);
154 if (PQresultStatus(result) != PGRES_COMMAND_OK)
156 pg_log_error("database removal failed: %s", PQerrorMessage(conn));
157 PQfinish(conn);
158 exit(1);
161 PQclear(result);
162 PQfinish(conn);
163 exit(0);
167 static void
168 help(const char *progname)
170 printf(_("%s removes a PostgreSQL database.\n\n"), progname);
171 printf(_("Usage:\n"));
172 printf(_(" %s [OPTION]... DBNAME\n"), progname);
173 printf(_("\nOptions:\n"));
174 printf(_(" -e, --echo show the commands being sent to the server\n"));
175 printf(_(" -f, --force try to terminate other connections before dropping\n"));
176 printf(_(" -i, --interactive prompt before deleting anything\n"));
177 printf(_(" -V, --version output version information, then exit\n"));
178 printf(_(" --if-exists don't report error if database doesn't exist\n"));
179 printf(_(" -?, --help show this help, then exit\n"));
180 printf(_("\nConnection options:\n"));
181 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
182 printf(_(" -p, --port=PORT database server port\n"));
183 printf(_(" -U, --username=USERNAME user name to connect as\n"));
184 printf(_(" -w, --no-password never prompt for password\n"));
185 printf(_(" -W, --password force password prompt\n"));
186 printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
187 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
188 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);