如何在Linux上删除PostgreSQL数据库

4

我正在尝试使用命令行界面在Linux上学习PostgreSQL。

我以前曾经按照一些教程添加了一些数据库,但现在已经忘记了所有学过的内容。

现在我想删除这些数据库。

我假定应该使用psql来完成这项任务,因为它是与PostgreSQL对话的命令行界面。

您可以在以下命令行输出中看到我尝试过的操作,但都没有成功。

psql (9.5.6)
Type "help" for help.

postgres=# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
 template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
(4 rows)

postgres=# dropdb template1
postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
 template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
(4 rows)

postgres-# DROP DATABASE template1
postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
 template0 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | postgres | UTF8     | en_CA.UTF-8 | en_CA.UTF-8 | 
(4 rows)

1
不要删除模板数据库,请阅读相关文档。 - klin
3个回答

13

是的,

DROP DATABASE template1;

不要忘记备份您的数据库:

备份: pg_dump 数据库名称 > 备份文件名.bak

恢复: psql 空数据库名称 < 备份文件名.bak


5

请确保在SQL命令的末尾加上分号 (;)。 尝试发出以下命令

DROP DATABASE template1;

在末尾加上分号。


还有大写的 DROP DATABASE(我的错误) - perlyking

0
如果您的数据库包含“template1”和“template0”数据库名称,您可以使用下面的脚本:
  1. UPDATE pg_database SET datistemplate='false' WHERE datname='template1';
  2. DROP DATABASE template1;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接