Not able to create read-only user in database with SELECT privileges only - postgresql

I want to create a Postgres user that has just read-only access to the database.
I saw a similar question, but it had no resolution for my error.
I even tried searching for the error but didn't find anything useful.
Syntax:
GRANT CONNECT ON DATABASE YourDatabaseName TO Read_Only_User;
GRANT USAGE ON SCHEMA public TO Read_Only_User;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO Read_Only_User;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO Read_Only_User;
# You can either grant USAGE to everyone
GRANT USAGE ON SCHEMA public TO public;
# Or grant it just to your read only user
GRANT USAGE ON SCHEMA public TO readonlyuser;
But none worked. I logged in as a root user.
I many times ended with the error
ERROR: permission denied for relation querystoresearchclient
Could anyone help here?

Related

Can't grant all tables permission denied

I can't grant a new role on all tables. A table denies the query. How can I grant the user to be able run this command?
CREATE ROLE userrole123 WITH LOGIN PASSWORD 'userrole123' VALID UNTIL '2024-01-07 09:37:39.0' INHERIT;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO userrole123;
Output
SQL Error [42501]: ERROR: permission denied for table test
I run this command GRANT USAGE ON SCHEMA public to myusername but it did not solve it.
Thanks
The documentation is pretty outspoken there:
Ordinarily, only the object's owner (or a superuser) can grant or revoke privileges on an object. However, it is possible to grant a privilege “with grant option”, which gives the recipient the right to grant it in turn to others.
So obviously the user who is running the GRANT is neither a superuser, nor does it own test, nor has it been granted the SELECT privilege WITH GRANT OPTION.

Q: How to grant analyze privileges to pghero user

I've successfully setup pgHero using the permissions guide here.
Everything is working, including historical query stats, except for the ability run analyze on queries that it shows are slow.
I get PG::InsufficientPrivilege: ERROR: permission denied for table <tableName>
How can I grant permission to analyze to the pghero user?
Turns out this is as simple as granting SELECT (and whatever other) privileges to the pghero user like so:
# Grant select access for all current tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO pghero;
# For all future tables
ALTER DEFAULT PRIVILEGES FOR ROLE <main-user> IN SCHEMA public GRANT SELECT ON TABLES TO pghero;

Create PostgreSQL user with read-only permission on AWS

I'm trying to create a user with read-only permission in Postgres on AWS.
CREATE USER readonly_username WITH PASSWORD 'password';
GRANT USAGE ON SCHEMA public to readonly_username;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_username;
After that i gave SELECT permission new tables:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_username;
But when i try to give permission to existing tables i got an error:
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_username;
ERROR: permission denied for relation admin_log
Can anyone help me?

Creating admin user in PostgreSQL

I am trying to create an admin role/user in PostgreSQL which should fulfil the following requirements:
Should be able to do backup for the particular database (and not
others)
Should be able to create usernames which can access the
particular database (and not others).
Should be able to create/delete tables in the specific database and not other database
Should not be able to create other data bases.
This is what I have so far:
create role dba with nosuperuser createdb createrole nologin replication bypassrls;
grant usage on schema public to dba;
alter default privileges in schema public grant all on tables to dba;
alter default privileges in schema public grant all on sequences to dba;
grant connect on database myDatabase to dba;
grant usage on schema public to dba;
grant select on all tables in schema public to dba;
grant select on all sequences in schema public to dba;
grant all privileges on all tables in schema public to dba;
create user dba_user login inherit encrypted password 'password' in role dba;
Please advise how to modify the above code to fulfill the requirements.
To achieve that, perform the following modifications:
Transfer ownership of the database and all schemas and objects in it to the new user.
Give the user CREATEROLE.
Make sure to REVOKE CONNECT ON all databases FROM PUBLIC. Grant the new user the CONNECT privilege on the database in question.
Don't give the new user any permissions on other databases or objects therein.

Grant access to views in 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?
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.
postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser;
postgres=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO testuser;
GRANT USAGE on schema:
GRANT USAGE ON SCHEMA schema_name TO username;
Grant SELECT for a specific table:
GRANT SELECT ON tbl_loans_new TO oloffm;
Grant SELECT for multiple tables:
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO username;