Skip to content

Instantly share code, notes, and snippets.

@songpon
Created December 8, 2017 05:01
Show Gist options
  • Save songpon/f515fe24183205f0d7591f6202c38e89 to your computer and use it in GitHub Desktop.
Save songpon/f515fe24183205f0d7591f6202c38e89 to your computer and use it in GitHub Desktop.
CREATE USER readonlyuser WITH ENCRYPTED PASSWORD 'secret';
GRANT USAGE ON SCHEMA public to readonlyuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonlyuser;
-- repeat code below for each database:
GRANT CONNECT ON DATABASE mydbname to readonlyuser;
\c mydbname
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonlyuser; --- this grants privileges on new tables generated in new database "foo"
GRANT USAGE ON SCHEMA public to readonlyuser;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonlyuser;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonlyuser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment