# How to import users on startup in Keycloak 19

**URL:** <https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928>\
**Category:** Getting advice\
**Created:** [December 12, 2022, 3:23pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928 "2022-12-12T15:23:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gatschet](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/gatschet/32/4502_2.png) [@gatschet](https://forum.keycloak.org/u/gatschet)\
**Post date:** [December 12, 2022, 3:23pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/1 "2022-12-12T15:23:32Z")

</div>

How can we imort users in Keycloak 19 on startup.

The command  
/opt/keycloak/bin/kc.sh -v start-dev  
–import-realm  
is only to import reamls on startup, but not for users.

I would create some default users on startup. Is this only possible with the import command “/opt/keycloak/bin/kc.sh import --dir /opt/keycloak/data/import/ --override true” ?

I would prefer startup and create users in one step, how i can do wiht the --import-realm option.

---

<div class="post-metadata">

**Author:** ![dasniko](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/dasniko/32/2969_2.png) [@dasniko](https://forum.keycloak.org/u/dasniko)\
**Post date:** [December 12, 2022, 7:52pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/2 "2022-12-12T19:52:12Z")

</div>

It’s possible with the `--import-realm` flag.  
If there are users in the realm file, they will be imported.

---

<div class="post-metadata">

**Author:** ![gatschet](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/gatschet/32/4502_2.png) [@gatschet](https://forum.keycloak.org/u/gatschet)\
**Post date:** [December 14, 2022, 12:33pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/3 "2022-12-14T12:33:38Z")

</div>

ok, thank you for the hint.  
the problem ist, it’s not possible to export the reamls and user to a file and then inport the file.  
If i do that, there are more than one realm in the file (master and mine) and kc throws a exeption while importing a file with more than one realm!

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.keycloak.representations.idm.RealmRepresentation` from Array value (token `JsonToken.START_ARRAY`)

the only way was to export the realms and users to different files and the merge the realm and users togehter…

---

<div class="post-metadata">

**Author:** ![dasniko](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/dasniko/32/2969_2.png) [@dasniko](https://forum.keycloak.org/u/dasniko)\
**Post date:** [December 14, 2022, 12:37pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/4 "2022-12-14T12:37:54Z")

</div>

Of course it’s possible to export only one specific realm, just read the docs properly: [Importing and Exporting Realms - Keycloak](https://www.keycloak.org/server/importExport#_exporting_a_specific_realm)  
(I do this regulary for some test- & dev-environments)

---

<div class="post-metadata">

**Author:** ![gatschet](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/gatschet/32/4502_2.png) [@gatschet](https://forum.keycloak.org/u/gatschet)\
**Post date:** [December 20, 2022, 1:16pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/5 "2022-12-20T13:16:50Z")

</div>

After spent many hours to do that, it was not possible to do that on startup. There was a very strange behavior on Keycloak. After i imported the same configuration before start, then it was working! I think there are some bugs on import configuration on startup time.

---

<div class="post-metadata">

**Author:** ![egml](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/egml/32/8256_2.png) [@egml](https://forum.keycloak.org/u/egml)\
**Post date:** [April 13, 2023, 6:43am UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/6 "2023-04-13T06:43:20Z")

</div>

In my case, I forgot to rebuild Keycloak image for the container. So that updated `realm.json` file was not copied to `/opt/keycloak/data/import/` and script was still using the old file. After rebuild `--import-realm` flag did it’s job and all data was imported, including users. Maybe it will help.

---

<div class="post-metadata">

**Author:** ![pikosss92](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/pikosss92/32/9392_2.png) [@pikosss92](https://forum.keycloak.org/u/pikosss92)\
**Post date:** [December 6, 2023, 1:50pm UTC](https://forum.keycloak.org/t/how-to-import-users-on-startup-in-keycloak-19/18928/7 "2023-12-06T13:50:39Z")

</div>

It is possible to export a realm including the users in the same realm.  
Make sure that you have such template in your realm.json

“users”: [  
{  
“id”: “service-account-alfresco”,  
“serviceAccountClientId”: “alfresco”,  
“username”: “service-account-alfresco”,  
“enabled”: true,  
“email”: “[service-account-alfresco@muster.com](mailto:service-account-alfresco@muster.com)”,  
“realmRoles”: [  
“offline\_access”,  
“uma\_authorization”  
],  
“clientRoles”: {  
“account”: [  
“view-profile”,  
“manage-account”  
],  
“realm-management”: [  
“query-groups”,  
“query-users”,  
“view-users”,  
“view-clients”,  
“view-realm”  
]  
}  
},  
{  
“id”: “mmustermann”,  
“username”: “mmustermann”,  
“enabled”: true,  
“email”: “[max.mustermann@muster.com](mailto:max.mustermann@muster.com)”,  
“firstName”: “Max”,  
“lastName”: “Mustermann”,  
“credentials”: [  
{  
“type”: “password”,  
“value”: “mmustermann”  
}  
],  
“realmRoles”: [  
“default-roles-test”  
],  
“groups”: [  
“/Test A/Test AB”,  
“/Test B/Test BA”  
]  
}  
]

* * *

user:mmustermann  
password: mmustermann
