PostgreSQL 有 2 个模板数据库: template0 不可修改,template1 可修改,用于创建每个新数据库。

所以我在玩特权,授予和撤销,它引起了我的注意,至少在我的 Docker 镜像(以及本地安装的 PostgreSQL)中,两个模板数据库具有相同的访问权限。

如果我读对了,它会同时授予 postgres 默认用户和 public 角色的连接权限。

-- output is simplified
postgres=# \l
                 List of databases
   Name    |  Owner   |   Access privileges
 template0 | postgres | =c/postgres +
                        postgres=CTc/postgres
 template1 | postgres | =c/postgres +
                      | postgres=CTc/postgres

但我确信尝试连接到 template0 会失败



我做了一些挖掘,目录 pg_databasedatallowconn 属性,在描述中它说



可能是 的答案,但我不确定。我的意思是它只是一个目录,我认为它存储有关对象的数据,我不确定它是否会以任何方式影响对象的行为。

所以最终的问题是 - 为什么我们不能连接到 template0

最佳答案

falsedatallowconn 字段中的值 pg_database 是阻止您连接到 template0 数据库的唯一原因。您可以通过简单地以 Postgres super 用户身份更新值来移除此锁:

UPDATE pg_database SET datallowconn = TRUE WHERE datname = 'template0';

现在,您可以通过以下方式连接到 template0:
psql -Upostgres -dtemplate0

但你最好不要那样做,当然。 postgres 开发人员在阻止访问时会想到一些事情。如果您损坏了模板数据库,则很难创建新数据库。

关于postgresql - template0 访问权限和 pg_database,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57838396/

10-11 08:56