PostgreSQL Source Code  git master
dropdb.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "fe_utils/string_utils.h"
Include dependency graph for dropdb.c:

Go to the source code of this file.

Functions

static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ help()

static void help ( const char *  progname)
static

Definition at line 154 of file dropdb.c.

References _, and printf.

Referenced by main().

155 {
156  printf(_("%s removes a PostgreSQL database.\n\n"), progname);
157  printf(_("Usage:\n"));
158  printf(_(" %s [OPTION]... DBNAME\n"), progname);
159  printf(_("\nOptions:\n"));
160  printf(_(" -e, --echo show the commands being sent to the server\n"));
161  printf(_(" -i, --interactive prompt before deleting anything\n"));
162  printf(_(" -V, --version output version information, then exit\n"));
163  printf(_(" --if-exists don't report error if database doesn't exist\n"));
164  printf(_(" -?, --help show this help, then exit\n"));
165  printf(_("\nConnection options:\n"));
166  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
167  printf(_(" -p, --port=PORT database server port\n"));
168  printf(_(" -U, --username=USERNAME user name to connect as\n"));
169  printf(_(" -w, --no-password never prompt for password\n"));
170  printf(_(" -W, --password force password prompt\n"));
171  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
172  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
173 }
#define printf(...)
Definition: port.h:198
const char * progname
Definition: pg_standby.c:36
#define _(x)
Definition: elog.c:87

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 23 of file dropdb.c.

References _, appendPQExpBuffer(), conn, connectMaintenanceDatabase(), PQExpBufferData::data, dbname, fmtId(), fprintf, get_progname(), getopt_long(), handle_help_version_opts(), help(), if_exists, initPQExpBuffer(), no_argument, optarg, optind, pg_log_error, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, PGRES_COMMAND_OK, port, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), printf, progname, required_argument, set_pglocale_pgservice(), TRI_DEFAULT, TRI_NO, TRI_YES, username, and yesno_prompt().

24 {
25  static int if_exists = 0;
26 
27  static struct option long_options[] = {
28  {"host", required_argument, NULL, 'h'},
29  {"port", required_argument, NULL, 'p'},
30  {"username", required_argument, NULL, 'U'},
31  {"no-password", no_argument, NULL, 'w'},
32  {"password", no_argument, NULL, 'W'},
33  {"echo", no_argument, NULL, 'e'},
34  {"interactive", no_argument, NULL, 'i'},
35  {"if-exists", no_argument, &if_exists, 1},
36  {"maintenance-db", required_argument, NULL, 2},
37  {NULL, 0, NULL, 0}
38  };
39 
40  const char *progname;
41  int optindex;
42  int c;
43 
44  char *dbname = NULL;
45  char *maintenance_db = NULL;
46  char *host = NULL;
47  char *port = NULL;
48  char *username = NULL;
49  enum trivalue prompt_password = TRI_DEFAULT;
50  bool echo = false;
51  bool interactive = false;
52 
53  PQExpBufferData sql;
54 
55  PGconn *conn;
56  PGresult *result;
57 
58  pg_logging_init(argv[0]);
59  progname = get_progname(argv[0]);
60  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
61 
62  handle_help_version_opts(argc, argv, "dropdb", help);
63 
64  while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
65  {
66  switch (c)
67  {
68  case 'h':
69  host = pg_strdup(optarg);
70  break;
71  case 'p':
72  port = pg_strdup(optarg);
73  break;
74  case 'U':
75  username = pg_strdup(optarg);
76  break;
77  case 'w':
78  prompt_password = TRI_NO;
79  break;
80  case 'W':
81  prompt_password = TRI_YES;
82  break;
83  case 'e':
84  echo = true;
85  break;
86  case 'i':
87  interactive = true;
88  break;
89  case 0:
90  /* this covers the long options */
91  break;
92  case 2:
93  maintenance_db = pg_strdup(optarg);
94  break;
95  default:
96  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
97  exit(1);
98  }
99  }
100 
101  switch (argc - optind)
102  {
103  case 0:
104  pg_log_error("missing required argument database name");
105  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
106  exit(1);
107  case 1:
108  dbname = argv[optind];
109  break;
110  default:
111  pg_log_error("too many command-line arguments (first is \"%s\")",
112  argv[optind + 1]);
113  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
114  exit(1);
115  }
116 
117  if (interactive)
118  {
119  printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
120  if (!yesno_prompt("Are you sure?"))
121  exit(0);
122  }
123 
124  initPQExpBuffer(&sql);
125 
126  appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
127  (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
128 
129  /* Avoid trying to drop postgres db while we are connected to it. */
130  if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
131  maintenance_db = "template1";
132 
133  conn = connectMaintenanceDatabase(maintenance_db,
134  host, port, username, prompt_password,
135  progname, echo);
136 
137  if (echo)
138  printf("%s\n", sql.data);
139  result = PQexec(conn, sql.data);
140  if (PQresultStatus(result) != PGRES_COMMAND_OK)
141  {
142  pg_log_error("database removal failed: %s", PQerrorMessage(conn));
143  PQfinish(conn);
144  exit(1);
145  }
146 
147  PQclear(result);
148  PQfinish(conn);
149  exit(0);
150 }
PGconn * connectMaintenanceDatabase(const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool echo)
Definition: common.c:160
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6617
bool yesno_prompt(const char *question)
Definition: common.c:443
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void pg_logging_init(const char *argv0)
Definition: logging.c:39
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4119
#define printf(...)
Definition: port.h:198
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:196
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
PGconn * conn
Definition: streamutil.c:54
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int port
Definition: pg_regress.c:91
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:40
static void help(const char *progname)
Definition: dropdb.c:154
trivalue
Definition: vacuumlo.c:33
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1160
static char * username
Definition: initdb.c:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int if_exists
Definition: pg_dumpall.c:71
char * dbname
Definition: streamutil.c:50
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
char * optarg
Definition: getopt.c:52
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
#define _(x)
Definition: elog.c:87
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92