Bertuz
Bertuz

Reputation: 2576

how to configure micronaut to use postgtres in place of mysql

I'm new to micronaut and I'm following this official guide which works fine: when running ./gradlew run (or test) a mysql container runs and the framework automatically connects to it. Starting from the original basecode I've tried to use postgresql instead.

By changing:

jpa:
  default:
    entity-scan:
      packages:
        - 'example.micronaut.domain' 
    properties:
      hibernate:
        show-sql: true
        hbm2ddl:
          auto: update 
        connection:
          db-type: postgres # here


...


test-resources:
  containers:
    postgres:
      image-name: postgres:latest

but running it raises an error:

01:24:04.625 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.boot.SessionFactoryBuilder]

Message: Could not resolve placeholder ${auto.test.resources.jpa.default.properties.hibernate.connection.url}

I guess this does not apply to postgres?

When the application is started locally — either under test or by running the application — resolution of the datasource URL is detected and the Test Resources service will start a local MySQL docker container, and inject the properties required to use this as the datasource.

According to the modules test resources it should be everything fine, but I guess I should put the connection URL myself on the yaml? What else is it missing or wrong? Will the container raise up with the one I've placed?


Update

After trying to see what was needed to be changed on the build.gradle the container nows runs when ./gradlew run is executed. But the following error is thrown:

16:06:32.826 [vert.x-eventloop-thread-0] ERROR org.hibernate.reactive.errors - HR000057: Failed to execute statement [select * from information_schema.sequences]: could not execute query 
java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: com/ongres/scram/common/stringprep/StringPreparation

Here is my current build.gradle:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.6.21"
    id("org.jetbrains.kotlin.kapt") version "1.6.21"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.6.21"
    id("org.jetbrains.kotlin.plugin.jpa") version "1.6.21"
    id("com.github.johnrengelman.shadow") version "7.1.2"
    id("io.micronaut.application") version "3.6.7"
    id("io.micronaut.test-resources") version "3.6.7"
}

version = "0.1"
group = "example.micronaut"

repositories {
    mavenCentral()
}

dependencies {
    kapt("io.micronaut.data:micronaut-data-processor")
    kapt("io.micronaut:micronaut-http-validation")
    kapt("io.micronaut.serde:micronaut-serde-processor")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut.data:micronaut-data-hibernate-reactive")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut.reactor:micronaut-reactor")
    implementation("io.micronaut.reactor:micronaut-reactor-http-client")
    implementation("io.micronaut.serde:micronaut-serde-jackson")
//    implementation("io.vertx:vertx-mysql-client")
    implementation("io.micronaut.sql:micronaut-vertx-pg-client")

    implementation("jakarta.annotation:jakarta.annotation-api")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    runtimeOnly("ch.qos.logback:logback-classic")
//    testResourcesService("mysql:mysql-connector-java")
    runtimeOnly("org.postgresql:postgresql")

    compileOnly("org.graalvm.nativeimage:svm")

    implementation("io.micronaut.sql:micronaut-vertx-pg-client")
    implementation("io.micronaut:micronaut-validation")

    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}


application {
    mainClass.set("example.micronaut.ApplicationKt")
}
java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
}
graalvmNative.toolchainDetection = false
micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("example.micronaut.*")
    }
    testResources {
        additionalModules.add("hibernate-reactive-postgresql")
    }
}

and my application.yml

micronaut:
  application:
    name: micronaut-guide
netty:
  default:
    allocator:
      max-order: 3
#tag::jpa[]
jpa:
  default:
    entity-scan:
      packages:
        - 'example.micronaut.domain' # <1>
    properties:
      hibernate:
        show-sql: true
        hbm2ddl:
          auto: update # <2>
        connection:
          db-type: postgres # <3>
    reactive: true
#end::jpa[]

I've also tried to add what was suggested below:

datasources:
  default:
    driverClassName: org.postgresql.Driver
    db-type: postgresql
    schema-generate: CREATE_DROP
    dialect: POSTGRES
jpa.default.properties.hibernate.hbm2ddl.auto: update

but I got the same error

Upvotes: 2

Views: 1062

Answers (1)

saw303
saw303

Reputation: 9082

Since your build.gradle is not part of your question and you initially started with MySQL, I suspect you did not modify the gradle.build file.

micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("example.micronaut.*")
    }
    testResources {
        additionalModules.add("hibernate-reactive-mysql") // <-- this needs to be removed
    }
}

Furthermore make sure you use these values in the application.yml

micronaut:
  application:
    name: deleteme
datasources:
  default:
    driverClassName: org.postgresql.Driver
    db-type: postgresql
    schema-generate: CREATE_DROP
    dialect: POSTGRES
jpa.default.properties.hibernate.hbm2ddl.auto: update
netty:
  default:
    allocator:
      max-order: 3

Important is datasources.default.db-type and the JDBC driver that indicates test resources what you want to do.

Upvotes: 1

Related Questions