Skip to content

Instantly share code, notes, and snippets.

@dux
Created March 1, 2018 14:57
Show Gist options
  • Save dux/96071af8fab20f4048c9769cd5e406c6 to your computer and use it in GitHub Desktop.
Save dux/96071af8fab20f4048c9769cd5e406c6 to your computer and use it in GitHub Desktop.
Create read only user on Postgre DB
CREATE USER readonly WITH ENCRYPTED PASSWORD 'readonly';
GRANT USAGE ON SCHEMA public to readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
-- repeat code below for each database:
GRANT CONNECT ON DATABASE foo to readonly;
\c foo
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonly; --- this grants privileges on new tables generated in new database "foo"
GRANT USAGE ON SCHEMA public to readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment