# Keycloak Database creation - keycloak-database-update.sql

**URL:** <https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543>\
**Category:** Miscellanaeous\
**Tags:** admin-console, authentication, identity-brokering, gatekeeper, oidc\
**Created:** [October 23, 2020, 3:14am UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543 "2020-10-23T03:14:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssb](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@ssb](https://forum.keycloak.org/u/ssb)\
**Post date:** [October 23, 2020, 3:14am UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/1 "2020-10-23T03:14:40Z")

</div>

Hi,

I have created a database using the configuration file. It specifies the file name as **keycloak-database-update.sql**. **Is there a way I could get this file ?** It would be easier for me to just execute the script rather than providing a specific application user, the rights to create objects in the database.  
Other option I have is to manually generate the scripts for database creation. So I need the **keycloak-database-update.sql** file.

Any suggestions are really appreciated.

---

<div class="post-metadata">

**Author:** ![ssb](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@ssb](https://forum.keycloak.org/u/ssb)\
**Post date:** [November 9, 2020, 9:36pm UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/2 "2020-11-09T21:36:12Z")

</div>

For anyone, who might be looking for the same and wants to execute the script manually, this might be helpful so I decided to post the solution.  
In the **connectionsJpa** , set the below :

1. “ **initializeEmpty** ” to “ **false** ”
2. “ **migrationStrategy** ” to “ **manual** ”
3. “ **migrationExport** ” to the path you want the script to be generated.

---

<div class="post-metadata">

**Author:** ![dsaraiva](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/dsaraiva/32/1665_2.png) [@dsaraiva](https://forum.keycloak.org/u/dsaraiva)\
**Post date:** [November 13, 2020, 12:59pm UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/3 "2020-11-13T12:59:51Z")

</div>

Looking for exactly the same! Thank you for sharing.

Why is this not mentioned at all on the database upgrade information?

> **[Upgrading Guide](https://www.keycloak.org/docs/latest/upgrading/#manual-relational-database-migration)**

---

<div class="post-metadata">

**Author:** ![ssb](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@ssb](https://forum.keycloak.org/u/ssb)\
**Post date:** [November 15, 2020, 9:08pm UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/4 "2020-11-15T21:08:47Z")

</div>

Well, it is not mentioned in any document but I think it is something that needs to be included. I had to try out a few things and even go through some code as I was unable to generate the sql file initially. I had to spend some time on it but I am glad the solution works for you.

---

<div class="post-metadata">

**Author:** ![davidgs](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/davidgs/32/3455_2.png) [@davidgs](https://forum.keycloak.org/u/davidgs)\
**Post date:** [June 30, 2021, 1:08pm UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/5 "2021-06-30T13:08:17Z")

</div>

Sadly, I’ve been struggling through the same issue with no resolution in site. Thanks to your excellent answer, I at least have the `keycloak-database-update.sql` file being generated now, but when I try to run it against MySQL, I get the following error:

```auto
SQL query:

ALTER TABLE keycloak.USER_ENTITY ADD CONSTRAINT UK_RU8TT6T700S9V50BU18WS5HA6 UNIQUE (REALM_ID, USERNAME)
MySQL said: Documentation

#1071 - Specified key was too long; max key length is 1000 bytes

```

And I have absolutely NO idea how to fix that one. If anyone has seen this, and found a solution, please do let me know!

Best Regards,  
dg

---

<div class="post-metadata">

**Author:** ![davidgs](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/davidgs/32/3455_2.png) [@davidgs](https://forum.keycloak.org/u/davidgs)\
**Post date:** [June 30, 2021, 1:26pm UTC](https://forum.keycloak.org/t/keycloak-database-creation-keycloak-database-update-sql/5543/6 "2021-06-30T13:26:11Z")

</div>

If I change from `initializeEmpt=false` to `true` then a database initialization is attempted, but it fails with:

```auto
09:23:52,548 INFO [org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider] (ServerService Thread Pool -- 57) Updating database.
 Using changelog META-INF/jpa-changelog-master.xml
09:23:52,809 ERROR [org.keycloak.connections.jpa.updater.liquibase.conn.DefaultLiquibaseConnectionProvider] (ServerService Thread Pool -- 57) Chang
e Set META-INF/jpa-changelog-2.5.0.xml::2.5.0-unicode-other-dbs::hmlnarik@redhat.com failed. Error: Can't DROP 'UK_RU8TT6T700S9V50BU18WS5HA6'; check that
 column/key exists [Failed SQL: ALTER TABLE keycloak.USER_ENTITY DROP KEY UK_RU8TT6T700S9V50BU18WS5HA6]
09:23:52,809 ERROR [org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider] (ServerService Thread Pool -- 57) Error has occurre
d while updating the database: liquibase.exception.MigrationFailedException: Migration failed for change set META-INF/jpa-changelog-2.5.0.xml::2.5.0-unico
de-other-dbs::hmlnarik@redhat.com:
     Reason: liquibase.exception.DatabaseException: Can't DROP 'UK_RU8TT6T700S9V50BU18WS5HA6'; check that column/key exists [Failed SQL: ALTER TABLE keycl
oak.USER_ENTITY DROP KEY UK_RU8TT6T700S9V50BU18WS5HA6]

```

I really am at a loss here.
