Search code examples
grailsh2grails-4

How to view h2 database in a default grails 4 application


I am building a brand new application with Grails 4 and I am trying to look at my database as I make changes to the application, but I am not able to access the h2 database with the usual URL http://localhost:8080/dbconsole.

I have looked at the documentation and under "4.4.4 Database Console" it says that I should be able to access it using the above URL. It also says that it is enabled by default, which leads me to more confusion.

I have not changed anything in my application.yml after creating the app and I have only created one domain class and that is the only thing I have changed. I have also tried changing the serverURL as mentioned in 4.4.4 in the documentation, but I have changed that back to the default.

Here is my datasource and dev environment from application.yml

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.h2.Driver
    username: sa
    password: ''

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

Solution

  • I found an answer here.

    According to the site, the integration has been removed since Spring Boot already includes h2, but the url is now /h2-console.

    If you have removed Spring Boot's Developer Tools developmentOnly("org.springframework.boot:spring-boot-devtools") from the dependencies in the build.gradle, then you will also have to add the following to the application.yml(There should already be a spring section that you can add this to by default).

    spring:
      h2:
        console:
          enabled: true
    

    After restarting the app you should be able to navigate to http://localhost:8080/h2-console and it will show the normal h2 db login screen.