Skip to content

Instantly share code, notes, and snippets.

@bradymholt
Created December 22, 2019 01:06
Show Gist options
  • Save bradymholt/fc8ec86dbc00098ee4622e1dbe69776c to your computer and use it in GitHub Desktop.
Save bradymholt/fc8ec86dbc00098ee4622e1dbe69776c to your computer and use it in GitHub Desktop.
Create PostgreSQL user and give permissions to objects in database
SELECT set_config('current.user', 'my_user', false);
SELECT set_config('current.dbname', 'my_database', false);
CREATE USER current_setting('current.user');
-- Give CREATE, CONNECT, TEMPORARY permissions
GRANT ALL PRIVILEGES ON DATABASE current_setting('current.dbname') TO current_setting('current.user');
-- Grant INSERT, UPDATE, DELETE access to all EXISTING tables in public schema
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO current_setting('current.user');
-- Grant INSERT, UPDATE, DELETE access to all NEW tables in public schema
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT INSERT, UPDATE, DELETE ON TABLES TO current_setting('current.user');
--GRANT SELECT, USAGE access to all EXISTING sequences in public schema
GRANT SELECT, USAGE ON ALL SEQUENCES IN SCHEMA public TO current_setting('current.user');
--GRANT SELECT, USAGE access to all NEW sequences in public schema
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, USAGE ON SEQUENCES TO current_setting('current.user');
-- Note EXECUTE on functions is granted by default on the public schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment