postgresql - template0 访问权限和 pg_database

标签 postgresql

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 会失败

FATAL: database "template0" is not currently accepting connections.

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

it is used to protect the template0 database from being altered.

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

所以最终的问题是 - 为什么我们不能连接到 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/

相关文章:

postgresql如何让COPY自动解释格式化的数字字段?

json - 将 json 转换为嵌套的 postgres 复合类型

c - 如何在 Postgres 的 C 语言函数中返回位(n)或位变化(n)字段?

java - 为什么需要数据库代理键的 generatedKeys?

postgresql - 如何在 kubernetes 上使用密码连接到 psql

java - Hibernate序列生成器始终为0

postgresql - 没有更新/删除的死元组

postgresql - postgres中是否有低优先级查询之类的东西?

postgresql - plpgsql:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换

postgresql - 在 PostgreSQL 中使用 COPY FROM 加载带有 TIME ZONE 的 NULL TIMESTAMP