Keycloak database schema migration for large databases

When Keycloak tables are large, the schema migration is the step most likely to extend an upgrade window. This page explains the automatic and manual paths, the 300,000-entry index threshold and the backup you need before starting.

On this page

Keycloak database schema migration for large databases: when the upgrade stalls at the database

Keycloak migrates its database schema automatically at startup by default, and that default is usually fine for small and medium databases. When your upgrade plan includes large tables, plan for a different path: a manual schema migration, a review of the migration timeout, and a verified backup taken before you touch anything. The schema migration runs inside the upgrade itself, so a long-running table change can hold up the new release's startup and make a rolling upgrade look like a failure when the database is simply still working.

Treat the migration as its own step with its own risk. Keycloak has a migration timeout setting for large databases, and the upgrading guide describes manual migration, where administrators apply the SQL themselves, as the approach for large deployments. Confirm the exact setting names and the manual workflow for your target release in the Keycloak upgrading guide, because they change between releases. Also confirm which tables in your environment exceed the row threshold for skipped indexes, since those indexes must be created by hand after startup.

The correct path depends on your starting version, your target version and your database engine, and upgrades run through supported minor and patch steps rather than one large jump. Record the current version, the database and its version, the row counts of the largest tables, and the restore procedure before you choose a path. Then test the full sequence on a copy restored from a backup that you have actually restored once, not just a dump file that exists. If your team cannot produce those facts, that gap is the first thing to close before scheduling a production window.

What in the Keycloak upgrade changes the database work

This table compares each part of a Keycloak upgrade that touches the database and states what the database team must do about it. Read it before scheduling the upgrade window, because the largest tables decide whether the default startup migration is acceptable. The main takeaway is that the schema migration behavior is version specific, so confirm it in the upgrading guide for your target release.

ChangeImpact on the databaseAction needed
Automatic schema migration at startupDefault behavior: Keycloak changes the schema when the new version starts, so the migration runs inside the startup window.Measure table sizes first and confirm whether startup migration fits your outage tolerance.
Manual schema migrationAdministrators apply the SQL themselves before or after the binaries change, which the upgrading guide suggests for large deployments.Generate the SQL for the target version, review it with your DBA, and apply it in a planned change.
Migration timeout settingLarge tables can exceed the time allowed for automatic migration, which interrupts startup.Set the timeout for your environment and test it against a copy of production data.
Skipped indexes above 300,000 entriesWhen a table holds more than 300,000 entries, Keycloak skips creating some new indexes during automatic migration.Check row counts, then apply the logged SQL manually after startup and confirm the indexes exist.
Backup reminder in the upgrading guideThe schema change is not reversed by restarting the old version, so a failed migration can leave the database unusable for that release.Take a backup you have tested to restore before starting the upgrade.
Keycloak 26.8 release notesThe 26.8 notes add login failures persisted to the database by default, which can increase write volume on busy deployments.Read the 26.8 notes and size database write capacity and table growth before upgrading to that line.
Target database engineSupported production databases include PostgreSQL 14 to 18, along with MariaDB, MySQL, SQL Server, Oracle, Aurora PostgreSQL and Azure SQL.Confirm your engine and version are supported for the target release and plan any engine upgrade separately.
Migration behavior per target versionMigration details, including index handling, can change from one release to the next.Read the upgrading guide for each release on your upgrade path, not only the final target.

Choosing the automatic or manual migration path

The path starts with a verified backup, because every branch depends on being able to restore. Count rows in the large tables next, then choose between automatic migration at startup and manual SQL. If the migration fails or times out, roll back to the verified backup. When it completes, check the startup logs for index SQL that Keycloak skipped, apply that SQL after startup, and confirm that logins work on the upgraded instance.

Migration path from backup through rollback or post-startup index work.

Automatic or manual schema migration for large tables

Keycloak migrates its database schema automatically when it starts, and this is the default behavior. The Keycloak process runs the schema changes itself, so the upgrade is complete once the new nodes come up against the database. For small and medium tables this keeps the work simple, because no separate database step is needed. On large tables the startup migration takes longer, and the migration timeout setting governs how long Keycloak waits for it. Confirm the timeout default and its setting name in the Keycloak upgrading guide for your target version before you plan a maintenance window.

  • Who runs the work: the Keycloak process, under the database account it is configured to use, so that account needs DDL rights during the upgrade.
  • When the schema changes: at the first startup of the new version, which in a cluster means the schema is changed while the upgrade is in progress.
  • Skipped indexes: above 300,000 entries in a table, Keycloak skips some new indexes and logs the SQL to apply after startup, so queries on those tables may run slower until the team applies them.
  • Timeout behavior: a long migration can exceed the configured timeout, so test the duration on a copy of production data before the real window.
  • Rollback: a restorable backup taken before the upgrade is the practical rollback path.

