Skip to content

[Feature]: Support role membership options ADMIN, INHERIT, SET #10007

Description

@jonasbadstuebner

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: true

I 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

Activity

  1. jonasbadstuebner commented on Feb 18, 2026

    @jonasbadstuebner
    ContributorAuthor

    Another idea proposed by a colleague of mine would be to use #5341 and remove the inRoles field for the new CRD, replacing it with roleGrants (or changing the behavior of the inRoles field).
    I'll add a comment to that issue as well.

  2. jsilvela commented on Feb 18, 2026

    @jsilvela
    Contributor

    Looking at the implementation, the role memberships defined in roleGrants would not be managed when creating the user, but during UpdateMembership.

    Sort of... that's not a peculiarity of CloudNativePG. It's coming from Postgres. In the Postgres CREATE ROLE we don't have fine-grained control of the IN ROLES having 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:

    • IMO it should not be done before [Feature]: Declarative role management (DatabaseRole CRD) #5341
    • a design question. Do roleGrants belong as a child of role? I could see an independent grant object 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.
  3. jsilvela commented on Feb 18, 2026

    @jsilvela
    Contributor

    Beside my comments above, I wonder:

    • isn't it easier to manage this with migrations or some such, adding the required GRANT commands, instead of wanting CloudNativePG to do everything?
    • and, if you want some user roles to inherit from the application role, but some not, isn't this an indication that you should have a second "applicationBis" role that is less powerful than the first, that those other user roles could inherit from?
  4. jonasbadstuebner commented on Feb 23, 2026

    @jonasbadstuebner
    ContributorAuthor

    Hey @jsilvela,
    thank you for looking into my request!

    You have to issue GRANT commands for exact setups.

    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 through GRANT anyway.
    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 wonder if this is worth adding to the CRD.

    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 inRoles field - if there is no reason to have creation of a role fail if the requested parent roles are not present - with a roleGrants field, as described above.

    if we move to an Role CRD, we should have a Grant CRD

    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.

    isn't it easier to manage this with migrations or some such
    [...]
    isn't this an indication that you should have a second "applicationBis" role

    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.

  5. github-actions commented on Apr 25, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 60 days with no activity.

  6. jonasbadstuebner commented on Apr 25, 2026

    @jonasbadstuebner
    ContributorAuthor

    Please mark it as „never stale“, I‘m only waiting for #5341

  7. jonasbadstuebner commented on Apr 25, 2026

    @jonasbadstuebner
    ContributorAuthor

    Not stale

  8. gbartolini commented on Jun 24, 2026

    @gbartolini
    Contributor

    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.

  9. jonasbadstuebner commented on Jun 25, 2026

    @jonasbadstuebner
    ContributorAuthor

    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:

    • Application Roles: Each microservice gets a dedicated role that owns its database.
    • Developer Roles: Each developer gets a personal login role (owning no objects). For developing, debugging or hotfixing, they can log in and run 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 have INHERIT FALSE.
    • Connection Roles: We grant dedicated roles to the developers based on if they are allowed to log in directly to the database or through the Pooler/PgBouncer. These memberships require 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

    • Alter a roles grants after creating it - which is easily forgotten and won't be noticed immediately - also it's not GitOps or in any other way declarative
    • Run a custom controller/sidecar/... to ensure the permissions are as we want them to be - which is putting work into a workaround and not actually tackling the problem at its root

    Where I would like to be
    I would like to manage the INHERIT membership 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.

  10. 6 remaining items

  11. gbartolini commented on Oct 23, 2026

    @gbartolini
    Contributor

    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

    spec:
      name: app
      inherit: true          # role-level: ALTER ROLE app INHERIT (all PostgreSQL versions)
    
      inRoles:               # simple form: plain membership with PostgreSQL defaults
        - read_only_group
    
      roleGrants:            # form with options, one entry per membership
        - name: pg_monitor
          admin: true        # WITH ADMIN OPTION (all supported PostgreSQL versions)
          inherit: true      # WITH INHERIT TRUE|FALSE (PostgreSQL 16+)
          set: true          # WITH SET TRUE|FALSE (PostgreSQL 16+)

    roleGrants is available only on the DatabaseRole CRD. The managed roles in the Cluster spec (.spec.managed.roles) keep inRoles as it is today and don't get roleGrants: The main reason is error reporting. DatabaseRole is its own resource, so a failure in one role (an unsupported option, a missing parent role, a rejected GRANT) shows up on that object, in status.applied, status.message and its conditions, and can be seen with kubectl get databaseroles. Roles under .spec.managed.roles are reconciled as one list and reported in the Cluster status, 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 on DatabaseRole, and as for the work on password management. DatabaseRole is also where declarative role management is going, so I'd rather not grow the Cluster API further. The rest of this comment is about DatabaseRole.

    Two lists, two jobs

    • inRoles stays as the simple, backwards-compatible list: "make this role a member of these roles", with PostgreSQL defaults.
    • roleGrants is the form with options. A roleGrants entry with only a name is equivalent to an inRoles item.
    • Both are reconciled through the same GRANT / REVOKE path, on creation and on update. Changes to either list are applied to existing roles.
    • A role must not appear in both lists, and names within roleGrants must be unique. The webhook / CRD validation should reject both cases, instead of letting one entry silently win.

    Role-level vs membership-level options

    • spec.inherit is the role attribute (rolinherit). It exists on all supported PostgreSQL versions, and it is the default for every membership of the role.
    • roleGrants[].inherit and roleGrants[].set are per-membership and exist only from PostgreSQL 16. The role-level inherit is the fallback for memberships that don't set their own.
    • roleGrants[].admin is 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.
    • I'd keep admin in the API: we want it to be complete. I would not add a role-level admin shorthand. It would duplicate roleGrants[].admin and would need its own validation (an error when inRoles is empty).

    Per-version availability

    Option PostgreSQL 14–15 PostgreSQL 16+
    roleGrants[].admin supported supported
    roleGrants[].inherit not supported supported
    roleGrants[].set not supported supported

    On PostgreSQL < 16, setting inherit or set in roleGrants should be reported as an error on the DatabaseRole (status.applied: false with a clear status.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-level INHERIT attribute and always allow SET 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_members has only admin_option, GRANT … WITH ADMIN OPTION and REVOKE ADMIN OPTION FOR … work, and WITH ADMIN true|false, WITH INHERIT and WITH SET are 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):

    CREATE ROLE "app" NOBYPASSRLS NOCREATEDB NOCREATEROLE INHERIT NOLOGIN NOREPLICATION NOSUPERUSER CONNECTION LIMIT -1;
    GRANT "read_only_group" TO "app";
    GRANT "pg_monitor" TO "app" WITH ADMIN true, INHERIT true, SET true;

    The resulting pg_auth_members rows:

    role admin_option inherit_option set_option
    pg_monitor t t t
    read_only_group f t t

    PostgreSQL 14 and 15 (checked on 14.24 and 15.19). The inherit and set options of the roleGrants entry are rejected there, so the entry can only carry admin:

    CREATE ROLE "app" NOBYPASSRLS NOCREATEDB NOCREATEROLE INHERIT NOLOGIN NOREPLICATION NOSUPERUSER CONNECTION LIMIT -1;
    GRANT "read_only_group" TO "app";
    GRANT "pg_monitor" TO "app" WITH ADMIN OPTION;

    Here pg_auth_members has only admin_option, which is t for pg_monitor and f for read_only_group.

    Changes to an existing role are applied as follows:

    Change in the spec PostgreSQL 16+ PostgreSQL 14–15
    Entry removed from inRoles or roleGrants REVOKE "r" FROM "app"; REVOKE "r" FROM "app";
    admin set back to false GRANT "r" TO "app" WITH ADMIN false; REVOKE ADMIN OPTION FOR "r" FROM "app";
    inherit / set changed GRANT "r" TO "app" WITH INHERIT false, SET true; rejected
    Role-level inherit changed ALTER ROLE "app" NOINHERIT ...; ALTER ROLE "app" NOINHERIT ...;

    Naming and consistency

    • roleGrants[].name matches how the rest of the CRD names a grantee (UsageSpec and others), so I'd keep name.
    • roleGrants says what is granted: role membership (GRANT role TO role). It leaves room for object-level privileges, which are a separate topic, and avoids a bare grants that could be read as either.
    • Role membership is declared state: the role definition owns its whole membership set, so removing an entry revokes it. This differs from object-level privileges on shared objects (database, schema), where explicit grant / revoke intents 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, once roleGrants has shipped, inRoles becomes redundant, so we may want to deprecate it there in a later release. On .spec.managed.roles, inRoles stays as the only way to declare memberships. That needs a separate maintainer discussion, not this change.

  12. added
    enhancement 🪄
    and removed
    triage
    on Oct 23, 2026
  13. added this to the 1.31.0 milestone on Oct 23, 2026
  14. gbartolini commented on Oct 23, 2026

    @gbartolini
    Contributor

    I am tentatively scheduling this for 1.31.

  15. moved this to In Progress in Roadmapon Oct 23, 2026
  16. added a commit that references this issue on Nov 2, 2026
    c6f7c46
  17. modified the milestones: 1.31.0, 1.32.0 on Nov 10, 2026
  18. gbartolini commented on Nov 10, 2026

    @gbartolini
    Contributor

    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.

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

Metadata

Metadata

Assignees

Labels

enhancement 🪄

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions