Keycloak stores role data in its relational database, typically PostgreSQL or MySQL. Roles are entities linked to either a realm or a specific client. Each row in the keycloak_role table represents one role definition, with columns defining name, description, composite relationships, and references to parent realms or clients. Database roles are used to determine authorization grants during token creation and validation.
Realm vs. Client Roles
Realm roles live at the global level for a realm. Client roles are scoped to a single client application. In the database, they share schema structure, but their references differ: realm roles link to REALM_ID; client roles link to CLIENT_ID. This separation allows identity architects to maintain strict boundaries between application-specific permissions and global rules.
Composite Roles
Composite roles aggregate multiple roles. The database tracks composites in join tables such as composite_role mapping parent roles to child roles. When a composite role is assigned to a user, all its child roles are automatically granted. This logic is efficient at runtime but requires careful design to avoid privilege escalation.
Role Assignments in the Database
Role mappings between users and roles are stored in tables like user_role_mapping and group_role_mapping. Users can inherit roles via group assignments, and Keycloak’s database ensures these relationships are chained correctly. When a token is generated, Keycloak queries these mappings to embed accurate claims.