MikeHT
MikeHT

Reputation: 297

Setting H2 database name in Spring Boot project

I have a Spring Boot project that uses H2. No matter what I set my database name to in my application.properties file, whey I bring up the H2 console, the name of the database is "test". I can log in and see my schema, but can't set the name of the database to anything but "test". No matter what name I use in the application.properties file, the name on the H2 console is "test".

My POM is

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.repository</groupId>
<artifactId>repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>repository</name>
<description>repository project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </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>

My application.properties file is

spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.jpa.show-sql=true

server.port=8081

Upvotes: 1

Views: 9755

Answers (3)

Wiktor Metelski
Wiktor Metelski

Reputation: 111

You need to set "generate-unique-name" to false and set name as below:

spring.datasource.generate-unique-name=false
spring.datasource.name=my-new-db-name

Upvotes: 7

madmax
madmax

Reputation: 96

  • I've tried the following settings and it worked as expected: rcp_h2.mv.db file was created.
  • For H2 URL: As Anton suggested - just go ahead and replace the default URL with your i.e. "jdbc:h2:mem:rcp_h2", it will get remembered so next time you don't need to do it.

If still the problem persists, you should try change other settings for example: port # or "spring.h2.console.path=/h2-console" -> "spring.h2.console.path=/h2" to see if it takes effect after the app restart. If not then probably something is wrong with your project setup.

spring.datasource.url=jdbc:h2:file:c:/temp/rcp_h2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.jpa.show-sql=true

server.port=8081

Upvotes: 0

Anton N
Anton N

Reputation: 2375

The followingis the reference to database name - file:~/test. You need to change test to something else.

Upvotes: 0

Related Questions