sql - 如何检查 PostgreSQL 公共(public)模式是否存在?

标签 sql postgresql schema

运行以下查询:

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/

相关文章:

mysql - 在 MySQL 中检测到 'x' 时添加到一个值

sql - 是否有可能在手动捕获异常时引发系统异常?

sql - 如何使用 T-SQL 脚本将数据库用户加入 db_owner 角色?

mysql - 如何在nodejs中创建mysql模式

数据库架构问题

java - 如果字段顺序更改,Avro 模式不兼容

php - 在 codeigniter 中向 Controller 注入(inject) SQL

sql - SELECT 语句 postgres 中的动态列

postgresql - 命令StdinPipe关闭太早

ruby-on-rails - 从原始 sql 查询返回哈希