简体   繁体   中英

How to export persistent database to in-memory databse in h2 from terminal

EDIT: fixed wrong command. I have an h2 database on disk. How can I export it into in-memory? I have tried the following:-

java -cp h2/bin/h2*.jar org.h2.tools.Server -tcp
java -cp h2/bin/h2*.jar org.h2.tools.RunScript -url jdbc:h2:$(pwd)/restapi -user sa -script export.sql
java -cp h2/bin/h2*.jar org.h2.tools.RunScript -tcp -url jdbc:h2:mem:testdb -user sa -script db-dump.sql &

where export.sql contains:-

SCRIPT TO 'db-dump.sql

but When I try to connect to jdbc:h2:mem:testdb via the web console, I get following error:-

Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149 (Help)

Server tool doesn't have connection parameters and can't be used to create a new database; your first command is not valid. Did you check its output?

RunScript tool can create a new database, but there is no reason to create an embedded in-memory database with it, such database will be available only to this tool and only while this process is running.

You need to take other steps instead.

  1. Export your database into SQL script with SCRIPT TO 'db-dump.sql' (if you have an open connection to it somewhere) or with
java -cp h2/bin/h2*.jar org.h2.tools.Script -url jdbc:h2:~/test -user sa -script db-dump.sql

if this persistent database is not opened anywhere.

  1. Start the normal H2 Server process; it provides transparent authentication for H2 Console that allows you to create a new database with it:
java -jar h2/bin/h2*.jar

It should open a web browser window with H2 Console.

In this window specify jdbc:h2:mem:1;INIT=RUNSCRIPT FROM 'db-dump.sql' as JDBC URL.

You can re-open such window by double-click on H2 Console icon in the system tray and you can use other commands from its context menu.

Note that if you type localhost:8082 in the browser directly, you will not be able to create a new database in it; you really need to open it from H2 (or you can copy URL with temporary security key from its status window available in context menu of tray icon).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM