【问题标题】:How to delete postgresql database on linux如何在 Linux 上删除 postgresql 数据库
【发布时间】:2017-08-18 12:32:44
【问题描述】:

我正在尝试使用命令行界面在 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)

【问题讨论】:

标签: linux database postgresql psql


【解决方案1】:

是的,

DROP DATABASE template1;

别忘了备份你的数据库:

备份:pg_dump name_of_database > name_of_backup_file.bak

恢复:psql empty_database < backup_file.bak

【讨论】:

    【解决方案2】:

    确保您的 SQL 命令以分号 (;) 结束 尝试发出命令

    DROP DATABASE template1;
    

    以分号结尾。

    【讨论】:

    • 和大写 DROP DATABASE 也是(我的错误)
    【解决方案3】:

    如果您的数据库包含“template1”和“template0”数据库名称。您可以在下面使用我的脚本:

    1. UPDATE pg_database SET datistemplate='false' WHERE datname='template1';
    2. 删除数据库模板 1;

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 2012-07-08
      • 2013-02-12
      • 2011-03-20
      • 2011-12-19
      • 1970-01-01
      • 2010-10-02
      • 2012-09-17
      • 2011-07-21
      相关资源
      最近更新 更多