Dhawal Mehta
Dhawal Mehta

Reputation: 73

Unable to use postgres with keycloak

I am trying to use postgres with keycloak. following Doc

$ ls keycloak-9.0.0/modules/system/layers/keycloak/org/postgresql/main
  config.xml  postgresql-42.2.10.jar

This is the config.xml file. config.xml

<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.3" name="org.postgresql">

    <resources>
        <resource-root path="postgresql-42.2.10.jar"/>
    </resources>

    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

These are the changes that i have made in standalone.xml standalone.xml

<datasources>

    <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:postgresql://localhost:5432/test</connection-url>
      <driver>postgresql</driver>
      <pool>
           <max-pool-size>20</max-pool-size>
      </pool>
      <security>
           <user-name>postgres</user-name>
           <password>StrongPassword</password>
      </security>
   </datasource>

    <drivers>
         <driver name="postgresql" module="org.postgresql">
              <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
          </driver>
     </drivers>


</datasources>

<default-bindings context-service="java:jboss/ee/concurrency/context/default" datasource="java:jboss/datasources/KeycloakDS">

The error i am getting. error

06:13:39,430 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 32) WFLYCTL0013: Operation ("add") failed - address: ([
    ("subsystem" => "datasources"),
    ("jdbc-driver" => "postgresql")
]) - failure description: "WFLYJCA0115: Module for driver [org.postgresql] or one of it dependencies is missing: [org.postgresql]"

How can i resolve this?

Upvotes: 7

Views: 11812

Answers (5)

uptoyou
uptoyou

Reputation: 1797

For those who came here from tutorials with Dockerized jboss/keycloak instance, you have built-in rdbms support without any configuration: https://hub.docker.com/r/jboss/keycloak/

docker run -p 8080:8080 -e KEYCLOAK_USER=kuser -e KEYCLOAK_PASSWORD=kpass -e DB_VENDOR=postgres -e DB_ADDR=host:port -e DB_DATABASE=postgres -e DB_USER=pguser -e DB_PASSWORD=pgpass jboss/keycloak:15.0.2

Upvotes: 0

Maxime GH
Maxime GH

Reputation: 615

This works for me in standalone:

  1. In file module.xml
 <module xmlns="urn:jboss:module:1.3" name="org.postgresql">

  <resources>
        <resource-root path="postgresql-42.2.23.jar" />
  </resources>

  <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
  </dependencies>
</module>

2.In file standalone.xml

    <datasources>
          
    <datasource jndi-name="java:jboss/datasources/KeycloakDS"
                     pool-name="KeycloakDS"
                     enabled="true"
                     use-java-context="true"
                     statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}" >
        
        <connection-url>jdbc:postgresql://192.168.1.XX:5432/Your-DataBase</connection-url>
             <driver>postgresql</driver>
        
             <pool>
                   <max-pool-size>20</max-pool-size>
             </pool>
             <security>
                   <user-name>UserName</user-name>
                   <password>**********</password>
             </security>
        <statement>
                 <prepared-statement-cache-size>32</prepared-statement-cache-size>
                  <share-prepared-statements>true</share-prepared-statements>
        </statement>
        </datasource>
        
        <drivers>
                 <driver name="postgresql" module="org.postgresql">
                  <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                 </driver>
        </drivers>
</datasources>

3.in file standalone.xml

    <default-bindings
        context-service="java:jboss/ee/concurrency/context/default"
        datasource="java:jboss/datasources/KeycloakDS"
        managed-executor-service="java:jboss/ee/concurrency/executor/default"
        managed-scheduled-executor-service="java:jboss/ee/concurrency/scheduler/default"
        managed-thread-factory="java:jboss/ee/concurrency/factory/default"/>

Upvotes: 0

edwil13x
edwil13x

Reputation: 1

This works for me.

  1. create PostgreSQL module.xml (not config.xml) inside base directory (keycloak-11.0.2/modules/system/layers/base/com/postgresql/)

  2. update standalone.xml

    <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true"
                statistics-enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
        <connection-url>jdbc:postgresql://localhost:5432/keycloak</connection-url>
        <driver>postgresql</driver>
        <security>
            <user-name>db-user</user-name>
            <password>db-pass</password>
        </security>
    </datasource>
    <drivers>
    <driver name="postgresql" module="com.postgresql.h2">
        <driver-class>org.postgresql.Driver</driver-class>
        <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
    </driver>
    <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
    </driver>
    </drivers>
    

Upvotes: 0

Edwin
Edwin

Reputation: 2278

I think your problem is that you named the settings config.xml and should be module.xml.

Second you have to download postgresql jar to that location: curl -O https://jdbc.postgresql.org/download/postgresql-X.X.X.jar > /opt/keycloak/modules/system/layers/keycloak/org/postgresql/main just replace the X.X.X with your version.

Then restart the server.

PS: you should use a vault for your password.

Upvotes: 1

stdunbar
stdunbar

Reputation: 17455

Can I suggest another way to configure this that is less likely to have an issue? I use the following to configure PostgreSQL and Keycloak and it has been working well. The key is to run this against a stopped Keycloak (using a fresh install). Save the below to something like setup-keycloak.cli:

embed-server --server-config=standalone.xml --std-out=echo

batch
#
# remove the default provided datasource
#
/subsystem=datasources/data-source=KeycloakDS/:remove

#
# add them
#
module add --name=org.postgres --resources=/path/to/postgresql-42.2.10.jar --dependencies=javax.api,javax.transaction.api
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)

/subsystem=datasources/data-source=KeycloakDS/:add(connection-url=jdbc:postgresql://localhost:5432/keycloak_database,driver-name=postgres,jndi-name=java:jboss/datasources/KeycloakDS,initial-pool-size=4,max-pool-size=64,min-pool-size=4,password=keycloak_user,user-name=keycloak_pass)

run-batch

Then run this with $KEYCLOAK_HOME/bin/jboss.sh --file=setup-keycloak.cli. This removes the KeycloakDS datasource, adds the PostgreSQL module, and recreates the KeycloakDS datasource with your parameters. You can use this to reproduce the configuration any time as long as you have a local copy of the PostgreSQL JDBC driver.

Upvotes: 8

Related Questions