Postgresql 简明教程
PostgreSQL - DROP Database
在本章中,我们将讨论如何删除PostgreSQL中的数据库。有两种选择可以删除数据库:-
In this chapter, we will discuss how to delete the database in PostgreSQL. There are two options to delete a database −
-
Using DROP DATABASE, an SQL command.
-
Using dropdb a command-line executable.
Using DROP DATABASE
此命令删除一个数据库。它删除数据库的目录条目并删除包含数据的目录。只有数据库所有者才能执行此操作。当您或其他人连接到目标数据库时,无法执行此命令(连接到postgres或任何其他数据库来发出此命令)。
This command drops a database. It removes the catalog entries for the database and deletes the directory containing the data. It can only be executed by the database owner. This command cannot be executed while you or anyone else is connected to the target database (connect to postgres or any other database to issue this command).
Syntax
DROP DATABASE 语法如下 −
The syntax for DROP DATABASE is given below −
DROP DATABASE [ IF EXISTS ] name
Parameters
该表列出了参数及其描述。
The table lists the parameters with their descriptions.
S. No. |
Parameter & Description |
1 |
IF EXISTS Do not throw an error if the database does not exist. A notice is issued in this case. |
2 |
name The name of the database to remove. |
Example
以下是一个简单的示例,它将从 PostgreSQL 架构中删除 testdb −
The following is a simple example, which will delete testdb from your PostgreSQL schema −
postgres=# DROP DATABASE testdb;
postgres-#
Using dropdb Command
PostgresSQL 命令行可执行文件 dropdb 是对 SQL 命令 DROP DATABASE 的命令行包装器。通过此实用工具和通过其他方法访问服务器来删除数据库之间没有实际差异。dropdb 摧毁了一个现有的 PostgreSQL 数据库。执行此命令的用户必须是数据库超级用户或数据库的所有者。
PostgresSQL command line executable dropdb is a command-line wrapper around the SQL command DROP DATABASE. There is no effective difference between dropping databases via this utility and via other methods for accessing the server. dropdb destroys an existing PostgreSQL database. The user, who executes this command must be a database super user or the owner of the database.
Parameters
下表列出了参数及其描述
The following table lists the parameters with their descriptions
S. No. |
Parameter & Description |
1 |
dbname The name of a database to be deleted. |
2 |
option command-line arguments, which dropdb accepts. |
Options
下表列出了命令行参数 dropdb 接受这些参数 −
The following table lists the command-line arguments dropdb accepts −
S. No. |
Option & Description |
1 |
-e Shows the commands being sent to the server. |
2 |
-i Issues a verification prompt before doing anything destructive. |
3 |
-V Print the dropdb version and exit. |
4 |
--if-exists Do not throw an error if the database does not exist. A notice is issued in this case. |
5 |
--help Show help about dropdb command-line arguments, and exit. |
6 |
-h host Specifies the host name of the machine on which the server is running. |
7 |
-p port Specifies the TCP port or the local UNIX domain socket file extension on which the server is listening for connections. |
8 |
-U username User name to connect as. |
9 |
-w Never issue a password prompt. |
10 |
-W Force dropdb to prompt for a password before connecting to a database. |
11 |
--maintenance-db=dbname Specifies the name of the database to connect to in order to drop the target database. |
Example
以下示例演示了从 OS 命令提示符删除一个数据库 -
The following example demonstrates deleting a database from OS command prompt −
dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
以上命令丢弃了数据库 testdb 。此处,我使用了 postgres (在 template1 的 pg_roles 下找到)用户名来丢弃数据库。
The above command drops the database testdb. Here, I have used the postgres (found under the pg_roles of template1) username to drop the database.