Get started with Liquibase and Neon
Learn how to manage schema changes in Neon with Liquibase
Liquibase is an open-source library for tracking, managing, and applying database schema changes. To learn more about Liquibase, refer to the Liquibase documentation.
This guide steps you through installing the Liquibase CLI, configuring Liquibase to connect to a Neon database, deploying a database schema change, and rolling back the schema change. The guide follows the setup described in the Liquibase Get Started.
Prerequisites
- A Neon account. See Sign up.
- A Neon project. See Create your first project.
- Liquibase requires Java. For Liquibase Java requirements, see Requirements. To check if you have Java installed, run
java --version
, orjava -version
on macOS`.
Download and extract Liquibase
-
Download the Liquibase CLI from https://www.liquibase.com/download.
-
Extract the Liquibase files. For example:
-
Open a command prompt to view the contents of your Liquibase installation:
Set your path variable
Add the Liquibase directory to your PATH
so that you can run Liquibase commands from any location.
Verify your installation
Verify that the Liquibase installation was successful by running the following command:
Prepare a Neon database
For demonstration purposes, create a blog
database in Neon with two tables, posts
and authors
.
-
Open the Neon Console.
-
Select your project.
-
Select Databases from the sidebar and create a database named
blog
. For instructions, see Create a database. -
Using the Neon SQL Editor, add the following tables:
Retrieve your Neon database connection string
From the Neon Dashboard, retrieve your password and a Java connection string from the Connection Details widget. Use the selection drop-down menu.
Your Java connection string should look something like the one shown below.
Connect from Liquibase to your Neon database
-
Create a directory for your Liquibase project. For example:
-
Change to your project directory and create a
liquibase.properties
file. -
Open the the
liquibase.properties
file in an editor and add entries for a liquibase changelog file and your databaseurl
. We'll call the changelog filedbchangelog.xml
. You will use this file to define schema changes. For theurl
, specify the Neon connection string you retrieved previously.
Take a snapshot of your database
In this step, you will run the generateChangelog command in your project directory to create a changelog file with the current state of your database. We'll call this file mydatabase_changelog.xml
.
You’ll get a changelog file for your database that looks something like this:
Create a schema change
Now, you can start making database schema changes by creating changesets and adding them to the database changelog file you defined in your liquibase.properties
file. A changeset is the basic unit of change in Liquibase.
-
Create the changelog file where you will add your schema changes:
-
Add the following changeset, which adds a
comments
table to your database. Replaceauthor="alex" id="myIDNumber1234"
with your auther name and id, which you can retrieve from your changelog file, described in the previous step.
Deploy your change
Deploy your database schema change by running the update command:
Command output
If the command was successful, you’ll see output similar to the following:
info
When you run a changeset for the first time, Liquibase automatically creates two tracking tables in your database:
- databasechangelog: Tracks which changesets have been run.
- databasechangeloglock: Ensures only one instance of Liquibase runs at a time.
You can verify these tables were created by viewing the blog
database on the Tables page in the Neon console. Select Tables from the sidebar.
Rollback a change
Try rolling back your last change by running the Liquibase rollbackCount command:
Command output
If the command was successful, you’ll see output similar to the following:
You can verify that creation of the comments
table was rolled back viewing the blog
database on the Tables page in the Neon console. Select Tables from the sidebar.
Next steps
Learn how to use Liquibase with Neon's database branching feature to set up a developer workflow. See Set up a developer workflow with Liquibase and Neon.
References
Last updated on