PostgreSQL自带的命令行工具03- dropdb

PostgreSQL自带的命令行工具03- dropdb

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

dropdb 是 PostgreSQL 中的一个命令行工具,用于删除指定的数据库。它基本上是在后台执行 SQL 命令 DROP DATABASE 来移除数据库的。使用 dropdb 可以让数据库管理员在命令行环境中轻松快速地删除数据库,但需要注意的是,这个操作是不可逆的,一旦数据库被删除,所有存储在该数据库中的数据都将丢失。

通过help 查看帮忙文档。

[pg16@test ~]$ dropdb --help
dropdb removes a PostgreSQL database.

Usage:
  dropdb [OPTION]... DBNAME

Options:
  -e, --echo                show the commands being sent to the server
  -f, --force               try to terminate other connections before dropping
  -i, --interactive         prompt before deleting anything
  -V, --version             output version information, then exit
  --if-exists               don't report error if database doesn't exist
  -?, --help                show this help, then exit

Connection options:
  -h, --host=HOSTNAME       database server host or socket directory
  -p, --port=PORT           database server port
  -U, --username=USERNAME   user name to connect as
  -w, --no-password         never prompt for password
  -W, --password            force password prompt
  --maintenance-db=DBNAME   alternate maintenance database

Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

示例1

删除一个名为 “white1” 的数据库

[pg16@test ~]$ dropdb white1
[pg16@test ~]$ psql -p 5777 
psql (16.2)
Type "help" for help.

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white2    | test2    | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(5 rows)

postgres=# 

这会删除名为 white1 的数据库,前提是您有足够的权限来删除这个数据库。

如果您需要以不同的用户身份来删除数据库,可以使用 -U 选项指定用户名:

dropdb -U username mydatabase

在这里,username 是 PostgreSQL 中具有删除数据库权限的用户。

示例2

删除角色test2下的white2库

[pg16@test ~]$ dropdb -U test2 white2

[pg16@test ~]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(4 rows)

主要选项

  • -U <username>:以指定的用户身份运行 dropdb 命令。
  • -h <hostname>:指定 PostgreSQL 服务器的主机名,适用于非本地数据库服务器的情况。
  • -p <port>:指定 PostgreSQL 服务器的端口,适用于数据库不在默认端口(5432)的情况。
  • -i:在删除数据库之前,提示确认操作。这是一个安全特性,可以防止意外删除数据库。

注意事项

  • 确保在执行 dropdb 之前了解它的影响,并确认没有数据需要保留或已经备份。
  • 某些情况下,如果数据库正在被访问或有连接活动,dropdb 可能会失败。在这种情况下,可能需要先断开所有连接到该数据库的客户端。
  • 删除数据库是一个不可逆操作,执行后数据库中的所有数据和结构都将永久丢失。

dropdb 命令是数据库维护和管理工作中的一个重要工具,特别是在开发和测试环境中,经常需要清理和重建数据库。然而,考虑到其操作的不可逆性,使用时需要格外小心。

谨记:心存敬畏,行有所止。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值