Featured Article

Liquibase Java Spring Boot Example Tutorial for Database Version Control

Kenneth Jul 13, 2026

Liquibase, an open-source database migration tool, is a boon for Java developers tasked with managing database schemas. When integrated with Spring Boot, it offers a seamless solution for version controlling and managing database schema changes. In this article, we'll explore a practical example of using Liquibase with a Spring Boot application.

8 Best Spring Boot Courses for Java Developers in 2026
8 Best Spring Boot Courses for Java Developers in 2026

First, let's set the stage. Liquibase enables you to describe changes to your database schema in a plaintext format, often referred to as changelog files. These files are version controlled, allowing you to track alterations to your database schema over time. By integrating Liquibase with Spring Boot, we can embed database migration functionality within our application.

Spring boot
Spring boot

Setting up Liquibase with Spring Boot

Before we dive into examples, let's configure Liquibase in a Spring Boot project.

Complete Spring Framework Cheat Sheet | Spring Boot, MVC
Complete Spring Framework Cheat Sheet | Spring Boot, MVC

1. Add the Spring Boot Liquibase starter dependency to your project. In Maven, add the following to your pom.xml:

```xml org.springframework.boot spring-boot-starter-liquibase ```

Configuring the dataSource

Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn
Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn

Ensure your application's dataSource is properly configured. Liquibase requires a dataSource to function correctly.

2. In application.properties, configure your dataSource:

``` spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=secret ```

Creating a changelog file

Spring vs Spring Boot – Key Differences Every Java Developer Should Know
Spring vs Spring Boot – Key Differences Every Java Developer Should Know

3. Create a resource folder named db/changelog with a properties file, changelog.yaml, containing version information.

4. Create your first changelog file, e.g., v1.0.0__initial_schema.sql, under db/changelog.

Defining Database Changes with Liquibase

Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline

Now that we've set up Liquibase, let's define some database changes.

The Liquibase SQL language is similar to standard SQL, but with additional commands for version tracking and change management. Here's an example of an initial schema:

How to Structure a Scalable Spring Boot Application | Ritesh Kumar Gupta posted on the topic | LinkedIn
How to Structure a Scalable Spring Boot Application | Ritesh Kumar Gupta posted on the topic | LinkedIn
Spring Boot Annotations Cheat Sheet (Save This!)
Spring Boot Annotations Cheat Sheet (Save This!)
an image of a computer screen with text and numbers on it, as well as other items
an image of a computer screen with text and numbers on it, as well as other items
Spring Boot Application Properties Example - Java Code Geeks
Spring Boot Application Properties Example - Java Code Geeks
6 Ways to Change Default Port in Spring Boot? Example Tutorial
6 Ways to Change Default Port in Spring Boot? Example Tutorial
an image of a computer architecture diagram with multiple data types in the same area and two separate lines that connect to each other
an image of a computer architecture diagram with multiple data types in the same area and two separate lines that connect to each other
Java + Spring Boot + Microservices Example - Step by Step Guide
Java + Spring Boot + Microservices Example - Step by Step Guide
Spring Boot Real-Time Project
Spring Boot Real-Time Project
Spring Boot Security-Scheduling-Transactions Annotations With Examples
Spring Boot Security-Scheduling-Transactions Annotations With Examples

```sql --liquibase formatted changelog -- -- changeSet 1.0.0 -- This changeSet creates the initial schema CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100) UNIQUE ); INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john.doe@example.com'); ```

Running Migrations

5. When your Spring Boot application starts, Liquibase will automatically run any outstanding migrations.

6. To manually run migrations, use the Liquibase CLI with the -url, -username, and -password parameters pointing to your database.

Managing Database Changes

Liquibase provides several commands for managing migrations, including:

  • list-Changelogs: Lists all changelogs in the order they've been executed.
  • first-changelog: Returns the ID of the first changelog in the system.
  • count-changesets: Returns the total number of executed and pending changelogs.

Rolling Back Changes

1. To roll back to a previous version, Liquibase provides the rollBack command:

```bash liquibase rollback --count 1 ```

2. This command will undo the last migration, maintaining database consistency.

Best Practices

To ensure the smooth operation of Liquibase, adhere to the following best practices:

  1. Divide large changes into smaller, manageable scripts.
  2. Use unique, descriptive IDs for your migrations.
  3. Follow naming conventions for changelog files (e.g., v1.0.0__description_of_change).
  4. Always version control your changelog files.

In conclusion, Liquibase provides a robust solution for managing database schema changes. By integrating it with Spring Boot, you can maintain your database schema as part of your software development lifecycle. Embrace this practice, and elevate your application's reliability and maintainability.