Getting users into Keycloak
Sooner or later every Keycloak project has to load users that already exist somewhere else: a spreadsheet from HR, an export from an old application, a list of customers from a previous login system. Keycloak has no CSV import. It reads users as JSON, in the same shape it uses in realm exports.
This tool turns a CSV with a header row into that JSON. You can load it with the admin console’s partial import or put it in a realm import file, without writing a script.
The ways Keycloak can load users
| Method | Good for | Notes |
|---|---|---|
| Partial import in the admin console | Adding users to a realm that already exists | Asks what to do when a user exists: fail, skip or overwrite. |
Realm import with --import-realm | Creating a new realm with its users | Skips a realm that already exists. |
| Admin REST API | Ongoing sync from another system | One request per user; needs a script. |
| User federation (LDAP, Active Directory) | Users who stay in a directory | No import file; users are read from the directory. |
For a one-time load of a few hundred or a few thousand users, partial import is usually the simplest. For users who should keep living in a directory, federation is the better fit, and the LDAP filter checker helps choose which of them Keycloak sees.
The user JSON Keycloak reads
Each user is an object with a username and optional fields. Groups are written as paths and realm roles by name.
{
"users": [
{
"username": "alice",
"firstName": "Alice",
"lastName": "Sample",
"enabled": true,
"emailVerified": false,
"requiredActions": ["UPDATE_PASSWORD"],
"groups": ["/staff"],
"realmRoles": ["user", "editor"],
"attributes": { "department": ["Sales"] }
}
]
}Attribute values are always lists of strings, even when there is only one value. Group paths start with a slash and name every parent group, such as /staff/support.
What this tool does
It reads CSV separated by commas or semicolons, with quoted fields as spreadsheets write them. The header names the columns, in any order and in any letter case.
| Column | Becomes |
|---|---|
| username | username, in lower case |
email; also the username when there is no username column | |
| firstName, lastName | firstName, lastName |
| enabled | enabled (true, yes, 1 or y); true when empty |
| emailVerified | emailVerified; otherwise the option you choose |
| password, temporary | A password credential; temporary unless the column says otherwise |
| groups | Group paths, several separated by the cell separator |
| realmRoles | Realm role names, several separated by the cell separator |
| Any other column | A user attribute, if you keep that option on |
It warns about rows with no username or email, rows with a different number of fields than the header, duplicate usernames and email addresses that do not look valid. It can add the Update Password required action to every user without a password, and it writes either a partial import file or a realm import file.
Preparing the CSV
Spreadsheet programs offer several CSV formats. Choose CSV with UTF-8 encoding, so names with accents or other scripts arrive intact. Some locales write semicolons instead of commas; the tool detects that from the header row. Remove empty rows at the end, and avoid merged cells or formulas in the export.
If one cell needs several groups or roles, separate them with a character that never appears in the names, such as a semicolon or a pipe, and set the same character in the tool.
What it does not do
It does not create groups or roles. They must exist in the realm, or be part of the same import, before users can join them. It does not set client roles, identity provider links or credentials other than plain passwords. It does not hash passwords; for that, see the next section. It does not send anything to Keycloak: you save the file and import it yourself.
Passwords
You have three choices, from safest to riskiest.
No password, with Update Password
Leave the password column out and keep the Update Password option on. Then send each user a link with the admin API’s execute-actions email, or let them use Forgot password. Nobody ever sees a password. This needs the realm’s email settings to work.
Existing hashes
If the old system stored PBKDF2 hashes, you can import them as credentials and users keep their passwords. The password hash generator shows the credential format Keycloak expects. Other algorithms, such as bcrypt, need a custom provider in Keycloak or a password reset.
Plain passwords in the CSV
The tool turns a password column into a credential with a plain value, marked temporary unless you say otherwise. Keycloak hashes it on import, but the CSV and the JSON hold it in plain text until you delete them. Use this only for test realms or one-time passwords people must change.
How to use it
- Create the groups and realm roles your users need, if they do not exist yet.
- Export the users from the old system as CSV with a header row. Rename columns to the names above, or keep extra columns as attributes.
- Paste the CSV. A few made-up users are filled in to show the format.
- Choose the cell separator for groups and roles, whether users must set a password, and whether emails are verified.
- Read the warnings and fix the CSV until none are left.
- Pick partial import or realm import, and save the file.
- In the admin console, open Realm settings, Action, Partial import, choose the file and choose what happens to existing users.
Real-world uses
Moving from another login system
When an application moves its sign-in to Keycloak, its users move too. Export them with their groups, import them with Update Password, and send reset links. If the old system used PBKDF2, import the hashes instead so nobody has to reset.
Seeding a test or staging realm
A CSV of test users with known roles makes repeatable test setups. Keep it in the repository and rebuild the realm from it whenever you need a fresh copy.
Onboarding a customer or a team
Business customers often send a spreadsheet of the people who need access. Convert it, check the warnings, import, and email the users their set-password link.
Common problems
| Problem | Likely cause | Fix |
|---|---|---|
| Users imported without their groups | The groups did not exist, or the path is wrong | Create the groups first and use full paths. |
| Attributes missing after import | The user profile does not know them | Add them to the realm’s user profile, or allow unmanaged attributes. |
| Import stops at a user | The user exists and the policy is fail | Choose skip or overwrite in the partial import. |
| Two users with one email | The realm does not allow duplicate emails | Clean the CSV; the warnings show duplicate usernames. |
| Reset emails never arrive | No SMTP settings in the realm | Set up email under Realm settings, Email. |
Security and privacy
The CSV and the JSON hold personal data, and maybe passwords. This page converts them in your browser and sends nothing to us. Keep the files out of shared folders and tickets, and delete them after the import. Prefer no passwords at all, so the files hold nothing that grants access.
Best practices
- Import into a staging realm first and sign in as a few of the users.
- Use lower-case usernames; the tool lowercases them for you.
- Mark emails verified only when the old system really verified them.
- Declare custom attributes in the user profile before the import.
- Load large numbers of users in batches, so one bad row does not hide the rest.
- For a move to managed hosting, plan users together with the rest of the realm; our migration page describes how that works.