【问题标题】:How does one drop a template database from PostgreSQL?如何从 PostgreSQL 中删除模板数据库?
【发布时间】:2012-07-08 11:33:36
【问题描述】:
postgres=# DROP DATABASE template_postgis;
ERROR:  cannot drop a template database

http://www.postgresql.org/docs/9.1/static/manage-ag-templatedbs.html 看起来好像如果我设置了template_postgis.datistemplate = false,我就可以删除它,但我不知道如何设置它。

【问题讨论】:

    标签: postgresql postgresql-9.1 database-template


    【解决方案1】:
    postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';
    UPDATE 1
    postgres=# DROP DATABASE template_postgis;
    DROP DATABASE
    postgres=# 
    

    【讨论】:

    • 我查看了创建模板的脚本。有行psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
    • 这会导致更多问题。我在 ubuntu 上从 9.1 -> 9.2 进行了升级。这隐式地创建了 template1。然后我执行了你的命令,它似乎工作。但是,现在我安装了 phpPgAdmin,但我无法登录,它说:'FATAL: database "template1" does not exist'
    • 重新创建模板1:create database template1 template template0;UPDATE pg_database SET datistemplate='true' WHERE datname='template1'; 另见:pgsql.inb4.se/2009/april/rebuild-template1.html
    【解决方案2】:

    您可以使用alter database 命令。比破坏元数据更简单、更安全。

    postgres=# create database tempDB is_template true;
    CREATE DATABASE
    postgres=# drop database tempDB;
    ERROR:  cannot drop a template database
    postgres=# alter database tempDB is_template false;
    ALTER DATABASE
    postgres=# drop database tempDB;
    DROP DATABASE
    postgres=# 
    

    Documentation

    【讨论】:

    • 此解决方案有效且远没有所选解决方案复杂。
    • @amoe 好地方。编辑添加文档链接
    • 感谢上帝,你存在的人。所有其他解决方案均无效。谢谢!
    • 这可行,但请确保在 'false' 周围添加单引号 POSTGRES v10
    【解决方案3】:
    update pg_database set datistemplate=false where datname='template0';
    update pg_database set datistemplate=false where datname='template1';
    
    ....
    
    Creating script to analyze new cluster                      ok
    Creating script to delete old cluster                       ok
    Checking for hash indexes                                   ok
    
    Upgrade Complete
    ----------------
    

    【讨论】:

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