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 167 of file dropdb.c.

References _, and printf.

Referenced by main().

168 {
169  printf(_("%s removes a PostgreSQL database.\n\n"), progname);
170  printf(_("Usage:\n"));
171  printf(_(" %s [OPTION]... DBNAME\n"), progname);
172  printf(_("\nOptions:\n"));
173  printf(_(" -e, --echo show the commands being sent to the server\n"));
174  printf(_(" -f, --force try to terminate other connections before dropping\n"));
175  printf(_(" -i, --interactive prompt before deleting anything\n"));
176  printf(_(" -V, --version output version information, then exit\n"));
177  printf(_(" --if-exists don't report error if database doesn't exist\n"));
178  printf(_(" -?, --help show this help, then exit\n"));
179  printf(_("\nConnection options:\n"));
180  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
181  printf(_(" -p, --port=PORT database server port\n"));
182  printf(_(" -U, --username=USERNAME user name to connect as\n"));
183  printf(_(" -w, --no-password never prompt for password\n"));
184  printf(_(" -W, --password force password prompt\n"));
185  printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
186  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
187  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
188 }
#define printf(...)
Definition: port.h:221
const char * progname
Definition: pg_standby.c:36
#define _(x)
Definition: elog.c:88

◆ main()

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

Definition at line 23 of file dropdb.c.

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