I have introduced you to database migration using Liquibase. Flyway does something similar, allowing us to manage the version of our database structure. It records migration information in a schema history table and uses database locking to prevent concurrent migration processes. How exactly does it work? Let’s find out in this tutorial!
First, I will create a new Maven project as an example:

with Flyway and PostgreSQL JDBC driver dependencies as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>${flyway-version}</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-database-postgresql</artifactId> <version>${flyway-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.14</version> </dependency> |
With:
|
1 2 3 4 5 |
<properties> ... <flyway-version>13.12.2</flyway-version> </properties> |
Here, the flyway-core dependency provides the Flyway core engine, while the flyway-database-postgresql dependency provides support for the PostgreSQL database.
I also use Java 25 for this example:
|
1 2 |
<maven.compiler.source>25</maven.compiler.source> <maven.compiler.target>25</maven.compiler.target> |
With Flyway, we can use SQL files or Java code to perform database migrations. Usually, I use SQL files because it’s much simpler. In this tutorial, I will use an SQL file as an example. If you want to use Java code, you can read more here.
With SQL files, Flyway, after having a connection to the database, will read these SQL files in a directory that we configure. The default is the src/main/resources/db/migration directory to do the migration.
The names of the SQL files need to follow a certain naming convention.
Flyway allows you to migrate to a new version of your database structure using a new SQL file with versioned migration. You can also use a single SQL file with repeatable migration. If you already have V1, V2, and V3 versions of a database structure, you can define a baseline migration version B3 so that when deploying your application to a new environment, Flyway only picks up from this version onwards, eliminating the need to run V1, V2, and V3 migration scripts. Flyway also supports undo migration, but this is only available for the Flyway Teams edition.
Below is an example of SQL file names:
- V2026.11.21.00000__Init.sql if adding a new version
- R__People_view.sql if it’s a repeatable migration
- B2026.11.21.00000__Baseline.sql if it’s a baseline migration.
- U2026.11.21.00000__Init.sql if it’s an undo migration.
The filename will start with the character V, R, B, or U, depending on how you want to perform the migration, as in the example above. Next is the version. You can refer to the version naming convention here. I usually use a date-based version like in the example above.
The separator between the two parts of the SQL file name is 2 underscore characters “__”; the part after these 2 underscores is a description of the changes you make in this SQL file.
As an example for this tutorial, I will create a new SQL file with the name V2021.08.07.000__Init with the content of creating a new student table as follows:
|
1 2 3 4 |
CREATE TABLE student ( ID INT PRIMARY KEY, name VARCHAR(250) NOT NULL ); |
Now, I will create a new main class to see how Flyway works.
The content of this class is initially as follows:
|
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.flyway; public class Application { public static void main(String[] args) { } } |
We will use the object of the Flyway class, and initialize it with the DataSource information connected to the database to prepare for the migration, as follows:
|
1 2 3 |
Flyway flyway = Flyway.configure() .dataSource("jdbc:postgresql://localhost:5432/student", "postgres", "123456") .load(); |
If you already have DataSource information, you can also use another overloaded method to use this DataSource:

no need to declare databaseURL, username, and password like me.
To run the migration, we will call the migrate() method of the Flyway object as follows:
|
1 |
flyway.migrate(); |
Now, if you run this example, and then check the database, you will see the following results:

In addition to the student table that we have declared in the SQL file, Flyway also creates another table similar to Liquibase to manage all the versions we have run. This table is called flyway_schema_history, used to keep track of history:

Some information recorded in the flyway_schema_history table is as follows:
- Migration version
- Description
- Migration type
- Script
- Checksum
- Execution timestamp
- Execution status
Flyway extracts version information from the name of the SQL file. It also uses a column to store the SQL file’s checksum to check which SQL files have run and which have not. You must not change the content of the files that Flyway has run the migration, if there are any changes, Flyway will immediately report an error.
If you want to add a new version, just create a new SQL file according to the naming convention that I mentioned above!
For example, if I add a new address column for the student table, I will create a new SQL file with the name V2021.08.0700001__add_student_address and the following content:
|
1 2 |
ALTER TABLE student ADD COLUMN address VARCHAR(255); |
Result:

