Created
February 27, 2025 13:51
-
-
Save shivanshs9/c8fcb3dd01be9658b9a51d6948d96b07 to your computer and use it in GitHub Desktop.
Postgres script to create read-write and read-only users
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
/* Create Read-write PSQL user and DB */ | |
CREATE DATABASE yourdbname; | |
CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass'; | |
GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser; | |
/* Create Read-only user for db $DB */ | |
CREATE ROLE rouser WITH LOGIN PASSWORD 'yourpass' | |
NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL 'infinity'; | |
\c $DB | |
GRANT CONNECT ON DATABASE $DB TO rouser; | |
GRANT USAGE ON SCHEMA public TO rouser; | |
GRANT SELECT ON ALL TABLES IN SCHEMA public TO rouser; | |
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO rouser; | |
REVOKE CREATE ON SCHEMA public FROM PUBLIC; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO rouser; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment