Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grant access to views in postgresql

Tags:

postgresql

I have a view called testview in postgresql.

I created a new user called testuser.

I would like testuser to have all privileges on all tables and views in the database.

To do this I ran the following commands:

GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;  GRANT USAGE ON SCHEMA public TO testuser;  GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO testuser; 

testuser now has access to all tables in the database, but if I try to run SELECT * FROM testview I get the following error: permission denied for relation testview.

What is wrong? How do testuser get access to testview?

like image 965
Thomas Avatar asked Dec 26 '16 19:12

Thomas


People also ask

How do I grant access to PostgreSQL view?

To include tables/views you create in the future, you can say: ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO testuser; Or if you want to give more than SELECT , you can say ALL PRIVILEGES instead.

How do I grant access to a materialized view in PostgreSQL?

Give SELECT permissions the new test_mv materialized view to the my_user role. CREATE ROLE my_user WITH NOLOGIN; GRANT SELECT ON test_mv TO my_user; To test the SELECT permission use SET ROLE to change the security context to the my_user role and verify with another check of current_user .

How do I grant a view to a SQL user?

For the existing view, you can go to the Properties of the view in SSMS, add users in the Permissions, and then grant select permission in the permissions list. Or use the following statement to grant user permissions: GRANT SELECT ON OBJECT::[schema]. [yourview] TO User1,User2.


1 Answers

I agree it should work. With permissions GRANT ... ON ALL TABLES should include views too.

Did you create the view after granting the privileges to testuser? If so then it doesn't have the same privileges as the other tables. That's because GRANT ... ON ALL TABLES means "on all tables that currently exist". To include tables/views you create in the future, you can say:

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO testuser; 

Or if you want to give more than SELECT, you can say ALL PRIVILEGES instead.

I think this behavior of ON ALL TABLES is one of the most misunderstood bits about Postgres permissions, and it isn't really called out in the standard documentation, so I tried to emphasize it in my own Postgres permissions overview.

like image 77
Paul A Jungwirth Avatar answered Oct 04 '22 19:10

Paul A Jungwirth