在本章中,我们将讨论如何删除在PostgreSQL数据库。有两个选择方法删除数据库:
使用DROP DATABASE从 SQL命令.
使用dropdb 一个命令行可执行文件.
使用此操作之前要小心,因为会导致失去所有存储在数据库中的所有信息。
使用 DROP DATABASE
此命令可删除数据库。它消除了目录数据库并删除包含数据的目录条目。它只能由数据库所有者执行。此命令不能执行,而或其他人连接到目标数据库。 (连接到Postgres或任何其他数据库发出此命令。)
语法
DROP DATABASE 的语法如下:
DROP DATABASE[IF EXISTS]name
参数
下表列出了参数及其说明.
参数
描述
IF EXISTS
Do not throw an error if the database does not exist. A notice is issued in this case.
name
The name of the database to remove.
我们不能删除一个数据库有任何打开的连接,包括我们自己的的连接frompsql或pgAdmin III 。如果我们要删除当前连接到的数据库,必须切换到另一个数据库或template1。因此,它可能是更方便使用程序DROPDB来代替,这是一个包装这个命令
实例
下面是一个简单的例子,这将删除PostgreSQL模式testdb :
postgres=#DROP DATABASE testdb;postgres-#
使用DROPDB命令
PostgreSQL的命令行可执行DROPDB围绕SQL命令DROP DATABASE命令行包装。通过此实用工具删除数据库和通过其他方法访问服务器之间有没有实际的区别。 DROPDB破坏现有PostgreSQL数据库。执行此命令的用户必须是数据库超级用户或数据库所有者。
语法
createdb 的语法如下所示:
dropdb[option...]dbname
参数
下表列出了参数,对它们的描述。
参数
描述
dbname
The name of a database to be deleted.
option
command-line arguments which dropdb accepts.
选项
下表列出了命令行参数DROPDB接收:
选项
描述
-e
Shows the commands being sent to the server.
-i
Issues a verification prompt before doing anything destructive.
-V
Print the dropdb version and exit.
--if-exists
Do not throw an error if the database does not exist. A notice is issued in this case.
--help
Show help about dropdb command line arguments, and exit.
-h host
Specifies the host name of the machine on which the server is running.
-p port
Specifies the TCP port or the local Unix domain socket file extension on which the server is listening for connections.
-U username
User name to connect as.
-w
Never issue a password prompt.
-W
Force dropdb to prompt for a password before connecting to a database.
--maintenance-db=dbname
Specifies the name of the database to connect to in order to drop the target database.
实例
下面的例子演示从OS命令提示符,删除数据库:
dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
上面的命令删除数据库TESTDB。在这里使用postgres(template1 pg_roles下找到)用户名,删除数据库。
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。