Before the upgrade, confirm the backup restores to a test database, and after startup, check the logs for the skipped-index SQL and apply it in a planned change.

How to run the database migration step in order

This sequence turns a Keycloak database upgrade on large tables into a rehearsed, reversible change, with a restore-tested backup, a logged list of skipped indexes and a rollback point kept until production checks pass.

  1. Confirm target Keycloak and database versions

    Before anything else, the team confirms the target Keycloak release against the upgrade notes for every minor version between the current and target releases, and checks that the production database engine and version are on Keycloak's supported list. For PostgreSQL, the supported range is 14 to 18, and the PostgreSQL versioning policy shows which major versions are still receiving updates. Owner: platform or identity engineering lead. The gate: a written version pair, with any database upgrade scheduled separately, and a note of any breaking changes that affect your custom providers or themes. Ask your database team to confirm the support window for your current engine version so the upgrade does not coincide with an unsupported database.

  2. Inventory table sizes and row counts

    The database owner runs a read-only query against the Keycloak schema to list the largest tables and their row counts, with the current migration timeout and migration mode noted from the deployment configuration. Tables above the 300,000-row threshold are flagged, because Keycloak skips creating some new indexes on those tables during automatic migration. The output is a dated table of row counts that the team attaches to the change record. The gate: the list is reviewed by someone who can estimate migration time from it, and the team agrees whether the migration will run automatically or manually.

  3. Take a full backup and prove it restores

    The database owner takes a full backup of the production Keycloak database immediately before the change, using the engine's native backup method, and stores it outside the cluster that will be upgraded. A backup that has not been restored is not yet a rollback point, so the team restores it into a scratch database, starts a Keycloak instance against that copy, and confirms that the realm list, a test user login and the admin console all work. The gate: a successful restore and a documented test result, recorded with the backup identifier and time. If the restore fails, production stays on the current version until the backup is fixed.

  4. Rehearse the upgrade in a test environment

    The team deploys the target Keycloak version against a copy of the restored production database in a non-production environment, using the migration mode they plan to use in production. They start with automatic migration by default and capture the startup log, the duration and any timeout errors, adjusting the migration timeout only if the rehearsal shows it is needed. The rehearsal must also run the same Keycloak custom providers and themes that production uses. The gate: the rehearsal completes without schema errors, and the team records the elapsed time as a reference for the production window.

  5. Capture the logged SQL for skipped indexes

    After the rehearsal startup, the team searches the Keycloak log for the statements that were not applied because the table held more than 300,000 entries. Each logged statement is copied into a versioned SQL file that names the target table and the index, and the file is reviewed by the database owner before production use. This is the step most often missed when teams only watch for a successful startup. The gate: the SQL file is complete against the rehearsal log, and the database owner confirms how and when the statements will run in production.

  6. Upgrade production and apply the index SQL after startup

    During the approved window, the team stops the Keycloak nodes, confirms the backup is current, starts the upgraded release and watches for migration completion. Once Keycloak is running, the database owner applies the reviewed index SQL, creating the indexes under the database engine's own locking behavior, which the team checks during the rehearsal. If the team chose manual migration, the SQL for the schema changes is applied first using the same review path. The gate: the index statements finish without error, and the log shows no pending skipped-index messages on the next startup.

  7. Verify logins and admin functions

    The team runs a fixed test script against production: an end-user sign-in through a test client, a token refresh, a logout, an admin console login, creation and deletion of a test user, and a check of any federation or brokered login that production relies on. Metrics and health endpoints are checked where they are enabled. The results are recorded against the checklist from the rehearsal. The gate: every item passes or has an agreed workaround, and the on-call owner signs off that the service is healthy.

  8. Keep the backup until production checks pass

    The pre-upgrade backup is retained as the rollback point until the production verification has passed and the business owners of the applications that sign in through Keycloak have confirmed normal operation. The rollback procedure, which restores the backup and runs the previous Keycloak release, is documented and was rehearsed in the restore test. Only after that confirmation does the team delete or archive the backup under its retention policy. The gate: a written close-out note that names the backup, the sign-off and the date the rollback point was released.

What to prepare before the schema migration starts

Schema migration on a large Keycloak database is hard to reverse once tables have changed, so the preparation work matters as much as the upgrade itself. The six items below give the platform team, the database owner and the identity owners a shared checklist to finish before anyone starts the new release.

Restorable backup and test restore

Take a full database backup immediately before the upgrade, and restore it into a separate instance to prove it works. The Keycloak upgrading guide reminds administrators to back up the database before upgrading. The database owner produces the backup and the restore log, and the platform lead signs off that Keycloak can start against the restored copy. An untested backup is not a rollback plan.

Keycloak upgrading guide

Row-count inventory of large tables

List the row counts of the largest Keycloak tables, especially those holding events, sessions and user attributes, and record them before the upgrade. Keycloak skips some new indexes once a table holds more than 300,000 entries, so you need to know which tables cross that line. The database owner produces the inventory, and the platform lead decides which tables need manual index work.

Staging copy sized close to production

Build a staging database with a copy of production data at close to production size, and run the full migration there with the same settings you will use live. Measure how long the migration takes and whether the migration timeout is reached. The platform team owns the copy and the timing results, and the identity team checks that test logins work afterward.

Database version support window

Confirm that your database engine version is still supported through the planned life of the deployment. PostgreSQL 14 support ends November 12, 2026, according to the PostgreSQL versioning policy, so a database still on 14 may need a major upgrade before or alongside the Keycloak upgrade. The database owner produces the version report and the upgrade decision, and should confirm the Keycloak database guide lists that version as supported.

PostgreSQL versioning policy

Inventory of custom providers touching the database

List every custom SPI provider, theme extension or script that reads or writes Keycloak tables directly, including custom JPA entities and scheduled jobs. These components can break or block a schema change, and some need retesting against the new release. The development lead owns the list, and each entry names the table it touches and the test that proves it still works.

Maintenance window plan

Agree a maintenance window long enough for the schema migration, the backup and the verification steps, plus time to roll back. Decide whether the migration runs automatically at startup or manually with SQL applied by the database team, and document who approves the go and no-go decision. The change manager owns the window and the communication to application owners who depend on sign-in.

Why the backup must be restorable and the timeout must be tested

The Keycloak upgrading guide tells administrators to back up the database before upgrading, but a backup only protects the upgrade if the team has restored it into a separate environment and confirmed that Keycloak starts against the restored copy. A dump file that has never been restored is an assumption, not a recovery plan. Before the production upgrade, restore the backup to a staging database of the same major PostgreSQL version, point a Keycloak instance of the target release at it, and confirm that realms, clients, users and sessions load. Record who owns the restore step, how long the database team needs to perform it, and where the backup is stored so it can be reached during an incident.

The migration timeout matters most when tables are large, because the schema migration runs inside Keycloak's startup window and a long-running index build or table rewrite can exceed it. If the timeout expires, the upgrade fails partway through, and the team must work out which changes were applied before retrying. Do not set the timeout from a guess or from a small test realm. Run the upgrade in staging against a copy of production-scale data, time each startup, and compare the result with the migration setting your Keycloak version documents in the upgrading guide. Repeat the measurement after any data growth, since the duration can change substantially between quarters.

For PostgreSQL, point-in-time recovery and continuous archiving give the database team a way to return to a moment just before the upgrade, which a single nightly dump may not provide. Ask the database team whether WAL archiving is already configured, where archived segments are kept, and whether a recovery to a chosen timestamp has been tested. Agree on a recovery target and an owner for the rollback decision before the maintenance window opens. The exact recovery timing depends on your data volume and storage, so confirm it with your own measurements rather than with a figure from this page.

What makes the database migration take longer

This table lists the factors that lengthen a Keycloak schema migration on a large database, the effect each one has on the work, and what to confirm before scheduling the upgrade. Read it before picking a migration method, because several drivers only show up in the production database and not in a test copy.

DriverEffect on the workWhat to confirm
Table size and row counts above 300,000 entriesKeycloak skips creating some new indexes during automatic migration once a table passes the threshold, so the database can be slower to query after startup until those indexes are appliedRow counts for the largest Keycloak tables in production, taken from the database itself and not estimated
Number of skipped indexes and the manual SQL to applySkipped indexes are logged as SQL statements that someone must run after startup, which adds a post-upgrade step and a review of each statementWhether the team has a written list of logged statements, who applies them, and when they run in the change window
Migration timeout settingA large migration can exceed the configured timeout, which interrupts startup and leaves the team to decide between a longer timeout and manual migrationThe timeout value in the current configuration and the value the upgrade notes recommend for the target release
Automatic versus manual migrationAutomatic migration runs at startup and is simpler to operate, while manual migration moves the SQL review and execution into the change planWhether the upgrading guide's recommendation for large deployments applies to this environment, and who will own the SQL
Database engine and versionMigration statements, index behavior and locking differ by engine, so timing measured on one engine does not transfer to anotherThe exact engine and version in production, and whether it appears in the supported list for the target Keycloak release
Synchronous replication in high availability setupsBoth supported high availability designs need a synchronously replicated database, and schema changes are then written through that replication path, which affects how long writes takeHow the production database replicates, whether synchronous commit is enabled, and the replication lag during a test migration
Custom providers that read or write schemaCustom SPI providers or extensions that touch Keycloak tables may break or need changes when the schema moves, which can stall the migration or the first loginAn inventory of custom providers, the tables they access, and whether each was tested against the target release
Backup and restore speedA restorable backup must be taken before the upgrade, and a large database makes both the backup and a restore rehearsal take longerThe measured time for a full backup and a test restore of the production-sized database, and where the backup is stored
Number of environments that must be testedEach environment, such as development, test, staging and production, may hold different data volumes and needs its own migration run and timing checkHow many environments exist, whether staging holds a copy of production-sized data, and who signs off each run

