DROP DATABASE

用于删除数据库的 SQL 命令

DROP DATABASE 是一个 DDL 命令,用于删除(永久删除)一个数据库。

DROP DATABASE 命令在所有 PostgreSQL 版本中都存在。

变更历史

示例

DROP DATABASE 的基本执行示例

postgres=# DROP DATABASE foo;
DROP DATABASE

尝试删除一个不存在的数据库

postgres=# DROP DATABASE bar;
ERROR:  database "bar" does not exist

安全地尝试删除一个可能不存在的数据库

postgres=# DROP DATABASE IF EXISTS bar;
NOTICE:  database "bar" does not exist, skipping

尝试删除当前数据库

postgres=# DROP DATABASE postgres;
ERROR:  cannot drop the currently open database

尝试删除另一个当前正在使用的数据库

postgres=# DROP DATABASE foo;
ERROR:  database "foo" is being accessed by other users
DETAIL:  There are 2 other sessions using the database.

(请注意,PostgreSQL 可能需要几秒钟的延迟才能确定无法删除该数据库。)

强制删除另一个当前正在使用的数据库(PostgreSQL 13 及更高版本)

postgres=# DROP DATABASE foo WITH (FORCE);
DROP DATABASE

无法删除任何模板数据库

postgres=# DROP DATABASE template0;
ERROR:  cannot drop a template database

postgres=# DROP DATABASE template1 WITH (FORCE);
ERROR:  cannot drop a template database

分类

DDL, SQL 命令

另请参阅

dropdb, CREATE DATABASE, ALTER DATABASE

反馈

提交关于“DROP DATABASE”的任何评论、建议或更正,请在此处 提交