Skip to content

Instantly share code, notes, and snippets.

@carlin-q-scott
Last active July 18, 2024 19:41
Show Gist options
  • Save carlin-q-scott/98f96612d90a7ed359e636c089400c7e to your computer and use it in GitHub Desktop.
Save carlin-q-scott/98f96612d90a7ed359e636c089400c7e to your computer and use it in GitHub Desktop.
Reset admin account password for Keycloak using SqlServer
-- Removes admin account and temporarily reassigns master realm users to a fake realm so that Keycloak will re-create the admin account using the KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD env vars.
declare @adminId varchar(36);
set @adminId = (select ID from user_entity where USERNAME = 'admin');
delete from credential where user_id = @adminId;
delete from user_role_mapping where user_id = @adminId;
delete from user_required_action where user_id = @adminId;
delete from user_entity where id = @adminId;
declare @masterRealmId varchar(36);
set @masterRealmId = (select id from realm where realm.name = 'master');
update user_entity set realm_id='banana' from user_entity where realm_id = @masterRealmId;
-- Run after running remove-admin.sql and restarting Keycloak
declare @masterRealmId varchar(36);
set @masterRealmId = (select id from realm where realm.name = 'master');
update user_entity set realm_id=@masterRealmId where realm_id = 'banana';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment