Corda’s database compatibility

October 05, 2021

We occasionally get questions about which databases Corda supports. This can be a bit of a confusing topic, so this blog aims to demystify Corda’s database compatibility.

There are a few databases to look at: H2, PostgreSQL, SQL Server, Oracle, and Azure SQL.

H2

H2 is Corda’s default database. It’s a variant of SQL made specifically for JVM apps. It’s fast, embedded, supports in-memory databases, and is relatively small—JAR files can be as little as 2MB! So it’s not surprising that this is the choice for local Corda development.

Your H2 database runs within the node, you don’t have to do anything specific. You can set up things like specific ports, but generally, you won’t need to configure anything for a Corda node to work locally.

When building your CorDapps, you’ll notice that H2 databases can also be persistent. In the `build` folder, you’ll see a `persistence` folder which contains files like `persistence.mv.db`.

You can also view the database using a JDBC browser and the H2 console.

H2 Console Popup window

Login Popup window

Much has already been written on H2, but I’d recommend taking a look at the Corda docs guide on database access when running H2.

PostgreSQL

Postgres is the database that we get the most questions on by far, and for good reason.

For Postgres to work correctly, you have to create schemas with the correct permissions, and then make sure the proper privileges are applied. The Postgres CordApp is a good example for setting this up.

SQL Server

Configuring Corda to use SQL Server is relatively simple. Use a simple data source properties configuration:

dataSourceProperties = {
dataSourceClassName = "com.microsoft.sqlserver.jdbc.SQLServerDataSource"
dataSource.url = "jdbc:sqlserver://:;databaseName=my_database"
dataSource.user = my_login
dataSource.password = "my_password"
}
database = {
schema = my_schema
}

Configuration is similar to the Postgres CordApp, but you’ll need to use a different JDBC driver (you can get this from the Microsoft Download Center).

You can read more about database schema setup in the Corda docs.

Oracle

To use Oracle, you’ll want to connect to the database as an administrator and create a user:

dataSourceProperties = {
dataSourceClassName = "oracle.jdbc.pool.OracleDataSource"
dataSource.url = "jdbc:oracle:thin:@::"
dataSource.user = my_user
dataSource.password = "my_password"
}
database = {
schema = my_admin_user
}

In this case, you’ll need to get your JDBC driver from Oracle.

Azure SQL
Using Azure SQL is a little more complicated. You’ll need to create two users: one will generate schema objects and the other will have permissions for a Corda node.

Run a couple of commands on your database to create them:

CREATE LOGIN my_admin_login WITH PASSWORD = 'my_password';
CREATE USER my_admin_user FOR LOGIN my_admin_login;
CREATE LOGIN my_login WITH PASSWORD = 'my_password';
CREATE USER my_user FOR LOGIN my_login;

Then you can create a schema and assign some permissions:

CREATE SCHEMA my_schema;
GO
CREATE USER my_admin_user FOR LOGIN my_admin_login WITH DEFAULT_SCHEMA = my_schema;
GRANT ALTER ON SCHEMA::my_schema TO my_admin_user;
GRANT SELECT, INSERT, UPDATE, DELETE, VIEW DEFINITION, REFERENCES ON SCHEMA::my_schema TO my_admin_user;
GRANT CREATE TABLE TO my_admin_user;
GRANT CREATE VIEW TO my_admin_user;
CREATE USER my_user FOR LOGIN my_login WITH DEFAULT_SCHEMA = my_schema;
GRANT SELECT, INSERT, UPDATE, DELETE, VIEW DEFINITION, REFERENCES ON SCHEMA::my_schema TO my_user;

Once you’ve set up your schemas properly, Azure SQL should collaborate with Corda without too many issues.

Summary

That’s essentially everything you need to know as far as compatibility goes. Hopefully, these concepts, samples, and resources will come in handy.

Best of luck, and happy coding ~

If you want more information, I recommend you look at:

Share: