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

时间:2019-04-09 10:25:05

标签: postgresql

我正在尝试向几张桌子上的用户授予只读访问权限。

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

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用户拥有所有特权,但也许我应该与自己的用户建立联系?

1 个答案:

答案 0 :(得分:0)

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

以下工作:

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

有用的命令:

  • 验证您当前的数据库:select current_database();

  • 检查可用关系:\d

感谢@a_horse_with_no_name指出错误。

相关问题