Spring boot H2 In memory database.

Application property to enable H2 console:

spring.h2.console.enabled = true

URL for H2 console:

http://localhost:8080/h2-console

Error : Database “C:/Users/sa/test” not found, either pre-create it or allow remote database creation (not recommended in secure environments)

OR

Database “mem:testdb” not found, either pre-create it or allow remote database creation (not recommended in secure environments)

Solution: In the logs, search for “h2-console” and copy the jdbc url to connect. In my case the url is: jdbc:h2:mem:07b1af7f-7014-4cf8-be0d-4fba35f1d245.

Update the JDBC URL to : jdbc:h2:mem:07b1af7f-7014-4cf8-be0d-4fba35f1d245

But the problem with this solution is every time we restart the app, the JDBC url will change. That will be annoying. To avoid the db name change every time the server restarts and to set a constant H2 database name, we can use the below properties in application.yml

spring:
datasource:
generate-unique-name: false
name: my-test-db

Once we have this setting we can connect to H2 using jdbc:h2:mem:my-test-db url and it will not change every time the server restarts.

The default H2 schema is “PUBLIC”

Connection test query : “SELECT 1 FROM DUAL”

%d bloggers like this: