ERROR: permission denied for relation tablename on Postgres while trying a SELECT as a readonly user

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly; 

The readonly user can connect, see the tables but when it tries to do a simple select it gets:

ERROR: permission denied for relation mytable SQL state: 42501 

This is happening on PostgreSQL 9.1

What I did wrong?

回答1:

Here is the complete solution for PostgreSQL 9+, updated recently.

CREATE USER readonly  WITH ENCRYPTED PASSWORD 'readonly'; GRANT USAGE ON SCHEMA public to readonly; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;  -- repeat code below for each database:  GRANT CONNECT ON DATABASE foo to readonly; \c foo ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonly; --- this grants privileges on new tables generated in new database "foo" GRANT USAGE ON SCHEMA public to readonly;  GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly; GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly; 

Thanks to http://jamie.curle.io/creating-a-read-only-user-in-postgres/ for several important aspects

If anyone find shorter code, and preferably one that is able to perform this for all existing databases, extra kudos.



回答2:

Try to add

GRANT USAGE ON SCHEMA public to readonly; 

You probably were not aware that one needs to have the requisite permissions to a schema, in order to use objects in the schema.



回答3:

This worked for me:

Check the current role you are logged into by using: SELECT CURRENT_USER, SESSION_USER;

Note: It must match with Owner of the schema.

Schema | Name | Type | Owner
--------+--------+-------+----------

If the owner is different, then give all the grants to the current user role from the admin role by :

GRANT 'ROLE_OWNER' to 'CURRENT ROLENAME';

Then try to execute the query, it will give the output as it has access to all the relations now.



回答4:

make sure your user has attributes on its role. for example:

postgres=# \du                              List of roles  Role name |                   Attributes                   | Member of  -----------+------------------------------------------------+-----------  flux      |                                                | {}  postgres  | Superuser, Create role, Create DB, Replication | {} 

after performing the following command:

postgres=# ALTER ROLE flux WITH Superuser; ALTER ROLE postgres=# \du                              List of roles  Role name |                   Attributes                   | Member of  -----------+------------------------------------------------+-----------  flux      | Superuser                                      | {} postgres  | Superuser, Create role, Create DB, Replication | {} 

it fixed the problem.

see tutorial for roles and stuff here: https://www.digitalocean.com/community/tutorials/how-to-use-roles-and-manage-grant-permissions-in-postgresql-on-a-vps--2



回答5:

You should execute the next query:

GRANT ALL ON TABLE mytable TO myuser; 

Or if your error is in a view then maybe the table does not have permission, so you should execute the next query:

GRANT ALL ON TABLE tbm_grupo TO myuser; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!