Forked from jackie-do/[postgresql]_set_up_read_only_user.md
Created
December 15, 2017 10:15
-
-
Save kieetnvt/dc3c85466be4ff7cac74679bbfb04522 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Create a linux user and set password | |
sudo adduser read_access | |
.. then enter your password for this user | |
2) Limit permission for the user (option) | |
.. normally, this user only has root permission on it's directory. (/home/read_access) | |
3) Set read-only role for this user to use Postgresql | |
psql -U postgres -d <your database> | |
CREATE USER read_access WITH PASSWORD '<your password>'; | |
GRANT CONNECT ON DATABASE <your database> TO read_access; | |
GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_access; | |
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO read_access; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO read_access; | |
ALTER USER read_access SET default_transaction_read_only = on; | |
<br/> | |
4) Dump your database | |
PGPASSWORD="<your password>" pg_dump -Fc --no-acl --no-owner -h localhost -U read_access -d <your database> -t <your table> > mydb.dump | |
5) If you try to "change you will get this error" | |
ERROR: cannot execute INSERT in a read-only transaction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment