Header And Logo

PostgreSQL
| The world's most advanced open source database.

dropdb.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * dropdb
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * src/bin/scripts/dropdb.c
00009  *
00010  *-------------------------------------------------------------------------
00011  */
00012 
00013 #include "postgres_fe.h"
00014 #include "common.h"
00015 #include "dumputils.h"
00016 
00017 
00018 static void help(const char *progname);
00019 
00020 
00021 int
00022 main(int argc, char *argv[])
00023 {
00024     static int  if_exists = 0;
00025 
00026     static struct option long_options[] = {
00027         {"host", required_argument, NULL, 'h'},
00028         {"port", required_argument, NULL, 'p'},
00029         {"username", required_argument, NULL, 'U'},
00030         {"no-password", no_argument, NULL, 'w'},
00031         {"password", no_argument, NULL, 'W'},
00032         {"echo", no_argument, NULL, 'e'},
00033         {"interactive", no_argument, NULL, 'i'},
00034         {"if-exists", no_argument, &if_exists, 1},
00035         {"maintenance-db", required_argument, NULL, 2},
00036         {NULL, 0, NULL, 0}
00037     };
00038 
00039     const char *progname;
00040     int         optindex;
00041     int         c;
00042 
00043     char       *dbname = NULL;
00044     char       *maintenance_db = NULL;
00045     char       *host = NULL;
00046     char       *port = NULL;
00047     char       *username = NULL;
00048     enum trivalue prompt_password = TRI_DEFAULT;
00049     bool        echo = false;
00050     bool        interactive = false;
00051 
00052     PQExpBufferData sql;
00053 
00054     PGconn     *conn;
00055     PGresult   *result;
00056 
00057     progname = get_progname(argv[0]);
00058     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
00059 
00060     handle_help_version_opts(argc, argv, "dropdb", help);
00061 
00062     while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
00063     {
00064         switch (c)
00065         {
00066             case 'h':
00067                 host = pg_strdup(optarg);
00068                 break;
00069             case 'p':
00070                 port = pg_strdup(optarg);
00071                 break;
00072             case 'U':
00073                 username = pg_strdup(optarg);
00074                 break;
00075             case 'w':
00076                 prompt_password = TRI_NO;
00077                 break;
00078             case 'W':
00079                 prompt_password = TRI_YES;
00080                 break;
00081             case 'e':
00082                 echo = true;
00083                 break;
00084             case 'i':
00085                 interactive = true;
00086                 break;
00087             case 0:
00088                 /* this covers the long options */
00089                 break;
00090             case 2:
00091                 maintenance_db = pg_strdup(optarg);
00092                 break;
00093             default:
00094                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00095                 exit(1);
00096         }
00097     }
00098 
00099     switch (argc - optind)
00100     {
00101         case 0:
00102             fprintf(stderr, _("%s: missing required argument database name\n"), progname);
00103             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00104             exit(1);
00105         case 1:
00106             dbname = argv[optind];
00107             break;
00108         default:
00109             fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
00110                     progname, argv[optind + 1]);
00111             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00112             exit(1);
00113     }
00114 
00115     if (interactive)
00116     {
00117         printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
00118         if (!yesno_prompt("Are you sure?"))
00119             exit(0);
00120     }
00121 
00122     initPQExpBuffer(&sql);
00123 
00124     appendPQExpBuffer(&sql, "DROP DATABASE %s%s;\n",
00125                       (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
00126 
00127     /* Avoid trying to drop postgres db while we are connected to it. */
00128     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
00129         maintenance_db = "template1";
00130 
00131     conn = connectMaintenanceDatabase(maintenance_db,
00132                             host, port, username, prompt_password, progname);
00133 
00134     if (echo)
00135         printf("%s", sql.data);
00136     result = PQexec(conn, sql.data);
00137     if (PQresultStatus(result) != PGRES_COMMAND_OK)
00138     {
00139         fprintf(stderr, _("%s: database removal failed: %s"),
00140                 progname, PQerrorMessage(conn));
00141         PQfinish(conn);
00142         exit(1);
00143     }
00144 
00145     PQclear(result);
00146     PQfinish(conn);
00147     exit(0);
00148 }
00149 
00150 
00151 static void
00152 help(const char *progname)
00153 {
00154     printf(_("%s removes a PostgreSQL database.\n\n"), progname);
00155     printf(_("Usage:\n"));
00156     printf(_("  %s [OPTION]... DBNAME\n"), progname);
00157     printf(_("\nOptions:\n"));
00158     printf(_("  -e, --echo                show the commands being sent to the server\n"));
00159     printf(_("  -i, --interactive         prompt before deleting anything\n"));
00160     printf(_("  -V, --version             output version information, then exit\n"));
00161     printf(_("  --if-exists               don't report error if database doesn't exist\n"));
00162     printf(_("  -?, --help                show this help, then exit\n"));
00163     printf(_("\nConnection options:\n"));
00164     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
00165     printf(_("  -p, --port=PORT           database server port\n"));
00166     printf(_("  -U, --username=USERNAME   user name to connect as\n"));
00167     printf(_("  -w, --no-password         never prompt for password\n"));
00168     printf(_("  -W, --password            force password prompt\n"));
00169     printf(_("  --maintenance-db=DBNAME   alternate maintenance database\n"));
00170     printf(_("\nReport bugs to <[email protected]>.\n"));
00171 }