Repository navigation
Description
Is there an existing issue already for this feature request/idea?
- I have searched for an existing issue, and could not find anything. I believe this is a new feature request to be evaluated.
What problem is this feature going to solve? Why should it be added?
I don't like when changes are made in the database with the personal role, where the applications role should have been used to have the owner set correctly, only because the user was not prompted to "become" another role, because they already had the required permissions inherited.
But I also want to have some roles where the permissions should be granted to every personal role directly.
inherit would have to be true for only these roles, but with CNPG I can only set it as a default for the user to be applied to all role memberships.
Describe the solution you'd like
I'd like to have a new field in the RoleConfiguration that holds information about the parent roles the configured role should be added to with additional fields for each membership option as described by the official PostgreSQL documentation.
# example taken from documentation https://cloudnative-pg.io/docs/1.28/declarative_role_management
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
spec:
managed:
roles:
- name: dante
ensure: present
comment: Dante Alighieri
login: true
superuser: false
inRoles:
- pg_monitor
- pg_signal_backend
# new field
roleGrants:
- role: my_application_role
inherit: false
admin: false
set: trueI am aware that these are the default options for admin and set - they could be omitted, I only put them here to clarify the schema I have in mind.
For inherit, the default is whatever is specified for the user, which would be true if not specified otherwise. So with my proposal, you'd be able to override this on a per-role level.
Describe alternatives you've considered
I considered to manage the grants externally and only use CNPG to set up the cluster, but I think this is a feature that could be useful to others as well.
For the schema I also considered changing the inRoles definition to allow for passing the options, but this would be a breaking change to the Cluster CRD. And the new field could not
Additional context
Looking at the implementation, the role memberships defined in roleGrants would not be managed when creating the user, but during UpdateMembership. This means if I want my user creation to fail if the role does not exist, I specify the membership in inRoles.
But it also means the order of .spec.managed.roles does not matter, because they would be created first and only after all of them are present the memberships would be reconciled.
When the same role is specified in both inRoles and roleGrants, the options specified in roleGrants take precedence.
Updating the options is done by simply GRANT-ing again with the updated options set. They will be overridden.
I have to adjust the GetParentRoles command to return the currently set options as well.
Please let me know your feedback to this.
I'd like to attempt implementing it and we could discuss further implementation details on the PR I'm going to open.
Backport?
N/A
Are you willing to actively contribute to this feature?
Yes
Code of Conduct
- I agree to follow this project's Code of Conduct
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn Progress
- StatusShow more project fieldsAnalysis
Activity
jonasbadstuebner commentedon Feb 18, 2026
Another idea proposed by a colleague of mine would be to use #5341 and remove the
inRolesfield for the new CRD, replacing it withroleGrants(or changing the behavior of theinRolesfield).I'll add a comment to that issue as well.
jsilvela commentedon Feb 18, 2026
Sort of... that's not a peculiarity of CloudNativePG. It's coming from Postgres. In the Postgres
CREATE ROLEwe don't have fine-grained control of theIN ROLEShaving different permissions. You have to issue GRANT commands for exact setups.I think here we have a dilemma that presents often for us. How far do we go with mapping Postgres to Kubernetes CRD?
We end up trying for a Pareto 80% - 20% rule, and I wonder if this is worth adding to the CRD.
For the purpose of discussion, assuming your proposal did go forward:
DatabaseRoleCRD) #5341roleGrantsbelong as a child ofrole? I could see an independentgrantobject working well (and mirroring the Postgres and general SQL logic of things) EDIT -- I like it better. We'd have managed roles and managed grants ... but if we move to an Role CRD, we should have a Grant CRD ... which brings me back to the Pareto 80% - 20% consideration.jsilvela commentedon Feb 18, 2026
Beside my comments above, I wonder:
jonasbadstuebner commentedon Feb 23, 2026
Hey @jsilvela,
thank you for looking into my request!
I think this could even be a benefit, if a DatabaseRole CRD is becoming a thing.
If a user is created with
IN ROLES, you have to manage subsequent changes to the roles memberships throughGRANTanyway.Also the order of how roles are applied does not matter if in one step, all roles are created and in a second step, all grants are applied.
And the granting of roles is done through the same process on both user create and grant update steps.
I think it is.
As database admin, I want to declaratively define who has access to what database in what way, and I want to do it as granularly as possible (in the realm that I operate in, which ends before actions related application logic) and in a way that I can alert on CNPG failing to reach the desired state.
I would, however, completely replace the
inRolesfield - if there is no reason to have creation of a role fail if the requested parent roles are not present - with aroleGrantsfield, as described above.This is up to you.
For me it would be enough to have role memberships be managed.
I'm not sure if a separate Grant CRD would imply that all grant statements from https://www.postgresql.org/docs/current/sql-grant.html would receive support sooner or later, which might increase complexity by a lot.
Also it would cross a line where operators of database clusters would have to know what objects are present inside the database so they could set up the correct grant for that.
I personally would try to avoid having to update my CNPG Grant CRs every time a developer adds a new table.
If you'd keep it down to role membership, you could have those in the Role CRD and don't have to know anything about the database contents.
This depends on what the use case looks like, I suppose.
I would draw the line (as described above) where database operation ends and application logic begins.
If a new table or index is added, a migration should be used so the owner is set to the application role correctly.
I want to give application owners the ability to log into the database and create and test out indices for example "in the name of the application", so they can be added to the migration later.
For that I want them to be able to explicitly "become that application", for which I have to set NOINHERIT, but SET TRUE.
github-actions commentedon Apr 25, 2026
This issue is stale because it has been open for 60 days with no activity.
jonasbadstuebner commentedon Apr 25, 2026
Please mark it as „never stale“, I‘m only waiting for #5341
jonasbadstuebner commentedon Apr 25, 2026
Not stale
gbartolini commentedon Jun 24, 2026
Although these options are available in PostgreSQL, I struggle to see the benefits of adding them to CloudNativePG, not just from a maintenance perspective but also from a usability perspective. Can you please provide some concrete examples of why this is needed? And why now, considering that we still cannot assign grants to databases or schemas? Thank you.
jonasbadstuebner commentedon Jun 25, 2026
Okay, let me take a step back to explain why I would love to have this feature:
Where we currently stand
We are evaluating migrating our bare-metal PostgreSQL to Kubernetes using CNPG. We provide "PostgreSQL-as-a-Service" internally for our developer teams and have our permissions laid out as follows:
SET ROLE <application-role>to act on behalf of the application. To ensure they don't accidentally inherit permissions without explicitly switching roles, these memberships must haveINHERIT FALSE.INHERIT TRUE.The problem I have with the current CNPG limitations
To achieve the current permission layout described above without my PR, we would need to either
Where I would like to be
I would like to manage the
INHERITmembership option with GitOps, as we do with everything else and have CNPG ensure the option is set as I want it to be for each role membership.Why I think we don't need anything else for this change to be beneficial to users
Databases and schemas can be created with their owner managed through CNPG, which is enough to mirror our setup.
As I wrote above, implementing a Grant CRD is a lot more work, which is currently not needed for me.
And I also think, it should not be CNPGs responsibility to manage grants on anything deeper than schemas. But that might be a topic for a discussion somewhere else, as a general direction of where CNPG wants to go, as @jsilvela was also thinking about this question above.
6 remaining items
feat(roles): introduce `roleGrants` field with role membership options
gbartolini commentedon Oct 23, 2026
I have finally had some time to think about this feature request, and I'd like to settle the shape of the API before the implementation details. Here is how I think the interface should look. I would love to hear your feedback, especially @jonasbadstuebner.
Proposed shape
roleGrantsis available only on theDatabaseRoleCRD. The managed roles in theClusterspec (.spec.managed.roles) keepinRolesas it is today and don't getroleGrants: The main reason is error reporting.DatabaseRoleis its own resource, so a failure in one role (an unsupported option, a missing parent role, a rejectedGRANT) shows up on that object, instatus.applied,status.messageand its conditions, and can be seen withkubectl get databaseroles. Roles under.spec.managed.rolesare reconciled as one list and reported in theClusterstatus, which is much harder to attribute to a single role. This is the same approach as for the TLS client certificates (#10896), which exist only onDatabaseRole, and as for the work on password management.DatabaseRoleis also where declarative role management is going, so I'd rather not grow theClusterAPI further. The rest of this comment is aboutDatabaseRole.Two lists, two jobs
inRolesstays as the simple, backwards-compatible list: "make this role a member of these roles", with PostgreSQL defaults.roleGrantsis the form with options. AroleGrantsentry with only anameis equivalent to aninRolesitem.GRANT/REVOKEpath, on creation and on update. Changes to either list are applied to existing roles.roleGrantsmust be unique. The webhook / CRD validation should reject both cases, instead of letting one entry silently win.Role-level vs membership-level options
spec.inheritis the role attribute (rolinherit). It exists on all supported PostgreSQL versions, and it is the default for every membership of the role.roleGrants[].inheritandroleGrants[].setare per-membership and exist only from PostgreSQL 16. The role-levelinheritis the fallback for memberships that don't set their own.roleGrants[].adminis per-membership (WITH ADMIN OPTION) and exists on all supported versions. It lets the member grant and revoke the role to others, so it should be documented as privileged.adminin the API: we want it to be complete. I would not add a role-leveladminshorthand. It would duplicateroleGrants[].adminand would need its own validation (an error wheninRolesis empty).Per-version availability
roleGrants[].adminroleGrants[].inheritroleGrants[].setOn PostgreSQL < 16, setting
inheritorsetinroleGrantsshould be reported as an error on theDatabaseRole(status.applied: falsewith a clearstatus.message), because the PostgreSQL version of the target cluster is only known at reconcile time. It should not be ignored silently: before 16, memberships always follow the role-levelINHERITattribute and always allowSET ROLE, so ignoring the option would hide a security-relevant difference.I checked the underlying SQL on PostgreSQL 14.24 and 15.19. There,
pg_auth_membershas onlyadmin_option,GRANT … WITH ADMIN OPTIONandREVOKE ADMIN OPTION FOR …work, andWITH ADMIN true|false,WITH INHERITandWITH SETare all syntax errors.Generated SQL
For the spec above, the role is created without memberships, and every membership is then applied with
GRANT.PostgreSQL 16 and later (checked on 16.15 and 18.6):
The resulting
pg_auth_membersrows:pg_monitorread_only_groupPostgreSQL 14 and 15 (checked on 14.24 and 15.19). The
inheritandsetoptions of theroleGrantsentry are rejected there, so the entry can only carryadmin:Here
pg_auth_membershas onlyadmin_option, which istforpg_monitorandfforread_only_group.Changes to an existing role are applied as follows:
inRolesorroleGrantsREVOKE "r" FROM "app";REVOKE "r" FROM "app";adminset back tofalseGRANT "r" TO "app" WITH ADMIN false;REVOKE ADMIN OPTION FOR "r" FROM "app";inherit/setchangedGRANT "r" TO "app" WITH INHERIT false, SET true;inheritchangedALTER ROLE "app" NOINHERIT ...;ALTER ROLE "app" NOINHERIT ...;Naming and consistency
roleGrants[].namematches how the rest of the CRD names a grantee (UsageSpecand others), so I'd keepname.roleGrantssays what is granted: role membership (GRANT role TO role). It leaves room for object-level privileges, which are a separate topic, and avoids a baregrantsthat could be read as either.grant/revokeintents are safer because other actors may legitimately hold grants. That is deliberate and worth stating in the docs.Deprecation
I don't think we should deprecate role-level
inherit: it is a real PostgreSQL role attribute, and the only inherit control before PostgreSQL 16.On
DatabaseRole, onceroleGrantshas shipped,inRolesbecomes redundant, so we may want to deprecate it there in a later release. On.spec.managed.roles,inRolesstays as the only way to declare memberships. That needs a separate maintainer discussion, not this change.gbartolini commentedon Oct 23, 2026
I am tentatively scheduling this for 1.31.
feat: set the membership options of a declarative role
gbartolini commentedon Nov 10, 2026
Unfortunately, due to time constraints (we need to release 1.31) and the complexity that emerged with ticket #11826, we must postpone this feature request to 1.32.