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_database
有 datalowconn
属性,在描述中它说
it is used to protect the template0 database from being altered.
这可能是一个答案,但我不确定。我的意思是它只是一个目录,我认为它存储了关于对象的数据,我不确定它是否会以任何方式影响对象的行为。
所以最终的问题是 - 为什么我们不能连接到 template0
?
最佳答案
值false
在datallowconn
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/