Using Liquibase with CockroachDB
CockroachDB is a distributed database with standard SQL for cloud applications. You can run CockroachDB on your local machine or use a cloud cluster.
Supported versions
- 22.X
- 21.X
- 20.X
Prerequisites
- Introduction to Liquibase – Dive into Liquibase concepts.
- Install Liquibase – Download Liquibase on your machine.
- Get Started with Liquibase – Learn how to use Liquibase with an example database.
- Design Your Liquibase Project – Create a new Liquibase project folder and organize your changelogs
- How to Apply Your Liquibase Pro License Key – If you use Liquibase Pro, activate your license.
Install drivers
To use Liquibase and CockroachDB, you need the JDBC driver JAR file (Maven download).
The latest version of Liquibase has a pre-installed driver for this database in the liquibase/internal/lib
directory, so you don't need to install it yourself.
If you use Maven, you must include the driver JAR as a dependency in your pom.xml
file.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.0</version>
</dependency>
Test your connection
- Ensure your CockroachDB is configured:
- You can check its status depending on your cluster setup. For example, you can check basic network connectivity (ping), port connectivity (telnet), and certificate validity. See the Troubleshoot Cluster Setup for more details.
- Generate or check TLS certificates for the user that you created during a secure CockroachDB cluster setup. Use the
cockroach cert
command to generate the certificates:
cockroach cert create-client user --certs-dir=certs --ca-key=my-safe-directory/ca.key --also-generate-pkcs8-key
- Specify the database URL in the
liquibase.properties
file (defaults file), along with other properties you want to set a default value for. Liquibase does not parse the URL. You can either specify the full database connection string or specify the URL using your database's standard JDBC format:
CockroachDB on-premises
url:jdbc:postgresql://localhost:26257/database?ssl=true&sslmode=require&sslrootcert=/full-path/certs/ca.crt&sslkey=/full-ath/certs/client.user.key.pk8&sslcert=/full-path/certs/client.user.crt
When using CockroachDB on-premises and specifying the URL, enter your IP address or host name, and then the port followed by the database name. An example of the format is: jdbc:postgresql://<IP OR HOSTNAME>:<PORT>/<DATABASE>
.
The SSL connection parameters to the full paths of the certificates that you generated are optional. A key in PKCS#8 format is the standard key encoding format in Java. As an alternative, you can use the URL without SSL connection parameters by specifying the username and password attributes:
url: jdbc:postgresql://localhost:26257/dev
username: root
password: password
CockroachDB cloud
url: jdbc:postgresql://liquibase-3r8.aws-us-east-2.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&sslrootcert=liquibase-ca.crt
When using a CockroachCloud instance and specifying URL, enter a global host name and the port 26257
by referring to the CockroachCloud website. Also, add the database name with the SSL mode and the path to the CA certificate to your URL.
Tip: To apply a Liquibase Pro key to your project, add the following property to the Liquibase properties file: licenseKey: <paste code here>
- Create a text file called changelog (
.xml
,.sql
,.json
, or.yaml
) in your project directory and add a changeset. - Navigate to your project folder in the CLI and run the Liquibase status command to see whether the connection is successful:
- Inspect the SQL with the update-sql command. Then make changes to your database with the update command.
- From a database UI tool, ensure that your database contains the
test_table
you added along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table.
If you already created a changelog using the init project
command, you can use that instead of creating a new file. When adding onto an existing changelog, be sure to only add the changeset and to not duplicate the changelog header.
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
<changeSet id="1" author="Liquibase">
<createTable tableName="test_table">
<column name="test_id" type="int">
<constraints primaryKey="true"/>
</column>
<column name="test_column" type="INT"/>
</createTable>
</changeSet>
</databaseChangeLog>
SQL example
-- liquibase formatted sql
-- changeset liquibase:1
CREATE TABLE test_table (test_id INT, test_column INT, PRIMARY KEY (test_id))
Tip: Formatted SQL changelogs generated from Liquibase versions before 4.2 might cause issues because of the lack of space after a double dash ( --
). To fix this, add a space after the double dash. For example: -- liquibase formatted sql
instead of --liquibase formatted sql
and -- changeset myname:create-table
instead of --changeset myname:create-table
.
databaseChangeLog:
- changeSet:
id: 1
author: Liquibase
changes:
- createTable:
tableName: test_table
columns:
- column:
name: test_column
type: INT
constraints:
primaryKey: true
nullable: false
JSON example
{
"databaseChangeLog": [
{
"changeSet": {
"id": "1",
"author": "Liquibase",
"changes": [
{
"createTable": {
"tableName": "test_table",
"columns": [
{
"column": {
"name": "test_column",
"type": "INT",
"constraints": {
"primaryKey": true,
"nullable": false
}
}
}
]
}
}
]
}
}
]
}
liquibase status --username=test --password=test --changelog-file=<changelog.xml>
Note: You can specify arguments in the CLI or keep them in the Liquibase properties file.
If your connection is successful, you'll see a message like this:
4 changesets have not been applied to <your_jdbc_url>
Liquibase command 'status' was executed successfully.
liquibase update-sql --changelog-file=<changelog.xml>
liquibase update --changelog-file=<changelog.xml>
If your update
is successful, Liquibase runs each changeset and displays a summary message ending with:
Liquibase: Update has been successful.
Liquibase command 'update' was executed successfully.
Now you're ready to start making deployments with Liquibase!
Note: CockroachDB has limited support for online schema changes in transactions. To avoid issues with incomplete transactions, you can set the runInTransaction attribute to false
. However, take into account that if this attribute is set to false and an error occurs part way through running a changeset that contains multiple statements, the Liquibase DATABASECHANGELOG table will be left in an invalid state.