Questions teams ask about large Keycloak database migrations

What drives the cost and effort of a large Keycloak database migration?

The main drivers are the number and size of the tables Keycloak must alter, the database engine you run, how much staging and rehearsal your team needs, and how much of the work your own database and platform staff can absorb. Cost splits into one-time work (inventory of table sizes, staging runs, backup and restore tests, the upgrade itself and index follow-up) and recurring work (patch cadence and quarterly minor releases). A written scope should name each of these, the environments covered and who holds the backup and rollback steps. Ask any partner to show how they will measure the migration in staging before they quote.

How long does a Keycloak database schema migration take?

Duration depends on table size, database engine, hardware and how the migration is run, so no general figure applies. Measure it yourself: copy a recent production backup into staging with matching hardware, run the upgrade under the same migration timeout you plan to use in production, and record how long each step takes. The Keycloak upgrading guide describes a migration timeout setting for large databases, so confirm its current name and default in the guide for your target version before you set a window. Use the staging result to schedule the production change.

Can our team run a large Keycloak database migration alone?

Often yes, if you have people who already operate Keycloak and the database engine, and you have time to rehearse. The work is less about the Keycloak command and more about knowing your table sizes, testing a restorable backup, deciding between automatic and manual migration, and applying any indexes Keycloak skipped. Teams without that experience usually benefit from a fixed-scope engagement that covers planning, a staging rehearsal and a written runbook. Confirm which steps your staff already perform before deciding.

What do we need from our database and platform staff?

Your database administrators need to provide access to a current backup, table size reports, maintenance windows and the ability to run DDL statements manually when you choose manual migration. Platform staff need to supply a staging environment that matches production, control of the Keycloak rollout, and monitoring so they can watch the migration in real time. Both groups should agree on who approves the change, who runs the rollback and who checks the skipped index statements after startup.

Is automatic schema migration safe for large tables?

Automatic migration is the default and is designed to run at startup, but it can be risky on large tables because the process may run longer than your window. The Keycloak upgrading guide also documents manual migration, which the guide suggests for large deployments, where administrators apply the SQL themselves and review it first. Choose the method after measuring table sizes in staging. Whichever path you pick, take and test a restorable database backup beforehand.

What happens to the indexes Keycloak skips on large tables?

When a table holds more than 300,000 entries, Keycloak skips creating some new indexes during automatic migration and logs the SQL needed to create them after startup. Until those indexes exist, queries against those tables can be slower under load, so the logged statements should be reviewed and applied in a planned window. Check the logs after every migration, record each statement, and apply it on staging first so you know how long index creation takes on your data.

How does a rollback work after a failed database migration?

The reliable rollback is a restore from the backup you took before the upgrade, so the backup must be verified before the change starts. Restoring means stopping Keycloak, restoring the database to the pre-upgrade state, and starting the previous Keycloak version against it. Rehearse this in staging, including the time the restore takes, and confirm that the old Keycloak version still starts against the restored schema. Keycloak does not offer an automatic downgrade of the database schema, so plan the restore path before you begin.

How DeployOpen scopes and delivers a Keycloak database migration

DeployOpen plans the database side of a Keycloak upgrade as a fixed-scope piece of work, starting from the table sizes and migration mode in your own environment. The work covers the migration choice, the timeout, the index statements to apply after startup, and a restorable backup, all delivered in your infrastructure.

How we scope

  • Measure row counts in the tables Keycloak migrates and identify which ones exceed the 300,000-entry index threshold.
  • Agree in writing whether automatic or manual schema migration fits your database, timeline and change windows.
  • Confirm the database engine, version and backup tooling, with the written scope listing deliverables before work starts.

How we build

  • Prepare a staging copy of production data and run the planned upgrade path against it first.
  • Set the migration timeout for your table sizes and capture the SQL Keycloak logs for indexes it skips.
  • Apply and verify the post-startup index statements, then test logins, sessions and admin functions on the upgraded instance.

How we hand over

  • Deliver a runbook covering backup, migration mode, timeout settings, index statements and rollback steps.
  • Document the restore test performed on the backup so your team can repeat it before each upgrade.
  • Walk your database and identity administrators through the procedure and record the verification results.

How we support

  • Offer ongoing support, upgrades and continuous testing as a retainer for later Keycloak minor releases.
  • Retest the migration procedure against each new Keycloak release and database minor version your team adopts.
  • Review table growth over time so future migrations are scoped against current row counts rather than past estimates.

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.