'How to pre-create or allow autocreation of H2 database?

I am trying to open test url

jdbc:h2:tcp://localhost/~/test

from H2 web interface, but it says

Database "D:/Users/USERNAME/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments)

How to fulfil either pf propositions?



Solution 1:[1]

If you have a separate H2 Server process (launched from a jar file or as a service), you can open H2 Console from its icon in the system tray and use jdbc:h2:~/test URL in it, this copy of H2 Console should be able to create embedded databases automatically (unless remote access from other hosts was enabled). Other Java processes will still need jdbc:h2:tcp://localhost/~/test URL for connections to this database and they can use this remote URL after creation of embedded database on the server.

If H2 Console and TCP Server of H2 were launched from your application, your application should create this database by itself by establishing a connection with the same embedded URL jdbc:h2:~/test first.

You can also use a shell tool or any other way to create your database: https://h2database.com/html/tutorial.html#creating_new_databases

All these ways also require the jdbc:h2:~/test URL to be used during creation.

There is also a flag to enable remote database creation, but this feature opens a security hole, either local (if remote connections are disabled by default) or remote if they were explicitly enabled. You should avoid usage of this feature.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evgenij Ryazanov