PostgreSQL - 无法更新 pg_database

aes*_*nak 3 postgresql postgresql-9.6

我无法使用用户 postgres 更新表 pg_database。正如您在下面看到的,我能够运行 SQL 查询,但是没有任何变化。

postgres=# select datname, datdba, datistemplate, datallowconn from pg_database;
    datname     | datdba | datistemplate | datallowconn
----------------+--------+---------------+--------------
 postgres       |     10 | f             | t
 template1      |     10 | t             | t
 template0      |     10 | t             | f
 my_template_1  |     10 | f             | t
(4 rows)

postgres=# UPDATE pg_database set datistemplate=true, datallowconn=false where datname='my_template_1' ;
UPDATE 1
postgres=# select datname, datdba, datistemplate, datallowconn from pg_database;
    datname     | datdba | datistemplate | datallowconn
----------------+--------+---------------+--------------
 postgres       |     10 | f             | t
 template1      |     10 | t             | t
 template0      |     10 | t             | f
 my_template_1  |     10 | f             | t
(4 rows)

postgres=#
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 5

您不能更新pg_database以更改数据库(您根本不应该更新系统表,尽管您可以为其中的一些表更新)。

使用ALTER DATABASE来代替:

alter database my_template_1
  with is_template true
       allow_connections false;
Run Code Online (Sandbox Code Playgroud)