How to start a server and create memory database with H2

236 views
Skip to first unread message

Tommi Korhonen

unread,
Nov 15, 2016, 9:23:09 AM11/15/16
to H2 Database
Hello H2 users!

I'm a newbie with H2. I've been browsing the documentation, but haven't been able to found a good example for what I need. Hopefully someone could help me with this. I'm looking for a script that
  1. starts an H2 as a server
  2. creates a memory database
  3. loads 'create table' -statements from a file and executes them
  4. loads data from csv-files and inputs data into tables (or links tables from a Postgres database)

Tommi Korhonen

unread,
Nov 16, 2016, 7:51:06 AM11/16/16
to H2 Database
I'm answering to myself. This is what I have so far.

# stop the server if already running
java -cp /path_to/h2/bin/h2-1.4.193.jar org.h2.tools.Server -tcpShutdown tcp://localhost
# start the server, allow access outside localhost
java -cp /path_to/h2/bin/h2-1.4.193.jar org.h2.tools.Server -webAllowOthers -tcpAllowOthers -pgAllowOthers &
# start command-line client. first connection creates database
java -cp /path_to/h2/bin/h2-1.4.193.jar org.h2.tools.Shell -url "jdbc:h2:tcp://localhost/mem:mydb;DB_CLOSE_DELAY=-1;INIT=runscript from 'init_database.sql' -sql ""

This seems to work.

Petr Holík

unread,
Nov 16, 2016, 1:56:37 PM11/16/16
to H2 Database
Hello,
all that you ask /as you proved/ can be found on H2 site. Sometimes its little bit hidden ;-)

Just reminder that for importing csv is function CSVREAD.
http://www.h2database.com/html/functions.html#csvread



Dne úterý 15. listopadu 2016 15:23:09 UTC+1 Tommi Korhonen napsal(a):
Reply all
Reply to author
Forward
0 new messages