Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql readonly role and user

I couldn't find an answer to this question: why does selecting from the table fail after the privileges were granted?

-- create new role
CREATE ROLE readonly;

-- grant access to all existing tables
GRANT CONNECT ON DATABASE shop TO readonly;
GRANT USAGE ON SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO readonly;

-- grant access to all table which will be created in the future
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO readonly;

-- create user and grant role to this user
CREATE USER b_readonly WITH PASSWORD 'reAdOnLy123';
GRANT readonly TO b_readonly;

My error message from db is following:

ERROR: permission denied for relation customer_search_query SQL state: 42501

Is there some new trick in Postgresql 9.6.5?

like image 395
Mike Winkelmann Avatar asked Feb 07 '18 12:02

Mike Winkelmann


People also ask

Is role same as user in Postgres?

Users, groups, and roles are the same thing in PostgreSQL, with the only difference being that users have permission to log in by default. The CREATE USER and CREATE GROUP statements are actually aliases for the CREATE ROLE statement.

How do you make a Postgres user read only in pgAdmin?

Type a descriptive name for the user. On the 'Definition' tab enter a secure password. On the 'Role membership' tab add the newly create group to the 'Member' window > click 'OK'. You have successfully created a new read-only user for your database.

How do you check if Postgres is read only?

The output of SELECT pg_is_in_recovery() can tell you if the cluster is in the read-only mode.

What is difference between user and schema in PostgreSQL?

In Oracle a schema and a user is a one to one relationship and there is no real distinction between a user and a schema. In PostgreSQL the situation is different: All the objects a user is creating are created in a specific schema (or namespace).


1 Answers

It is likely that the table you're querying from, customer_search_query is not in the public schema. Try running this command.

GRANT SELECT ON customer_search_query TO readonly;
like image 77
sindhu_sp Avatar answered Oct 20 '22 16:10

sindhu_sp