Skip to content

Instantly share code, notes, and snippets.

@luckylu
Created January 14, 2021 06:04
Show Gist options
  • Save luckylu/981db2d4f723ad355c23c6a6bf038531 to your computer and use it in GitHub Desktop.
Save luckylu/981db2d4f723ad355c23c6a6bf038531 to your computer and use it in GitHub Desktop.
create readonly user on postgresql 9
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 SELECT 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