]> granicus.if.org Git - postgresql/blob - src/bin/scripts/dropdb.c
Empty search_path in Autovacuum and non-psql/pgbench clients.
[postgresql] / src / bin / scripts / dropdb.c
1 /*-------------------------------------------------------------------------
2  *
3  * dropdb
4  *
5  * Portions Copyright (c) 1996-2018, 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,
133                                                                           progname, echo);
134
135         if (echo)
136                 printf("%s\n", sql.data);
137         result = PQexec(conn, sql.data);
138         if (PQresultStatus(result) != PGRES_COMMAND_OK)
139         {
140                 fprintf(stderr, _("%s: database removal failed: %s"),
141                                 progname, PQerrorMessage(conn));
142                 PQfinish(conn);
143                 exit(1);
144         }
145
146         PQclear(result);
147         PQfinish(conn);
148         exit(0);
149 }
150
151
152 static void
153 help(const char *progname)
154 {
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"));
172 }