Skip to content

Instantly share code, notes, and snippets.

@francisco-rojas
Created May 19, 2015 17:56
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 francisco-rojas/4f7bef663fbc1376e501 to your computer and use it in GitHub Desktop.
Save francisco-rojas/4f7bef663fbc1376e501 to your computer and use it in GitHub Desktop.
PostgreSQL recipies

To create a Read only user

Access postgres console:

sudo -u postgres psql

Create user and grant read access to the database

CREATE USER user_name WITH ENCRYPTED PASSWORD 'your_password';
CREATE DATABASE user_name;
GRANT CONNECT ON DATABASE "database_to_access" TO user_name;
\c database_to_access
GRANT USAGE ON SCHEMA public TO user_name;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO user_name;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO user_name;

Login with the new user:

psql -U user_name -h localhost -d database_to_access -W

Run your query:

SELECT * FROM users;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment