Migrate users from a legacy database with a Keycloak user storage provider

Move users from a home-grown login database to Keycloak without a password reset, using a User Storage SPI provider in import mode that verifies legacy hashes at first login and re-hashes them into Keycloak.

On this page

Migrate users from a legacy database to Keycloak without a password reset

A team that owns a home-grown login table can move those users into Keycloak without forcing a password reset. The approach is a User Storage SPI provider running in import mode: Keycloak verifies each user's existing password hash against the legacy store at first login, then creates a local copy of the user and re-hashes the password with Keycloak's own algorithm. The effort depends mainly on three things: the hash scheme the legacy table uses, the number of applications that sign in against it, and the number of account stores that must be read. Each of these changes the custom code, the test matrix and the cutover sequence, so none of them can be estimated well from the outside.

Start by answering four questions from your own environment. Which hash function and parameters does the table use, and is there a salt stored per user? Are passwords stored in a form Keycloak's out-of-the-box verification can read, or does the provider need custom hashing code? Which applications authenticate against this table today, and do they all use one login path? Is there more than one table, directory or service that holds credentials? Keycloak supports argon2 and PBKDF2 natively and documents how to add a custom hashing algorithm in the Keycloak server developer guide, so an unusual legacy scheme is the most common reason the work grows.

DeployOpen scopes this work as a fixed-scope engagement. The written scope should name the hash schemes, the applications and their login flows, the account stores, the test users and the cutover order, and it should state what the client team must provide, such as database read access, a test copy of the data and an owner for each application. Ask for the quote to split one-time work (provider build, testing, migration runs) from anything recurring (support or retainer). Once every application has moved and the imported users are unlinked, the legacy store can be retired, and that retirement should be a named item in the plan with its own sign-off.

Which legacy login features map to Keycloak and which need design work

This table compares common home-grown login features with their Keycloak equivalents and marks each as a direct mapping, a custom provider or mapper job, or a rebuild. Use it to scope the migration before writing the User Storage SPI provider, because the legacy hash scheme and custom password rules decide most of the effort.

Legacy featureKeycloak equivalentMigration status
Username and email loginRealm login with username or email, configured in realm login settingsMaps directly. Confirm that legacy emails are unique and lowercased before import
Password hashesArgon2 and PBKDF2 verified out of the box, other schemes need a custom hashing providerNeeds design work. Confirm the exact legacy algorithm, salt and iteration settings first
Password policy rulesKeycloak password policies such as length, character classes and historyRebuild. Legacy rules not expressible as policies need a custom policy provider
Profile attributes (name, phone, locale)User attributes and user profile configurationMaps directly through the provider, with a mapper for any non-standard fields
Roles and groupsRealm and client roles, groups and group membershipNeeds mapping. Decide which legacy roles become realm roles versus client roles
Account status (active, locked, disabled)User enabled flag and brute force detection settingsNeeds design work. Lockout counters rarely carry over and usually restart at zero
Multi-factor authenticationOTP, WebAuthn and passkeys configured in authentication flowsRebuild. Existing TOTP secrets usually cannot be moved, so plan re-enrollment
Social sign-in linksIdentity brokering with linked identity providersNeeds mapping. Match on verified email and document users who have no link
Session lifetimes and idle timeoutsRealm and client session and token lifetime settingsRebuild. Set new values per client and test logout in each application
Custom token claimsProtocol mappers on clients and client scopesRebuild. Each bespoke claim needs an explicit mapper and a consumer check

How the import mode user storage provider handles the first login

The first-login flow is where a legacy migration either works or quietly fails, so this section is the one to walk through with your security and application teams before any build starts. Keycloak's User Storage SPI lets the server connect to an external user store and credential source. The built-in LDAP and Active Directory support is an implementation of the same SPI, and your custom provider is enabled per realm under User Federation. With the import strategy, the provider creates a local copy of each user in the Keycloak database the first time that user is found, and after that the legacy store is no longer queried for profile data.

On a first login, Keycloak asks the provider to verify the submitted password against the legacy hash. If the check succeeds, the provider hands the credential back so Keycloak can store it in its own hashing format, which is argon2 or PBKDF2 unless you configure otherwise. The next login then runs entirely inside Keycloak, and the legacy hash is no longer needed for that user. If the check fails, the user is rejected and nothing is written, so a wrong password never overwrites a good local credential. Confirm how your provider behaves for users who exist in the legacy store but have not yet logged in, because that population determines how long the legacy database must stay reachable.

The custom provider is written in Java against the Keycloak Server Developer Guide, which covers the provider interfaces and their packaging. Before estimating the work, your team must confirm which legacy hash formats the provider has to verify, including salt handling, iteration counts and any application-specific wrapping, and whether any hashes are stored in a form that cannot be verified without the original application code. Ask whether the provider needs a custom hashing algorithm in addition to the built-in options, since that is a separate build item. Test verification against a sample of real hashes from each format in a non-production realm first.

Documented paths for moving users and credentials

A User Storage SPI provider is the only documented path that lets Keycloak verify a password against your legacy store at login. You write a provider that implements the user storage interfaces, enable it per realm under User Federation, and set it to import mode. In that mode Keycloak creates a local copy of each user the first time they sign in, after which it stops querying the legacy store for that user. The provider needs to check the submitted password against the stored hash, so it works only when your legacy hashes use an algorithm you can reproduce in Java.

  • What it moves: Usernames, profile attributes and password credentials, the latter verified at first login and then re-hashed into Keycloak's own algorithm.
  • What it cannot move: Passwords you only hold in plaintext are not a reason to skip verification, but a hash you cannot reproduce means the provider cannot confirm a password and users must reset it.
  • When it fits: Large user bases where you want zero forced resets and can tolerate a long tail of users who sign in once after cutover.
  • Build effort: The provider is custom code that must be maintained across Keycloak upgrades, so its dependencies and packaging belong in your upgrade plan.
  • Operations: Keep the legacy store reachable until every application has moved, then unlink imported users and retire it.

Before committing, confirm the exact algorithm, salt and iteration parameters of your current hashes against the Keycloak server developer guide and the server administration guide, which also describe adding custom hashing algorithms.

Sequence: inventory, pilot, parallel run, cutover and unlinking

This sequence moves users from a home-grown login database into Keycloak without a password reset, keeping the legacy store authoritative until each application is proven on Keycloak and only then detaching the imported users and retiring the old store.

  1. Inventory users, hash schemes and readers

    The identity owner and the application leads list every user population in the legacy database, every password hash scheme in use (including older schemes that may coexist with newer ones), and every application or script that reads the database directly. Direct readers matter because they will keep working against the old store unless they are moved or confirmed retired. The step is done when each application has a named owner, each hash scheme has sample records that the team can verify, and direct readers are classified as migrate, replace or retire.

  2. Select and run a pilot realm

    Create a separate Keycloak realm with a small group of test users drawn from the legacy database, and configure one low-risk application against it. The pilot shows whether login, token claims, group or role mapping and profile fields behave as the application expects before any production user is involved. Confirm with the application team that the pilot exercises password login, logout and password change. The step is done when the pilot group signs in through the application with no unexpected prompts or missing attributes.

  3. Build and test the provider against copied data

    Develop the User Storage SPI provider in import mode and test it against a copy of the legacy data, never the production database. The provider must look up users, verify each legacy hash scheme, and let Keycloak write a re-hashed credential on first successful login. Test wrong passwords, locked or disabled accounts, unusual characters, and users who exist only in one system. Keycloak supports argon2 and PBKDF2 natively, so any other legacy scheme needs a custom hashing implementation, which the Server Developer Guide describes. The step is done when every hash scheme verifies correctly on the copy and a re-hashed credential is stored after login.

  4. Run applications in parallel with the legacy store authoritative

    Point applications at Keycloak for sign-in while the legacy database remains the system of record for password changes and account administration. Each user is imported on first login, and the provider verifies the legacy hash at that moment. Because password changes still go to the legacy store, the team must also decide how a changed password is reflected in Keycloak before the next login. Monitor failed logins, duplicate accounts and user counts against the legacy table. The step is done when error rates and account counts stay within the expectations the team set during the pilot.

  5. Cut over each application in turn

    Move applications one at a time: update the client configuration, redirect URIs and secrets, run a smoke test with real user accounts that have agreed to it, and switch the application's login to Keycloak. Keep a rollback path to the previous login method until the application is confirmed stable. Record which applications still use direct database reads, because those need their own migration plan before the store can be retired. The step is done when the application's sign-in, logout and session behavior are signed off by its owner.

  6. Confirm every application and direct reader has moved

    Before any unlinking, reconcile the inventory from the first step against the cutover record. Every application must authenticate through Keycloak, and every direct database reader must be migrated, replaced or formally retired. Ask the owners of batch jobs, reporting tools and integrations to confirm, because these are often missed. The step is done when the inventory shows no open items and the owner of the legacy store has signed the list.

  7. Unlink imported users as a separate decision

    Unlinking is a deliberate later step, not part of the cutover. Once all applications have moved, the team can unlink imported users so that Keycloak stops querying the legacy store for them and holds the only copy of their credentials and profile data. Confirm first that no user remains outstanding in the legacy store that has not logged in, and decide how those accounts will be handled. The step is done when unlinking has been run on a tested copy, a rollback plan is documented, and the change is approved by the identity and application owners.

  8. Retire the legacy store after a verified period

    Only after unlinking and a period of normal operation should the legacy database be made read-only, then archived and decommissioned under the organization's retention rules. Keep an export of the legacy data for audit purposes as your compliance team requires, and confirm the retention period with them. The step is done when the legacy store is no longer reachable by any application, the archive has been verified, and the decommissioning record is filed with the change owner.

How a first login moves a user from the legacy store into Keycloak

The flow starts when a user signs in to an application, which redirects to Keycloak. Keycloak checks its own local copy first. If the user has not been imported yet, the User Storage provider looks the user up in the legacy database and checks the password against the legacy hash. On success, Keycloak stores a local copy with a credential it has re-hashed, then issues tokens. A failed check returns the user to the login page without creating anything in Keycloak. The legacy database is only read during this step, so it stays read-only until every application has moved.

First login imports a user from the legacy database while the old store stays read-only.

What drives the cost and duration of this migration

Most of the effort in a legacy database migration comes from what the old system does beyond storing users. Each driver below adds build, test and cutover work, and each one is a line item to name in a written scope before work starts.

Legacy hash schemes

Every distinct password hash format in the legacy database needs its own verification code in the user storage provider, plus tests against real samples. Ask whether the old system used one scheme or several across account generations, and whether any hashes use salts or parameters stored in separate columns. Whoever builds the provider is paid for each scheme, so ask the quote to list them by name and count, and to state who supplies sample hashes for testing.

Keycloak developer docs

Applications and login integrations

Each application that signs users in against the legacy store must be repointed to Keycloak, and each one needs its own redirect URIs, client settings and logout tests. The unlink step cannot happen until the last application has moved. Ask for a list of every application, its protocol, and which team owns its cutover, since coordination time often exceeds build time.

Direct database readers and reports

Batch jobs, reporting tools and internal services that read the legacy tables directly are easy to miss and break once the store is retired. Each reader needs an inventory entry, a replacement source, and a decision on whether it reads from Keycloak or from a copy. Ask the quote to include a discovery pass against database logs and query history, and to name who signs off on each replacement.

Custom profile and role data

Attributes such as department, licence numbers, entitlements and application roles must map onto Keycloak user attributes, groups and client roles. Mapping gaps show up as missing claims in tokens, which application teams find late. Ask for a field-by-field mapping document, the list of attributes that will be transformed, and the owner who approves any value that cannot be mapped cleanly.

MFA and social login to rebuild

If the legacy system offers one-time passwords, passkeys or social sign-in, those features must be configured in Keycloak and tested with migrated accounts. Enrolled second factors may not carry over automatically, so users could need re-enrollment. Ask whether existing MFA enrollments will be moved or reset, who communicates that to users, and whether the quote includes configuration of Keycloak's OTP, WebAuthn and identity provider settings.

Parallel run window

Running the legacy store and Keycloak side by side while applications move in waves adds monitoring, support and synchronization effort for as long as the window lasts. Each extra week of parallel running keeps two login paths in service. Ask the quote to state the number of waves, the rollback criteria for each wave, who watches login error rates, and when the legacy store will be unlinked and retired.

Questions buyers ask before moving users out of a home-grown database

How much effort does moving users out of a home-grown database take?

Effort depends mostly on how many applications authenticate against the legacy store, how many user records and password hash formats exist, and how the login database is wired into your apps. A single login path with one hash scheme is far simpler than several applications that each read the table directly or use different hashing. The work covers mapping the legacy schema to Keycloak users and attributes, writing the user storage provider, testing hash verification, moving each application to OpenID Connect, and retiring the old store. Ask any partner to break the quote into those components so you can see which ones your own team could take on.

How long does a legacy user database migration usually run?

Duration is driven by the number of applications to move, the hash algorithms you must support, and how many test cycles each application needs before cutover. The import strategy lets users keep signing in during the transition, so the long pole is usually application migration and testing rather than the data itself. Plan for a period where legacy and Keycloak both run, and ask for a timeline in the written scope that lists milestones per application. Any duration you are quoted should trace back to those specific inputs.

What does the migration need from our team?

You need an owner for the legacy schema and password hashing, people who can confirm which applications read the login database, and application owners who will test sign-in, logout and session handling after each switch. Security or compliance staff should review how hashes are verified, how the provider is deployed and where credentials are stored during the transition. Ask the partner to list these roles and time commitments in the scope so they can be planned before work starts.

Can our own engineers do this migration without a partner?

Yes, if your team has Java experience for writing a provider against the User Storage SPI, a working Keycloak deployment, and staff who understand the legacy hashing exactly. The provider is a custom extension, so it must be rebuilt and retested every time you upgrade Keycloak. Consider whether you can staff that ongoing work and the security review that comes with it. A partner can take on the build, the application cutovers or both, and the written scope makes the split explicit.

Do users have to reset their passwords during the move?

No, not in an import-mode design. The provider checks a submitted password against the legacy hash on first login, and if it matches, Keycloak stores the user locally and re-hashes the password using its own algorithm, such as argon2 or PBKDF2, which Keycloak supports out of the box. Users who never log in keep their legacy hash in the old store until they do. Confirm with your engineers that your existing hash format can be verified by the provider, because unusual or salted schemes need custom code.

What happens to users who never log in after the cutover?

They remain in the legacy store and are not copied into Keycloak until they authenticate, so the old database must stay available for them. Decide whether to keep those accounts indefinitely, force a reset through a communication campaign, or run a bulk import of the remaining records with a documented policy. Each option affects the retirement date and the security risk of keeping unused credentials. Make the choice explicit in the scope and record how it will be audited.

When can we switch off the legacy database?

Only after every application authenticates through Keycloak, the imported users have been unlinked from the legacy store, and you have decided what happens to dormant accounts. Keycloak's documented approach is to unlink imported users once all applications have moved, then retire the legacy store. Keep a backup and a tested restore path until your retention policy allows deletion, and confirm the final step with your security and compliance owners.

What usually goes wrong and how to prevent it

Most failed legacy user migrations fail in the seams: hashes nobody inventoried, stores that stay live longer than planned, and applications that depend on details the cutover changed. The cards below cover the risks we see most often, each with a check to run before the cutover window.

Unknown hash schemes found late

Legacy tables often hold passwords under several schemes, such as bcrypt, salted SHA-2 or a custom construction added during a past rewrite. Keycloak supports argon2 and PBKDF2 out of the box, so any other scheme needs a custom hashing provider before the first login can be verified. Inventory every hash format and its cost parameters against the user table before the provider is built, and confirm which accounts still use old schemes.

Keycloak admin docs

Unlinking the legacy store too early

Once imported users are unlinked and the legacy store is retired, any application still reading from it or any user who never logged in has no fallback. The import strategy stops querying the external store after the local copy exists, so a missed application or a stale batch becomes an outage. Keep the legacy store read-only and running until every application has moved and a dated report shows no remaining dependent clients.

Users diverging across two stores

During a gradual migration, a user who changes a profile field or password in the old system while Keycloak already holds an imported copy ends up with two conflicting records. Nothing in the import strategy reconciles these automatically. Freeze writes to the legacy store for migrated accounts or route all changes through Keycloak, and decide which store wins for each attribute before go-live.

Password changes applied to the wrong store

A password reset flow, support tool or admin script that still writes to the legacy database leaves Keycloak verifying the old hash and rejecting the new password, or the reverse. This failure is quiet until a user is locked out. Audit every system with write access to the legacy credential table and point password changes at Keycloak before the first account is migrated.

Missing profile attributes

Legacy schemas often hold fields that have no place in the default Keycloak user model, such as employee numbers, branch codes or consent dates. Tokens built from the imported user may then lack claims that downstream applications expect. Map every legacy column to a Keycloak attribute or a deliberate omission, and test that each application still receives the claims it reads.

Session and token claim changes

Moving identity to Keycloak changes issuers, client IDs, token lifetimes and the claim shape that applications parse. Sessions issued by the old system may not be honored, and applications that match on a legacy subject identifier can fail to find their existing users. Confirm that subject identifiers are preserved or mapped, and test token handling and logout in each application before its redirect cutover.

Keycloak developer guide

What the client team must provide and what it receives

The client team must provide the inputs that make a legacy hash verification possible before any code is written. That means documentation of the legacy schema, the hashing algorithm, its salt and iteration parameters, and how the hash string is encoded, plus a test copy of the database with sample hashed records, so the provider can be checked against real rows rather than assumptions. The team also needs an application inventory listing every client that signs in against the old store, with its redirect URIs, its owner who can approve cutover, and the protocol each one uses. Finally, it must decide how the account states map into Keycloak: which users are active, locked, disabled or pending, and whether any accounts with legacy credentials should be excluded from import. Confirm with your own engineers whether the hashing function is standard or custom, because a custom scheme requires a custom credential provider that the team must maintain.

In return, the team receives a tested User Storage SPI provider running in import mode, along with the realm configuration that enables it under User Federation and the mapper settings for user attributes. It also gets a runbook covering the first-login verification flow, the rehash step, the monitoring signals to watch, and the rollback path if verification fails for a subset of users. Parallel-run results show how the new login path behaves beside the legacy store for each application before traffic moves. The final deliverable is a decommission checklist that defines when imported users are unlinked and the legacy store is retired, and which owner signs off on each application before that step.

Responsibilities should be written into the statement of work before work starts. DeployOpen's fixed-scope engagements set out deliverables and timelines in writing, and the client should ask for the provider's test evidence, the parallel-run acceptance criteria and the decommission sign-off in that document. Ask for any effort or timeline estimate to be tied to the inputs above, because late discovery of hash formats or undocumented applications is the most common reason the work expands. The client should also confirm who operates the provider after handover, since it becomes part of the identity platform and needs an owner for upgrades to the Keycloak line.

Operating Keycloak after the cutover

Once users are imported and every application points at Keycloak, the work shifts to keeping the server recoverable, observable and upgradable. The cards below cover the items that most often get skipped after a legacy database migration, and they connect directly to the unlinking step, which should only happen after these controls are in place.

Admin access and recovery

Before the legacy store is retired, confirm who holds administrator rights in the target realm and that at least one recovery path exists outside normal sign-in. Imported users may have been mapped to roles from the old system, so review those role assignments rather than assuming they carried over correctly. Record the break-glass procedure in your runbook and test it on a non-production copy of the realm. Keep the admin console and Administration REST API on a hostname or context path separate from public frontend URLs, as the production guide describes.

Server admin docs

Metrics endpoint enabled

Start Keycloak with --metrics-enabled=true so that Prometheus-format metrics are exposed at /metrics on the management interface. Migration can change login volume and failure patterns, so you want a baseline captured during the parallel period. Decide whether to enable the optional HTTP request and cache histograms, since they add detail at the cost of extra series. Confirm the management port is reachable only by your monitoring system, not from public networks.

Metrics guide

Schema migration on upgrades

Keycloak migrates its database schema automatically at startup by default. For a realm that was populated by a large import, plan the first upgrade with a database backup taken immediately before it and a rollback decision documented in advance. Where your database team requires change control, the upgrading guide describes manual migration, where administrators apply the SQL themselves. Ask your database team to confirm the supported database version against the Keycloak database guide before any upgrade window is booked.

Upgrading guide

Operator update approval

If you run Keycloak on Kubernetes through the Operator, the Operator documentation strongly recommends manual approval mode for operator updates. Automatic upgrades can move the server version without a planned test window, which matters when the realm contains imported credentials and freshly rehashed passwords. Make the approval step part of your change process, and review release notes before approving. Record which version each environment runs so that staging and production stay aligned.

Operator installation

Retiring the user storage provider

Once every application has moved and the imported users are unlinked, remove the legacy user storage provider from the realm under User Federation. Unlinking should follow a check that no client still depends on the old store for password verification or profile lookups. Keep an encrypted export of the legacy database and the provider code under retention, so you can answer later questions about migration coverage. Confirm the removal in a staging realm first so that login paths for users who never returned are understood.

Server developer guide

How DeployOpen scopes and delivers a Keycloak user migration

DeployOpen plans, builds and hands over the Keycloak side of a legacy user database migration inside your own infrastructure. The work runs from a written scope through a tested user storage provider, a verified login cutover, and a documented retirement of the old store once every application has moved.

How we scope

  • We inventory the legacy login database, its password hash formats, and the applications that authenticate against it.
  • The written scope names each application, realm, client and login flow that moves to Keycloak in the migration.
  • We agree in writing who supplies the legacy database access, test accounts, and the application owners who verify their own sign-in.

How we build

  • We build a User Storage SPI provider in import mode that verifies legacy hashes at first login and stores the user in Keycloak.
  • We test every legacy hash format against sample accounts in a non-production realm before any real user is touched.
  • We re-hash each verified password into the Keycloak hashing policy, then confirm that later logins no longer query the legacy store.

How we hand over

  • You receive the provider source, its build and deployment steps, and the realm configuration as reviewable artifacts.
  • The runbook covers the cutover order, rollback steps, the unlink procedure, and the checks that confirm the legacy store is no longer needed.
  • Your team receives test evidence showing hash verification, first-login import, and unlinking results for each application.

How we support

  • Engagements are fixed-scope, with the scope, deliverables and timelines agreed in writing before work starts.
  • Ongoing support, upgrades and continuous testing are available as a retainer, including updates to the provider when Keycloak changes.
  • Before the legacy database is retired, we can retest the remaining sign-in paths and confirm the cleanup record for your audit file.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.