运行以下查询:
SELECT exists (
SELECT
schema_name
FROM
information_schema.schemata
WHERE
schema_name = 'public'
) AS schema_exists;
我总是得到 FALSE
,即使公共(public)架构存在也是如此。
我应该如何检查此架构是否存在?
编辑
我正在使用 PostgreSQL 8.4 版
最佳答案
information_schema.schemata
中的信息取决于您所连接的角色,因此一般来说,通过查询来发现模式并不是真正正确的 View 。
doc on information_schema.schemata
in 9.3说:
The view schemata contains all schemas in the current database that are owned by a currently enabled role.
但是(至少对我来说)从那句话还不太清楚,为什么你看不到 public
。
在邮件列表帖子中,Tom Lane 的解释更进一步:
参见 http://www.postgresql.org/message-id/11650.1357782995@sss.pgh.pa.us
他的结论:
As things stand, a non-superuser won't see "public", "pg_catalog", nor even "information_schema" itself in this view, which seems a tad silly.
这看起来和这个问题中的问题完全一样。
底线:使用 pg_namespace
而不是 information_schema.schemata
这已在 9.4 版中进行了修改,以符合用户的期望。 current doc说:
模式的The view schemata contains all schemas in the current database that the current user has access to (by way of being the owner or having some privilege).
USAGE
权限现在足以从该 View 中获取它。
关于sql - 如何检查 PostgreSQL 公共(public)模式是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781952/