Skip to content

Instantly share code, notes, and snippets.

@reanim8ed
Last active August 24, 2021 14:57
Show Gist options
  • Save reanim8ed/3f229f0dfe30eb050a46e81f558d96b4 to your computer and use it in GitHub Desktop.
Save reanim8ed/3f229f0dfe30eb050a46e81f558d96b4 to your computer and use it in GitHub Desktop.
[Create a read-only user in PostgreSQL] #postgres

Connect to psql:

  su - postgres database_name
  psql
  1. To create a new user in PostgreSQL:

CREATE USER username WITH PASSWORD 'your_password';

  1. GRANT the CONNECT access:

GRANT CONNECT ON DATABASE database_name TO username;

  1. Then GRANT USAGE on schema:

GRANT USAGE ON SCHEMA schema_name TO username;

  1. GRANT SELECT

Grant SELECT for a specific table: GRANT SELECT ON table_name TO username;

Grant SELECT for multiple tables: GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO username;

If you want to grant access to the new table in the future automatically, you have to alter default:

ALTER DEFAULT PRIVILEGES IN SCHEMA schema_name
GRANT SELECT ON TABLES TO username;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment