The basic concept in Postgres
Roles are global objects that can access all the databases in the db cluster, taking into account the required privileges.
A cluster stores multiple databases that contain multiple schemas. Schemas (even with the same name) in different databases are not connected. Granting privileges for a schema applies only to this particular schema in the current database (the current database at the time of granting).
Each database starts with a default public schema. This is an agreement, and many settings begin with it. In addition, a public schema is simply a schema like any other.
Based on MySQL, you can start with a single public scheme, virtually ignoring the level of the scheme. I regularly use dozens of schemas for each database.
Schemas are few (but not completely) like directories in the file system.
Once you use multiple schemas, be sure to understand search_path :
- How it affects the identifier of the search identifier and the "current scheme"
default privileges
In the GRANT documentation:
PostgreSQL provides default privileges for some types of public objects. By default, tables do not have the privileges of public columns, schemas, or table spaces. For other types, the default privileges granted by public are: CONNECT and CREATE TEMP TABLE for databases; EXECUTE privilege for functions; and USAGE privileges for languages.
All of these default values ββcan be changed with ALTER DEFAULT PRIVILEGES :
- Provide everything in a specific schema in db for a group role in PostgreSQL
Group role
As @Craig commented , it is best to use GRANT privileges for a group role, and then create a specific user member for that role ( GRANT group role to user role). this way, itβs easier to figure out and revoke privilege packages needed for specific tasks.
A group role is another role without logging in. Add a login to convert it to a user role. More details:
- Why did PostgreSQL bring users and groups together in roles?
Recipe
Say we have a new mydb database, mydb group and user myusr ...
When connecting to the database in question as a superuser (for example, postgres ):
REVOKE ALL ON DATABASE mydb FROM public;
Assign a user all privileges to all tables as you wrote (I could be more restrictive):
GRANT ALL ON ALL TABLES IN SCHEMA public TO mygrp; GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO mygrp;
To set default privileges for future objects, run for each role that creates objects in this scheme:
ALTER DEFAULT PRIVILEGES FOR ROLE myusr IN SCHEMA public GRANT ALL ON TABLES TO mygrp; ALTER DEFAULT PRIVILEGES FOR ROLE myusr IN SCHEMA public GRANT ALL ON SEQUENCES TO mygrp;
Now highlight the group to the user:
GRANT mygrp TO myusr;
Related answer:
- PostgreSQL - the database user should be allowed only call functions
Alternative (non-standard) setting
Coming from MySQL, and since you want to keep privileges for shared databases, you might like this custom db_user_namespace setting. In the documentation:
This option allows usernames for each database to be used. By default it is disabled.
Read the manual carefully. I do not use this setting. This does not cancel the above.