Create a read-only user in PostgreSQL database

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;
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.