<!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/drop_database.sgml,v 1.20 2005/06/21 04:02:31 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/drop_database.sgml,v 1.21 2005/11/22 15:24:17 adunstan Exp $
PostgreSQL documentation
-->
<refsynopsisdiv>
<synopsis>
-DROP DATABASE <replaceable class="PARAMETER">name</replaceable>
+DROP DATABASE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
</synopsis>
</refsynopsisdiv>
<title>Parameters</title>
<variablelist>
+ <varlistentry>
+ <term><literal>IF EXISTS</literal></term>
+ <listitem>
+ <para>
+ Do not throw an error if the database does not exist. A notice is issued
+ in this case.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="PARAMETER">name</replaceable></term>
<listitem>
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.173 2005/10/15 02:49:15 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.174 2005/11/22 15:24:17 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
* DROP DATABASE
*/
void
-dropdb(const char *dbname)
+dropdb(const char *dbname, bool missing_ok)
{
Oid db_id;
bool db_istemplate;
if (!get_db_info(dbname, &db_id, NULL, NULL,
&db_istemplate, NULL, NULL, NULL, NULL, NULL))
- ereport(ERROR,
+ {
+ if (! missing_ok)
+ {
+ ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", dbname)));
+ }
+ else
+ {
+
+ /* Close pg_database, release the lock, since we changed nothing */
+ heap_close(pgdbrel, ExclusiveLock);
+ ereport(NOTICE,
+ (errmsg("database \"%s\" does not exist, skipping",
+ dbname)));
+
+ return;
+ }
+ }
if (!pg_database_ownercheck(db_id, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.319 2005/11/21 12:49:31 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.320 2005/11/22 15:24:17 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
DropdbStmt *newnode = makeNode(DropdbStmt);
COPY_STRING_FIELD(dbname);
+ COPY_SCALAR_FIELD(missing_ok);
return newnode;
}
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.256 2005/11/21 12:49:31 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.257 2005/11/22 15:24:17 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
_equalDropdbStmt(DropdbStmt *a, DropdbStmt *b)
{
COMPARE_STRING_FIELD(dbname);
+ COMPARE_SCALAR_FIELD(missing_ok);
return true;
}
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.514 2005/11/21 12:49:31 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.515 2005/11/22 15:24:17 adunstan Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
/*****************************************************************************
*
- * DROP DATABASE
+ * DROP DATABASE [ IF EXISTS ]
*
* This is implicitly CASCADE, no need for drop behavior
*****************************************************************************/
{
DropdbStmt *n = makeNode(DropdbStmt);
n->dbname = $3;
+ n->missing_ok = FALSE;
+ $$ = (Node *)n;
+ }
+ | DROP DATABASE IF_P EXISTS database_name
+ {
+ DropdbStmt *n = makeNode(DropdbStmt);
+ n->dbname = $5;
+ n->missing_ok = TRUE;
$$ = (Node *)n;
}
;
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.247 2005/11/21 12:49:32 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.248 2005/11/22 15:24:18 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
{
DropdbStmt *stmt = (DropdbStmt *) parsetree;
- dropdb(stmt->dbname);
+ dropdb(stmt->dbname, stmt->missing_ok);
}
break;
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.42 2005/10/15 02:49:44 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.43 2005/11/22 15:24:18 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
} xl_dbase_drop_rec;
extern void createdb(const CreatedbStmt *stmt);
-extern void dropdb(const char *dbname);
+extern void dropdb(const char *dbname, bool missing_ok);
extern void RenameDatabase(const char *oldname, const char *newname);
extern void AlterDatabase(AlterDatabaseStmt *stmt);
extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt);
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.294 2005/11/21 12:49:32 alvherre Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.295 2005/11/22 15:24:18 adunstan Exp $
*
*-------------------------------------------------------------------------
*/
{
NodeTag type;
char *dbname; /* database to drop */
+ bool missing_ok; /* skip error if db is missing? */
} DropdbStmt;
/* ----------------------