Тут показані розбіжності між вибраною ревізією та поточною версією сторінки.
| Порівняння попередніх версій Попередня ревізія Наступна ревізія | Попередня ревізія | ||
|
programming:databases:postgresql:roles_example [2020/07/29 15:26] charon |
programming:databases:postgresql:roles_example [2020/07/29 15:45] (поточний) charon [Grant usage for future tables, sequences, routines in schema for basic roles] |
||
|---|---|---|---|
| Рядок 16: | Рядок 16: | ||
| </ | </ | ||
| + | ---- | ||
| ===== Create basic roles: DO IT ONCE ===== | ===== Create basic roles: DO IT ONCE ===== | ||
| Рядок 28: | Рядок 29: | ||
| GRANT CONNECT ON DATABASE postgres TO experiment_admin, | GRANT CONNECT ON DATABASE postgres TO experiment_admin, | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| ===== Create new scheme ' | ===== Create new scheme ' | ||
| Рядок 36: | Рядок 39: | ||
| </ | </ | ||
| - | Grant access to the schema | + | ==== Grant access to the schema |
| <code postgresql> | <code postgresql> | ||
| GRANT USAGE ON SCHEMA myschema TO experiment_read_write, | GRANT USAGE ON SCHEMA myschema TO experiment_read_write, | ||
| Рядок 42: | Рядок 45: | ||
| </ | </ | ||
| - | Grant usage of existing tables, sequences, routines in schema for basic roles | + | All next commands in this section require that current user can administer roles // |
| + | <code postgresql> | ||
| + | GRANT experiment_admin, | ||
| + | </ | ||
| + | and | ||
| + | <code postgresql> | ||
| + | REVOKE experiment_admin, | ||
| + | </ | ||
| + | |||
| + | ==== Grant usage of existing tables, sequences, routines in schema for basic roles ==== | ||
| <code postgresql> | <code postgresql> | ||
| GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA myschema TO experiment_admin, | GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA myschema TO experiment_admin, | ||
| Рядок 51: | Рядок 63: | ||
| </ | </ | ||
| - | Grant usage for future tables, sequences, routines in schema for basic roles | + | ==== Grant usage for future tables, sequences, routines in schema for basic roles ==== |
| <code postgresql> | <code postgresql> | ||
| - | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_write, | + | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_only, |
| - | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_write, | + | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_only, |
| - | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_write, | + | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_only, |
| - | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_write, | + | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_only, |
| - | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_write, | + | ALTER DEFAULT PRIVILEGES FOR ROLE experiment_read_only, |
| </ | </ | ||
| - | How to create new users | + | <WRAP center round info 60%> |
| + | These rules above will work only if user switches role (//SET ROLE experiment_admin// | ||
| + | </ | ||
| - | New read-only user | + | ---- |
| - | This user can only read existing objects. But in schema public it can also create and manage his own objects, like tables. | + | |
| + | ===== How to create new users ===== | ||
| - | Basic setup | + | ==== New read-only user ==== |
| + | This user can only read existing objects. But in schema public it can also create and manage his own objects, like tables. | ||
| + | |||
| + | === Basic setup === | ||
| <code postgresql> | <code postgresql> | ||
| CREATE ROLE reader1 WITH LOGIN PASSWORD ' | CREATE ROLE reader1 WITH LOGIN PASSWORD ' | ||
| Рядок 73: | Рядок 90: | ||
| </ | </ | ||
| - | Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA | + | === Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA |
| <code postgresql> | <code postgresql> | ||
| ALTER DEFAULT PRIVILEGES FOR ROLE reader1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ALTER DEFAULT PRIVILEGES FOR ROLE reader1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ||
| Рядок 82: | Рядок 99: | ||
| </ | </ | ||
| - | Cleanup | + | === Cleanup |
| <code postgresql> | <code postgresql> | ||
| REVOKE reader1 FROM CURRENT_USER; | REVOKE reader1 FROM CURRENT_USER; | ||
| </ | </ | ||
| - | New read-write user | + | ==== New read-write user ==== |
| This user cannot create new objects in schema, like tables. It can read, write, delete and update existing objects (for example, modify rows in tables). | This user cannot create new objects in schema, like tables. It can read, write, delete and update existing objects (for example, modify rows in tables). | ||
| - | Exception: in schema public it can also create and manage his own objects, like tables. | + | <WRAP center round important 60%> |
| + | Exception: in schema | ||
| + | </ | ||
| - | Basic setup | + | |
| + | === Basic setup === | ||
| <code postgresql> | <code postgresql> | ||
| CREATE ROLE writer1 WITH LOGIN PASSWORD ' | CREATE ROLE writer1 WITH LOGIN PASSWORD ' | ||
| Рядок 99: | Рядок 119: | ||
| </ | </ | ||
| - | Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA | + | === Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA |
| <code postgresql> | <code postgresql> | ||
| ALTER DEFAULT PRIVILEGES FOR ROLE writer1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ALTER DEFAULT PRIVILEGES FOR ROLE writer1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ||
| Рядок 108: | Рядок 128: | ||
| </ | </ | ||
| - | Cleanup | + | === Cleanup |
| <code postgresql> | <code postgresql> | ||
| REVOKE writer1 FROM CURRENT_USER; | REVOKE writer1 FROM CURRENT_USER; | ||
| </ | </ | ||
| - | |||
| - | New admin user | + | ==== New admin user ==== |
| This user can create new objects in schema | This user can create new objects in schema | ||
| - | Basic setup | + | === Basic setup === |
| <code postgresql> | <code postgresql> | ||
| CREATE ROLE admin1 WITH LOGIN PASSWORD ' | CREATE ROLE admin1 WITH LOGIN PASSWORD ' | ||
| Рядок 126: | Рядок 145: | ||
| </ | </ | ||
| - | Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA | + | === Grant usage for future tables, sequences, routines in schema: REPEAT FOR EVERY SCHEMA |
| <code postgresql> | <code postgresql> | ||
| ALTER DEFAULT PRIVILEGES FOR ROLE admin1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ALTER DEFAULT PRIVILEGES FOR ROLE admin1 IN SCHEMA myschema GRANT ALL PRIVILEGES ON TABLES TO experiment_read_write, | ||
| Рядок 135: | Рядок 154: | ||
| </ | </ | ||
| - | Cleanup | + | === Cleanup |
| <code postgresql> | <code postgresql> | ||
| REVOKE admin1 FROM CURRENT_USER; | REVOKE admin1 FROM CURRENT_USER; | ||
| </ | </ | ||