Skip to content

Commit 7fe33a5

Browse files
committed
Add --if-exists option to dropdb and dropuser.
Josh Kupershmidt, with some further editing by me.
1 parent 94478aa commit 7fe33a5

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

doc/src/sgml/ref/dropdb.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ PostgreSQL documentation
8686
</listitem>
8787
</varlistentry>
8888

89+
<varlistentry>
90+
<term><option>--if-exists</></term>
91+
<listitem>
92+
<para>
93+
Do not throw an error if the database does not exist. A notice is issued
94+
in this case.
95+
</para>
96+
</listitem>
97+
</varlistentry>
98+
8999
<varlistentry>
90100
<term><option>-V</></term>
91101
<term><option>--version</></term>

doc/src/sgml/ref/dropuser.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ PostgreSQL documentation
8888
</listitem>
8989
</varlistentry>
9090

91+
<varlistentry>
92+
<term><option>--if-exists</></term>
93+
<listitem>
94+
<para>
95+
Do not throw an error if the database does not exist. A notice is
96+
issued in this case.
97+
</para>
98+
</listitem>
99+
</varlistentry>
100+
91101
<varlistentry>
92102
<term><option>-V</></term>
93103
<term><option>--version</></term>

src/bin/scripts/dropdb.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static void help(const char *progname);
2121
int
2222
main(int argc, char *argv[])
2323
{
24+
static int if_exists = 0;
25+
2426
static struct option long_options[] = {
2527
{"host", required_argument, NULL, 'h'},
2628
{"port", required_argument, NULL, 'p'},
@@ -29,6 +31,7 @@ main(int argc, char *argv[])
2931
{"password", no_argument, NULL, 'W'},
3032
{"echo", no_argument, NULL, 'e'},
3133
{"interactive", no_argument, NULL, 'i'},
34+
{"if-exists", no_argument, &if_exists, 1},
3235
{NULL, 0, NULL, 0}
3336
};
3437

@@ -79,6 +82,9 @@ main(int argc, char *argv[])
7982
case 'i':
8083
interactive = true;
8184
break;
85+
case 0:
86+
/* this covers the long options */
87+
break;
8288
default:
8389
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
8490
exit(1);
@@ -110,8 +116,8 @@ main(int argc, char *argv[])
110116

111117
initPQExpBuffer(&sql);
112118

113-
appendPQExpBuffer(&sql, "DROP DATABASE %s;\n",
114-
fmtId(dbname));
119+
appendPQExpBuffer(&sql, "DROP DATABASE %s%s;\n",
120+
(if_exists ? "IF EXISTS " : ""), fmtId(dbname));
115121

116122
/*
117123
* Connect to the 'postgres' database by default, except have the
@@ -146,6 +152,7 @@ help(const char *progname)
146152
printf(_("\nOptions:\n"));
147153
printf(_(" -e, --echo show the commands being sent to the server\n"));
148154
printf(_(" -i, --interactive prompt before deleting anything\n"));
155+
printf(_(" --if-exists don't report error if database doesn't exist\n"));
149156
printf(_(" --help show this help, then exit\n"));
150157
printf(_(" --version output version information, then exit\n"));
151158
printf(_("\nConnection options:\n"));

src/bin/scripts/dropuser.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static void help(const char *progname);
2121
int
2222
main(int argc, char *argv[])
2323
{
24+
static int if_exists = 0;
25+
2426
static struct option long_options[] = {
2527
{"host", required_argument, NULL, 'h'},
2628
{"port", required_argument, NULL, 'p'},
@@ -29,6 +31,7 @@ main(int argc, char *argv[])
2931
{"password", no_argument, NULL, 'W'},
3032
{"echo", no_argument, NULL, 'e'},
3133
{"interactive", no_argument, NULL, 'i'},
34+
{"if-exists", no_argument, &if_exists, 1},
3235
{NULL, 0, NULL, 0}
3336
};
3437

@@ -79,6 +82,9 @@ main(int argc, char *argv[])
7982
case 'i':
8083
interactive = true;
8184
break;
85+
case 0:
86+
/* this covers the long options */
87+
break;
8288
default:
8389
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
8490
exit(1);
@@ -110,7 +116,8 @@ main(int argc, char *argv[])
110116
}
111117

112118
initPQExpBuffer(&sql);
113-
appendPQExpBuffer(&sql, "DROP ROLE %s;\n", fmtId(dropuser));
119+
appendPQExpBuffer(&sql, "DROP ROLE %s%s;\n",
120+
(if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
114121

115122
conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
116123

@@ -141,6 +148,7 @@ help(const char *progname)
141148
printf(_("\nOptions:\n"));
142149
printf(_(" -e, --echo show the commands being sent to the server\n"));
143150
printf(_(" -i, --interactive prompt before deleting anything\n"));
151+
printf(_(" --if-exists don't report error if user doesn't exist\n"));
144152
printf(_(" --help show this help, then exit\n"));
145153
printf(_(" --version output version information, then exit\n"));
146154
printf(_("\nConnection options:\n"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy