Skip to content

Instantly share code, notes, and snippets.

@rpromyshlennikov
Last active April 3, 2023 13:56
Show Gist options
  • Save rpromyshlennikov/6228d0dacea2fcbb5988057fa86388d6 to your computer and use it in GitHub Desktop.
Save rpromyshlennikov/6228d0dacea2fcbb5988057fa86388d6 to your computer and use it in GitHub Desktop.
Grant all on DB to new user
GRANT CONNECT ON DATABASE "db-name" TO "user-name";
\c "db-name"; -- REQUIRED!!
GRANT USAGE ON SCHEMA "schema-name" TO "user-name";
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA "schema-name" TO "user-name";
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA "schema-name" TO "user-name";
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA "schema-name" TO "user-name";
GRANT ALL PRIVILEGES ON DATABASE "db-name" TO "user-name";
REVOKE USAGE ON SCHEMA "schema-name" FROM "user-name";
REVOKE SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA "schema-name" FROM "user-name";
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA "schema-name" FROM "user-name";
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA "schema-name" FROM "user-name";
REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA "schema-name" FROM "user-name";
REVOKE ALL PRIVILEGES ON DATABASE "postgres" FROM "user-name";
REVOKE CONNECT ON DATABASE "db-name" FROM "user-name";
DROP USER "user-name";
@rpromyshlennikov
Copy link
Author

There is a simpler way for creating users/DBs if you don't need to set granular permission.

CREATE USER my_username WITH PASSWORD 'my_password';
ALTER ROLE my_username WITH SUPERUSER;
CREATE DATABASE database_name;
GRANT ALL PRIVILEGES ON DATABASE "database_name" to my_username;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment