使用PostgreSQL为特定表授予权限(关系“*”不存在)

问题描述 投票:0回答:1

我试图在几个表上授予对用户的只读访问权限。

这是我到目前为止所做的:

postgres=# CREATE ROLE myuser LOGIN PASSWORD 'mypassword';
CREATE ROLE
postgres=# GRANT CONNECT ON DATABASE mydb TO myuser;
GRANT

我尝试过的测试(不起作用):

postgres=# GRANT SELECT ON TABLE mytable TO myuser;
ERROR:  relation "mytable" does not exist
postgres=# GRANT SELECT ON TABLE mydb.mytable TO myuser;
ERROR:  schema "mydb" does not exist
postgres=# GRANT SELECT ON TABLE public.mytable TO myuser;
ERROR:  relation "public.mytable" does not exist
postgres=# GRANT SELECT ON TABLE mydb.public.mytable TO myuser;
ERROR:  cross-database references are not implemented: "mydb.public.mytable"

我依赖的资源:

https://tableplus.io/blog/2018/04/postgresql-how-to-create-read-only-user.html https://www.postgresql.org/docs/current/sql-grant.html

我不明白缺少什么,因为我遵循这些文章,我的逻辑是指定哪个数据库是表,但没有提到。

我已经检查过StackOverflow上的类似问题,但步骤与我上面提到的资源相同。

(我不知道它是否相关,但我使用的是postgresql 9.6)

帮助将非常感谢!

============

编辑:当我运行\ d命令

postgres=# \d
No relations found.

postgres=# select current_database();
 current_database 
------------------
 postgres
(1 row)

我认为postgres用户拥有所有权限,但也许我应该与自己的用户联系?

postgresql
1个回答
0
投票

要指定特定表的权限,您必须连接到相关数据库。您可以使用\connect\c命令连接到数据库。

以下工作:

postgres=# \connect mydb
postgres=# GRANT SELECT ON TABLE mytable TO myuser;

有用的命令:

  • 验证您当前的数据库:select current_database();
  • 检查可用的关系:\d

感谢@a_horse_with_no_name指出错误。

© www.soinside.com 2019 - 2024. All rights reserved.