suchorski / springboot-keycloak-server

Embeded Keycloak on Spring Boot Server
Apache License 2.0
26 stars 12 forks source link

How to integrate with Postgres Database #10

Closed ffroliva closed 4 months ago

ffroliva commented 4 months ago

Hi Suchoski,

Your project is great and I believe it should be expanded a little. One thing I believe would make it more suitable for production is to allow switching to a different database, for example, Postgres.

I noticed that you have src/main/resourcesapplication.yml and src/main/resources/META-INF/keycloak-server.json with the same database configuration.

I simply added a new profile application-postgres with postgres datasource configuration but it was not enough to switch the database to a different provider.

Do you have an idea how to accomplish having a different database provider? Would it be sufficient to change the following section of keycloak-server.json:

...
    "connectionsJpa": {
        "default": {
            "url": "${keycloak.connectionsJpa.url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1}",
            "driver": "${keycloak.connectionsJpa.driver:org.h2.Driver}",
            "driverDialect": "${keycloak.connectionsJpa.driverDialect:}",
            "user": "${keycloak.connectionsJpa.user:sa}",
            "password": "${keycloak.connectionsJpa.password:}",
            "showSql": "${keycloak.connectionsJpa.showSql:}",
            "formatSql": "${keycloak.connectionsJpa.formatSql:}",
            "globalStatsInterval": "${keycloak.connectionsJpa.globalStatsInterval:}"
        }
    },
...

Your insight on this would be greatly appreciated.

PS: Bom trabalho! :)

suchorski commented 4 months ago

@ffroliva Hi. Thanks for your time.

I tried to change on one place too but with no success. You need to change on both application properties and keycloak-server.json to work

PS.: Valeu :) Brasileiro?

ffroliva commented 4 months ago

The ideia is to be able to dynamically change the database provider by enabling a profile in spring boot. I will try to work on that and let you know if that works on my end.

Ps: Sim, de Brasilia, mas more em Londres.

Em seg., 22 de abr. de 2024 às 14:22, Thiago Suchorski < @.***> escreveu:

@ffroliva https://github.com/ffroliva Hi. Thanks for your time.

I tried to change on one place too but with no success. You need to change on both application properties and keycloak-server.json to work

PS.: Valeu :) Brasileiro?

— Reply to this email directly, view it on GitHub https://github.com/suchorski/springboot-keycloak-server/issues/10#issuecomment-2069409567, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF67VG6ISYCMST6KKCSXHATY6UFIPAVCNFSM6AAAAABGSUYGN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRZGQYDSNJWG4 . You are receiving this because you were mentioned.Message ID: @.***>

ffroliva commented 4 months ago

I was able to enable postgres in the following way:

  1. Created the directory: config/keycloak-server/postgres/keycloak-server.json
  2. Added postgres driver to pom.xml
  3. Added spring-boot-docker-compose dependency to pom.xml
  4. Created the profile application-postgres.yml with postgres database configurations
  5. Created a docker-compose.yml with postgres databaset setup.
  6. When starting the application I started it with 2 extra JVM environment variables -Dspring.profiles.active=postgres -Djboss.server.config.dir=/springboot-keycloak-server/config/keycloak-server/postgres

With this I was able to start the application with postgres, however now I have 2 keycloak-server.json to manage, which is not ideal.

I think it is possible to remove the -Djboss.server.config.dir and rely on the one within the META-INF directory, however, I would probably need to add a lot more variables:

-Dkeycloak.connectionsJpa.url=jdbc:postgresql://localhost:5432/keycloak
-Dkeycloak.connectionsJpa.driver=org.postgresql.Driver
-Dkeycloak.connectionsJpa.driverDialect=org.hibernate.dialect.PostgreSQLDialect
-Dkeycloak.connectionsJpa.user=keycloak
-Dkeycloak.connectionsJpa.password=keycloak

Any insight on this?

suchorski commented 4 months ago

Since you are using docker, why not use something like this?

https://github.com/suchorski/keycloak-docker-compose

ffroliva commented 4 months ago

Thanks for sharing. Nice docker compose setup. I will give it a try. I though about having keycloak as spring boot application so that I could customize it as needed. Also by having it as spring application I can integrate it to my stack which also uses spring boot. For now I just need Postgres as my database which I was able to accomplish with the JVM arguments above.