MoonPie
MoonPie

Reputation: 63

R2dbc database not auto initialising in spring project

I am creating a simple spring boot project that connects to the r2dbc postgres

My application.yml looks like this :

  r2dbc:
    url: r2dbc:postgresql://localhost:5432/postgres
    username: postgres
    password: admin
    sql:
      init:
        mode: always
  logging:
    level:
      org.springframework: DEBUG
      io.r2dbc: DEBUG
  data:
    initialization-mode: always


server:
  port: 8088

My UserData model is :

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "USER_DATA", schema = "management")
public class UserData {
    @Id
    String userId;
    String firstName;
    String lastName;
    String emailId;
    String password;
}

I have created this schema.sql file in my src/main/resources folder as per the documentation of SpringBoot specified

CREATE SCHEMA IF NOT EXISTS management;
CREATE TABLE IF NOT EXISTS user_data (
    user_id SERIAL PRIMARY KEY,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    email_id VARCHAR(100) UNIQUE,
    password VARCHAR(255)
);
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kaori</groupId>
    <artifactId>management</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
            <version>0.8.13.RELEASE</version> <!-- Correct version -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

As soon as i start my application the schema doesn't create nor does the table. I have tried all sorts of configuration for this but cant seem to get it working

Upvotes: 0

Views: 59

Answers (1)

Hantsy
Hantsy

Reputation: 9241

The SQL initialization property is spring.sql.init.mode, not r2dbc.sql.init.mode. This property works for Jdbc/R2dbc,etc.

spring.sql.init.mode=always 

Check my working example.

Upvotes: 0

Related Questions