Skip to content

Instantly share code, notes, and snippets.

@codersofthedark
Last active March 25, 2018 21:20
Show Gist options
  • Save codersofthedark/2093d100db7ccfe6c75797ec7459285c to your computer and use it in GitHub Desktop.
Save codersofthedark/2093d100db7ccfe6c75797ec7459285c to your computer and use it in GitHub Desktop.
Create PSQL User and give read access to it to all tables in all schemas
CREATE USER "someuser" WITH PASSWORD 'somepassword';
GRANT india_group TO "someuser";
GRANT "prateekthakore" TO "sharad.sachdeva";
GRANT CONNECT ON DATABASE somedatabase TO "someuser"
DO $do$
DECLARE
theSchema text;
BEGIN
FOR theSchema IN SELECT DISTINCT table_schema FROM information_schema.tables
LOOP
EXECUTE format($$ GRANT USAGE ON SCHEMA %I TO "someUser" $$, theSchema);
EXECUTE format($$ GRANT SELECT ON ALL SEQUENCES IN SCHEMA %I TO "someUser" $$, theSchema);
EXECUTE format($$ GRANT SELECT ON ALL TABLES IN SCHEMA %I TO "someUser" $$, theSchema);
END LOOP;
END;
$do$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment