Set up Liquibase for multiple environments
Last updated: July 14, 2025
In real-world projects, you’ll often work with several database environments, such as development, testing, staging, and production. Liquibase gives you tools to apply database changes safely and selectively across these environments. This article walks through how to use Liquibase contexts to manage multiple environments.
Before you begin
Make sure you've installed Liquibase and can run
liquibase
from your command line.Connect your database to Liquibase. You'll need to follow the integration guide for your specific database.
If you have an existing database, you'll also need to generate your changelog from your existing database.
Set up your changelog structure and be sure your changelogs are connected.
Procedure
Add context labels to your changesets
Liquibase contexts let you apply changesets conditionally depending on the environment.
You can define changesets with contexts like legacy
and non-legacy
. In the example code, the first changeset runs in the legacy environment, HR_Dev
. The second changeset runs in the non-legacy environment, HR_Test
.
Example Code
<changeSet id="1" author="liquibase" context="legacy">
<createTable tableName="old_employee_data">
<column name="emp_id" type="int" />
<column name="name" type="varchar(100)" />
</createTable>
</changeSet>
<changeSet id="2" author="liquibase" context="non-legacy">
<createTable tableName="employee">
<column name="id" type="int" />
<column name="first_name" type="varchar(50)" />
<column name="last_name" type="varchar(50)" />
</createTable>
</changeSet>
Apply your changes.
To apply changes only for HR_Dev, run:
liquibase update --context-filter=legacy
To apply changes for HR_Test
, run:
liquibase update --context-filter=non-legacy