Keycloak realm import and export for environment promotion: when teams need it
Teams need realm export and import when a Keycloak realm was configured by hand in development and must now exist in test and production without being rebuilt through the admin console. Export captures the realm's clients, roles, groups, identity providers and authentication flows as a file, and import loads that file into another Keycloak. The promotion gap is what matters here: a realm that works in development is only useful downstream if its configuration can be copied, reviewed and repeated, and manual rework in each environment is where drift starts. Export and import are the right tool for a one-time copy or a repeatable baseline, while the exact contents, secrets handling and behavior on running nodes are covered in the sections that follow.
Realm export and import sit alongside three other ways to manage Keycloak configuration. The Terraform provider is the usual choice when a team wants every change in version control as a reviewed plan, with state and drift detection. The Admin REST API and kcadm suit scripted, incremental changes to users, clients and roles. The Keycloak Operator's KeycloakRealmImport resource brings the same import idea into Kubernetes, where a realm file is applied as a custom resource. Export and import is the simplest of these to start with, but it is a snapshot tool rather than a change-management tool, so teams should decide early whether they will keep the exported files as the source of truth or use them only to seed an environment.
Before choosing this route, confirm three things with your Keycloak version and deployment model. Check the official export and import documentation for the release you run, because the supported flags and file formats are versioned. Decide which values will differ between environments, such as hostnames, redirect URIs and client secrets, and plan how each will be supplied. Finally, confirm that your environments run the same Keycloak version, since promoting a realm file across mismatched releases adds a compatibility question on top of the configuration work. Ask any implementation partner to write these decisions into the scope before work begins.
Which promotion method fits which environment
This table compares the three ways to move Keycloak realm configuration between environments: the kc.sh export and import commands, startup import from a directory, and the Operator's KeycloakRealmImport resource. Read each row to match a method to where your Keycloak runs and what you need to move. Terraform is covered on a sibling topic, so it is not compared here.
| Method | Where it runs | What it targets and when it fits |
|---|---|---|
| kc.sh export and import | Command line on a host or container where the Keycloak distribution is installed | Moves realm data as files between environments on VMs, containers or Windows. Fits one-off copies or a scripted promotion that an operator runs by hand, with a review of the output before it reaches production. |
| Startup import from a directory | Keycloak process at startup, reading a mounted or local import directory | Loads realm JSON files when the server starts. Fits non-Kubernetes deployments that rebuild from files, but it adds realms at startup and does not replace an existing realm's full state, so confirm the behavior for your version. |
| KeycloakRealmImport resource | Kubernetes or OpenShift cluster running Keycloak through the Keycloak Operator | Applies a realm definition as a custom resource managed by the Operator. Fits clusters where realm files are kept in Git and applied through the same pipeline as other manifests. It applies only to Kubernetes-hosted Keycloak. |
| Terraform provider | Pipeline or workstation that calls the Keycloak Admin API | Covered on a sibling topic. Fits teams that want realm changes tracked as code with plan and drift review across environments. |
| Exported files containing secrets | Any environment where exported JSON is stored or transferred | Exports can omit or mask secret values, so confirm what your version includes. Plan how client secrets, SMTP credentials and key material are supplied separately in each target environment. |
| Running nodes during import | Production clusters with more than one Keycloak node | Confirm with the Keycloak upgrade and clustering documentation how changes reach running nodes, and schedule imports with that behavior in mind rather than assuming instant consistency. |
What a realm export includes and what it leaves out
A realm export carries realm configuration, not the full state of a running identity system. Clients, client scopes, roles, groups, identity provider definitions, authentication flows, required actions and realm settings such as token lifetimes and password policies generally travel in the JSON file that kc.sh export produces. That makes the export the right vehicle for promoting the shape of a realm from development to test to production. Before you rely on it, confirm for your exact Keycloak version which object types the export covers and which it skips, because coverage has changed between releases and the server administration guide is the authoritative list.
Several things deliberately fall outside the file or need separate handling. User accounts, password hashes, TOTP secrets and credentials registered by users, such as passkeys, are not part of a configuration promotion, and you should confirm how your version handles them if you export with users included. Client secrets and keys are the sensitive items: check whether the export writes them in clear text or masks them, and plan how each target environment receives its own client secrets, realm signing keys and certificates. Credentials for upstream providers, such as the client secret for an external OIDC or SAML identity provider or an LDAP bind password, also need to be supplied per environment through a secret store rather than copied from the source realm.
Treat the export file as a change artifact. Review it before import, search it for secret values, and decide where it is stored, since an export that contains secrets becomes a sensitive asset. Build a short checklist from the server administration guide for your version, then test one realm end to end in a non-production environment and compare the imported result against the source with a diff. Anything the checklist marks as environment-specific should be documented as a manual step or managed through configuration tooling, so the next promotion does not depend on someone remembering it.
How to export from development and import into test
This sequence moves a realm's configuration from a development node into a test environment in a repeatable way, with a check after each stage so that drift and missing settings surface before production is involved.
Stop or quiesce the development node
Before exporting, stop the development Keycloak node or confirm that no administrator is changing the realm. The export reads the realm state at that moment, so in-flight edits made during the export can produce an inconsistent file. Whoever owns the development instance makes this call and records the time and Keycloak version used. The step is done when no one has changed the realm since the agreed point and the export is scheduled against that snapshot. Confirm the exact export options for your Keycloak version in the server administration guide before running the command.
Run the export with kc.sh
Run kc.sh export against the development database with the realm and output directory options your version supports, and export the realm data to a directory on the administrator's workstation or a controlled build host. Check the command help for your release to confirm whether users, client secrets and other sensitive values are included by default. The step is complete when the command exits without errors and the expected realm files exist in the output location. Record the Keycloak version alongside the files so the import can be matched to it later.
Review and scrub the exported files
Open the exported JSON and review it for content that should not enter version control, including client secrets, user credentials, SMTP passwords, identity provider client secrets and LDAP bind credentials. Replace each sensitive value with a placeholder or a reference to an external secret store, and confirm that users are excluded unless the promotion specifically requires them. The step is complete when a second reviewer has checked the file and a search for known secret fields returns no literal values. Keep the unredacted export out of shared locations.
Commit the sanitized realm to version control
Store the scrubbed realm file in a Git repository with the Keycloak version, export date and the placeholder list documented in the same change. Use a pull request so the diff is reviewed like any other configuration change, and tag the commit that the test promotion will use. The step is done when the repository holds the sanitized file, the tag exists and the diff shows only realm content, not secrets. This commit becomes the record of what was promoted.
Apply environment-specific values to the test realm file
Substitute test-specific values for hostnames, redirect URIs, identity provider endpoints, client secrets and mail settings, using a template or a deployment pipeline rather than editing a copy by hand. Keep the environment values in the test environment's secret store or variables. The step is complete when a rendered test file exists, it contains no production hostnames, and every placeholder from the previous step has a documented source. Confirm which settings your Keycloak version reads from the realm file and which must be supplied at startup.
Import the realm into test
Import the rendered file into the test environment either with kc.sh import on a stopped test node or through startup import, depending on how the test instance is run. Check the import options for your version, especially whether an existing realm with the same name is overwritten or skipped. The step is complete when the command finishes without errors and the server log shows the realm was created or updated as expected. Note the import method used so the next promotion can repeat it exactly.
Verify the imported configuration
Compare the imported realm with the development source by listing clients, roles, identity providers, authentication flows and required actions through the admin console or the Admin REST API, and diff those lists against the exported file. Then perform a login test with a test user for each client that uses the realm. The step is done when the lists match, every expected difference is explained by an environment value, and each login succeeds. Record failures as defects against the promotion before any further change.
Repeat the sequence for each promotion
Keep the export, scrub, commit, render and import steps in a documented runbook so that every promotion, including the move to production, follows the same path with the same checks. Store the verification results with the promotion record. The step is complete when the runbook names the owner of each stage and a successful test promotion has been recorded. Changes made directly in a non-development environment should be exported back into version control so the repository stays the source of truth.
Handling secrets and per-environment overrides
The import file describes the realm, so it is the wrong place for credentials. Confidential client secrets, SMTP passwords and client secrets for upstream identity providers should be supplied to each environment from a secret store, an orchestrator secret or the deployment tool's vault integration. Before you rely on an export, check how each secret field appears in it, because a masked or placeholder value must never be imported as if it were the real credential.
- Client secrets: Set confidential client secrets after import or through your secret manager, and rotate them as part of the promotion runbook.
- SMTP credentials: Keep the SMTP username and password out of the realm file and inject them on each environment as environment variables or mounted secrets.
- Identity provider secrets: Upstream client secrets for brokered providers need the same handling. Confirm whether your export stores them masked or omits them.
- Git hygiene: Do not commit exports that contain user data or secret material. Add a scan step before any realm file reaches version control.
- Runtime injection: Reference secrets from the Operator resource or the platform's secret objects so the realm file stays identical across environments.
Check that a fresh import into test produces working logins without any secret copied by hand from a developer's workstation.
Redirect URIs, web origins, the public hostname and internal service URLs differ between development, test and production, yet they are stored inside client and realm settings that the export captures. Decide which values are environment-specific before the first export, and keep them out of the shared baseline. Hostname behavior is driven by Keycloak's hostname configuration, so the values Keycloak publishes need to be verified after every import rather than assumed from the source environment.
- Redirect URIs and web origins: Test clients often carry wildcard or localhost entries copied from development. Replace them with the exact URLs for each environment.
- Frontend hostname: The public URL that users and relying applications see is set per environment. Confirm the issuer in the discovery document after import.
- Internal URLs: Backchannel endpoints for services inside your network can differ from public ones. Record both per environment.
- Brokered provider endpoints: Callback and metadata URLs registered at each upstream provider must match the hostname of the environment that uses them.
- Email and branding: Sender addresses, reply-to addresses and links in templates may still point to a development domain.
Compare the issuer, redirect configuration and email settings with a per-environment checklist before any user signs in.
Overrides are applied outside the realm file, either through server configuration on each node or through the fields of the Operator resource, so one baseline file can serve every environment. Startup import of a realm file is intended for a realm that does not yet exist, so the way overrides reach an existing realm is a separate question. Confirm that behavior for your Keycloak version in the Keycloak Operator installation guide and the upgrade notes before you depend on it.
- Server configuration: Set hostname and database values through configuration options or environment variables on each node, not by editing the realm file.
- Startup import behavior: Plan a distinct, documented step for updating an existing realm, and test it in a non-production environment first.
- KeycloakRealmImport resource: Keep one resource manifest per environment that references secrets and environment values, while the realm JSON stays shared.
- Overlay directories: Maintain one Kustomize or Helm overlay per environment so environment values are versioned without forking the realm file.
- Node consistency: Every running node must read the same effective configuration. A node with different overrides can issue tokens with a different issuer.
After each rollout, check the effective configuration on every node through the discovery document and a test login.
How a realm moves from development to production
The flow starts with a realm configured in development and exported to a file kept in version control. Before import, secret values and environment-specific settings are substituted outside the file, so the exported artifact carries no live credentials. The same reviewed file is then imported into test and, after a review gate, into production through the method your team chose. Check that the trust boundary holds: secrets should enter only at import time, and every environment should receive the same reviewed configuration.
Realm configuration moves through a review gate, with secrets injected only at import.
What happens to running nodes during an import
Importing a realm is not a neutral file copy when Keycloak nodes are already serving traffic. The cards below cover the risks that connect the import itself to the state of running nodes, the cache and session layer, and the version of Keycloak on each side. Confirm each point against the Keycloak version and high availability design of your own deployment before you schedule a promotion.
Importing into a live cluster
A startup import and a runtime import behave differently. Startup import is designed for nodes that have not yet begun serving requests, so running a realm import against a cluster that is already handling logins can leave some nodes with the old configuration and others with the new one. Check how your Keycloak version and deployment method handle import, and whether a planned maintenance window is needed before any node receives the change.
Keycloak docsOverwriting existing realm state
An import that uses an existing realm name can replace clients, roles, identity providers and flows that someone changed directly in the target environment. Users, sessions and credentials may or may not be affected depending on what the export contained. Confirm what the import overwrites by testing against a copy of production before the real promotion, and keep a fresh export of the target realm as a rollback point.
Cache and session effects
Keycloak keeps realm and client data in caches and holds user sessions in distributed cache entries. When configuration changes underneath those caches, nodes can keep serving stale client settings or session data until caches are invalidated or the node restarts. Ask how your version invalidates cached realm data after an import and whether existing sessions survive a change to a client's redirect URIs or signing keys.
Export and target version mismatches
A realm exported from one Keycloak minor version may contain fields or defaults that a different version handles differently. Upgrades can also change database schema and default behavior, so an export taken before a target upgrade may need review. Confirm that source and target run the same release, and read the upgrade notes for every version between them before importing into a newer target.
Upgrading guideRestarting or rolling nodes after import
Some configuration is read only when a node starts, so an import may leave running nodes out of step until they restart. In a cluster, plan a rolling restart so that at least some nodes stay available during the change, and verify each node after it rejoins. Confirm with your deployment method whether a rolling restart is required for the imported settings, and test the sequence in a non-production cluster that matches your topology.
High availability overviewQuestions teams ask before promoting Keycloak realms
Do users move with the realm when I export and import it?
Usually not in the same way as realm configuration. A realm export covers the realm's configuration, such as clients, roles, client scopes, authentication flows and identity provider settings, and can optionally include users. Teams should confirm in their own Keycloak version whether users, credentials and sessions are part of the export they run, because user data often has different privacy and retention rules than configuration. Plan a separate, controlled path for user records in each environment, and never assume production users should be copied into development or test.
How much effort does a first realm promotion take, and what drives it?
The effort depends less on the export command and more on how many environment-specific values the realm contains. The main drivers are the number of clients and their redirect URIs, the count of identity providers with their own secrets, custom authentication flows, custom themes or SPI providers that must exist in every environment, and whether the team has agreed which environment owns each setting. Ask any partner to quote the work from a written inventory of the realm, the target hostnames and the promotion path, because those inputs set the scope.
Can an internal team promote realms without outside help?
Yes, a capable platform team can do it, provided it has someone who knows the Keycloak admin model and the deployment on each side. The work is mostly disciplined process: exporting in a repeatable way, reviewing the output, removing or externalizing secrets, applying environment overrides and verifying the result. Teams usually find the first promotion takes longer than later ones, and the risk is inconsistent manual steps across environments. Outside help is most useful for designing the workflow once, setting up verification, or documenting the procedure so operators can repeat it.
How should secrets be handled when realms move between environments?
Secrets should never travel inside a committed realm file. Client secrets, identity provider client secrets, SMTP credentials and keystore passwords differ per environment and must come from the target environment's secret store or from variables supplied at import time. Teams should check exported files for secret values before committing them, rotate any secret that was exposed, and confirm how the Keycloak version they run handles masked or omitted values. Access to the export files and the secret store should be limited to the people who run promotions.
What is the difference between startup import and the Operator's realm import resource?
Both load realm JSON into Keycloak, but they fit different deployment models. Startup import is a server-side option for a Keycloak instance you start yourself, while the Operator's KeycloakRealmImport resource applies a realm to a Keycloak custom resource on Kubernetes or OpenShift. Teams should confirm in the Keycloak Operator documentation how each handles existing realms, overwrites and secrets. Importing into a running production node without a plan can change live configuration, so promotion should be tested in a non-production environment first.
What should I ask an outside partner to include in the engagement?
Ask for a written scope that names the environments, the Keycloak version and deployment method on each side, and the realm objects that will move. The partner should describe how it will separate configuration from user data, how environment overrides and secrets will be handled, how it will verify a promotion, and what happens when a node is running during import. Also ask who owns the repository of realm files after handover, what documentation the team receives, and how upgrades to a newer Keycloak release will be handled, since promotion steps can change between versions.
Does promoting a realm affect running Keycloak nodes?
It can, so the timing and procedure matter. Importing configuration into a live cluster changes what running nodes serve, and teams should plan for active sessions, cached data and any clients that depend on changed settings. Confirm how your Keycloak version and deployment handle imports while nodes are running, and schedule production changes in a window with a rollback plan. Verify login, token issuance and key client flows in the target environment before and after the change.
Choosing between kc.sh, startup import and the Operator
The first decision is where Keycloak runs and who is allowed to change it. A team that starts Keycloak from kc.sh on a virtual machine or in a container usually has the most direct options: export a realm with kc.sh export, then either run an import at startup or use the admin tooling against the running server. A team on Kubernetes or OpenShift that installs Keycloak through the Operator should move realm changes into KeycloakRealmImport resources, because the Operator owns the lifecycle of the Keycloak custom resource and a hand-run import sits outside that record. Ask which model your platform team already operates before choosing, since mixing the two on one environment makes it hard to say which path last changed a realm.
Change control matters as much as hosting. If realm definitions already live in Git and are applied by a pipeline, the import path should be the one that pipeline calls, and the exported JSON should be treated as a reviewed artifact rather than a backup. If configuration is not kept as code, startup import is the simplest route for a small number of realms, but it runs when the server starts, so the team needs to decide who restarts nodes and when. Confirm the exact import behavior for your Keycloak version in the Keycloak server administration guide before writing a promotion procedure around it.
For the Operator, the recommended installation path is Operator Lifecycle Manager, and the Operator documentation strongly recommends manual approval mode for operator updates to avoid unintended automatic upgrades. Confirm the approval mode on your cluster before you rely on realm import resources in any promotion flow, because an automatic operator upgrade can change how Keycloak and its custom resources behave between environments. Record the Operator and Keycloak versions in each environment so that a realm exported from one version is imported only into a compatible target.
How DeployOpen scopes and delivers Keycloak realm promotion
DeployOpen plans, implements, documents and supports self-hosted open-source platforms inside the client's own infrastructure, including Keycloak. For realm import and export, the work starts with a written scope that defines which realm configuration moves between development, test and production, how secrets are handled, and how each environment is verified before a change reaches production.
How we scope
- Inventory the realms, clients, roles, identity providers and flows that must move between environments, and separate them from environment-specific data.
- Agree in writing which secrets, client credentials and user data are excluded from exports and how they are supplied at each environment.
- Confirm the Keycloak version, deployment method and whether each environment runs the Operator, containers or virtual machines before work starts.
How we build
- Set up export and import procedures using kc.sh and the Operator's KeycloakRealmImport resource, with overrides defined per environment.
- Build the promotion sequence so realm changes are applied in a controlled order, with the impact on running nodes and active sessions reviewed first.
- Test each promotion path in a non-production environment, checking that clients, redirect URIs and identity provider settings resolve correctly after import.
How we hand over
- Deliver written runbooks covering export, import, rollback, secret injection and the checks to run after each promotion.
- Provide the configuration files and resource definitions in a form your team can store under version control and review.
- Walk your operators through the procedure and record the decisions made during scoping, so later changes follow the same rules.
How we support
- Engagements are fixed-scope, with scope, deliverables and timelines agreed in writing before work starts.
- Ongoing support, upgrades and continuous testing are available as retainers for promotion procedures that change with each Keycloak release.
- Ask for a quote based on the written scope, which sets out the components, who performs each task and what your team must provide.

