Posted onPublished May 20, 2020 by alexjv89
-- create user
CREATE USER dbreadonlyuser WITH PASSWORD 'somepass';
-- give user permission to connect to db
GRANT CONNECT ON DATABASE dbname TO dbreadonlyuser;
-- give user permission to access schema
GRANT USAGE ON SCHEMA "public" TO dbreadonlyuser;
-- grants access to all tables in this schema public for this user
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dbreadonlyuser;
-- grants access to all future tables that you might add to the db. This way you dont want to keep running the previous command again and again when you create new table
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO dbreadonlyuser;