【问题标题】:Postgres user can't select from public tablesPostgres 用户无法从公共表中选择
【发布时间】:2019-11-10 04:38:48
【问题描述】:

您好,我的 postgresql 数据库中有一个用户 \dt 为用户输出:

 test=> \dt
                  List of relations
 Schema |           Name           | Type  |  Owner
--------+--------------------------+-------+----------
 public | code                     | table | postgres
 public | code_to_indicator        | table | postgres
 public | indicator                | table | postgres

如您所见,表的所有者是 postgres 但是如果为 postgres 用户运行 \dt 命令,它会说:

postgres=# \dt
Did not find any relations.

这怎么可能?另外,如果我尝试由 postgres 用户选择,它也会失败:

postgres=# select * from indicator;
ERROR:  relation "indicator" does not exist
LINE 1: select * from indicator;

有人可以帮忙吗?直到今天一切都很好,出了点问题我什至不知道是什么。 PostgreSQL 版本:11.4

【问题讨论】:

    标签: postgresql schema public rights


    【解决方案1】:

    psql 提示符开头的postgres=# 表示您已连接到postgres 数据库,该数据库是作为管理员连接的占位符创建的,通常其中没有任何内容。如果你没有在命令行中指定数据库,psql 会寻找与用户同名的数据库,所以postgres 用户会默认连接到这个。

    要改为连接到test 数据库,您可以在psql 提示符中键入\c test,或使用psql -d test 启动它。

    【讨论】:

      【解决方案2】:

      在表名不合格时查看public 或任何架构中的表的能力取决于search_path 设置。

      如果您尚未永久修改search_path,请在会话中发出RESET search_path;,然后重试。

      search_path 如果不是由您直接修改,如何修改?播放 pg_dump 获得的 SQL 文件具有从搜索路径中删除 public 的效果,这几乎就是这个问题所显示的。

      如果它是通过ALTER USER postgres SET search_path TO ... 或每个数据库或全局永久修改的,则需要通过等效的 ALTER 命令撤消它。

      请参阅How does the search_path influence identifier resolution and the "current schema"PostgreSQL documentation 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多