Skip to content

Instantly share code, notes, and snippets.

@YourFriendCaspian
Forked from blasterpal/add_read_only_pg_user.rb
Last active September 2, 2017 03:27
Show Gist options
  • Select an option

  • Save YourFriendCaspian/89e6e0985f417143839827c9ca1ec3bc to your computer and use it in GitHub Desktop.

Select an option

Save YourFriendCaspian/89e6e0985f417143839827c9ca1ec3bc to your computer and use it in GitHub Desktop.
Add read only user to PG 9 on DB, cookbook snippet.
# Create a user with Login
sql = [%(CREATE ROLE <username> WITH PASSWORD '<password>' LOGIN)]
# # Allow to connect
sql << %(GRANT CONNECT ON DATABASE reports TO <username>)
# # Allow to use schema
sql << %(GRANT USAGE ON SCHEMA public TO <username>)
# # grant on current objects
sql << %(GRANT SELECT ON ALL TABLES IN SCHEMA public TO <username>)
# # grant on future objects
sql << %(ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO <username>)
%x(psql reports postgres -c "#{sql.join(';')}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment