Skip to content

Instantly share code, notes, and snippets.

@hbasria
Created December 25, 2021 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbasria/699ca60bce29d4fb49ef1ac5a0401d32 to your computer and use it in GitHub Desktop.
Save hbasria/699ca60bce29d4fb49ef1ac5a0401d32 to your computer and use it in GitHub Desktop.
-- 1. Create a group
CREATE ROLE readaccess;
-- 2. Grant usage on schema:
GRANT USAGE ON SCHEMA schema_name TO readaccess;
-- 3.1 Grant select for a specific table:
GRANT SELECT ON table_name TO readaccess;
-- 3.2 Grant select for multiple tables:
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO readaccess;
-- 4. 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 readaccess;
-- 5. To create a new user in PostgreSQL
CREATE USER username WITH PASSWORD 'your_password';
-- 6 Grant the connect access:
GRANT CONNECT ON DATABASE database_name TO username;
-- 7 Grant the read access:
GRANT readaccess TO username;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment