18

In Spring Boot project I am trying to see in-memory tables from my IDE.

How to access in-memory h2 database from Intellij IDEA.

Here is a snippet from my application.yml:

 h2:
    datasource:
        url: jdbc:h2:mem:mydb
        username: username
        password: 123
        driver-class-name: org.h2.Driver
        init-sql: h2.sql
    console:
      enabled: true
      path: /search/console
      settings:
        trace: false
        web-allow-others: false

Intellij has no field to input username for in-memory database: Intellij has no field to input username for in-memory database Test Connection shows success, however it doesn't see tables from h2.sql. I can access them using h2 console.

valijon
  • 1,304
  • 2
  • 20
  • 35
  • I assume Intellij IDEA is creating new in-memory db with default user and password when I click the button "Test Connection" – valijon Sep 18 '18 at 14:35
  • 1
    In-memory database results are available only while the application is running and you have stored some data. See http://www.h2database.com/html/features.html#in_memory_databases. If you want to create a Data Source for the in-memory database that your application uses, you need to start tcp server in your application on some tcp port and use this port when creating new H2 Data Source in IDE. – Andrey Sep 18 '18 at 15:19
  • If you add a maven or gradle runtime dependency it will be automatically available to you and populate some default values it should work fine – Vishal Torne Sep 18 '18 at 16:51
  • Duplicate of https://stackoverflow.com/questions/28940912/connect-to-h2-database-using-intellij-database-client/28950817#28950817 ? – Javaru Sep 19 '18 at 19:20
  • Duplicate of https://stackoverflow.com/a/64673466/2930427 Requires H2 to be exposed as per example code here: https://stackoverflow.com/a/52949164/2930427 – jase Nov 04 '20 at 03:59

3 Answers3

2

Just please bear in mind this may show you the db but not the table as they will only be visible in the h2 console. For you to access them through IntelliJ you may need to change the url and connection to be of a file type rather than in memory.

So instead of this:

#spring.datasource.url=jdbc:h2:mem:testdb

You'd have something like this:

spring.datasource.url=jdbc:h2:file:~/Users/yourUser/IdeaProjects/resume-portal/src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE

And then after creating a datasource from url

enter image description here

enter image description here

You should now see both the database and its tables.

enter image description here

And that will still also be available from the console if you connect using the new url.

enter image description here PS: If'd prefer to use a relative path you can change the url to be something similar to jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE

Here is the application.yml file for the application.

spring.jpa.defer-datasource-initialization=true
#spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
1

By the default, IntellJ doesn't show any of database.

enter image description here

  1. right click at the datasource then choose Properties

enter image description here

  1. At the Schemas tab, you'll see a list of options to choose which database should be shown (I normally choose All databases). Choose the database which you need it to be shown

enter image description here

The result:

enter image description here

Phong Bui
  • 441
  • 3
  • 7
1

I found another way. I created the H2 Server bean and added it to my SpringBoot Application. It looks like this:

// Start internal H2 server so can query from IDE
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
    return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}

I also had the following on my application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

spring.sql.init.mode=always

# Initialise H2 with H2GIS for spatial support ? see schema-h2.sql also
spring.sql.init.platform=h2
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
h2.tcp.enabled=true

In order to get my data.sql put into the tables in Intellij I had to make sure I used create-drop and added the spring.sql.init.mode=always

Note: I was using the H2GIS dialect - but this has not so far caused any issues

The connection setup in Intellij was still remote with this string jdbc:h2:tcp://localhost:9092/mem:testdb - dont forget to change the port to 9092

enter image description here

I the connection window in Intellij I also checked all tables under Schema. Then the tables are present for the H2 in mem database.

Al Grant
  • 2,102
  • 1
  • 26
  • 49