How to create a read only user in PostgreSQL
Create a new user in PostgreSQL
CREATE USER username WITH PASSWORD 'your_password';
Example:
testdb=# create user user1 with password 'password';
CREATE ROLE
Grant the connect access to the user:
GRANT CONNECT ON DATABASE <databasename> to <username>;
Example:
testdb=# grant connect on database testdb to user1;
GRANT
Grant usage on Schema:
GRANT USAGE ON SCHEMA <schema_name> to <username>
Example:
testdb=# grant usage on schema public to user1;
GRANT
Grant select on the tables:
--Grant select on specified table:
GRANT SELECT ON table_name TO username;
--Grant select on all table:
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO username;
Example:
GRANT SELECT ON ALL TABLES IN SCHEMA public TO user1;