
Overview
Firstly the goal of this blog is to walk you through setting up a new Maven project on a Linux, Unix, or Mac computer with PostgreSQL. You will generate an example project in this tutorial. Then follow the instructions to put the concepts you learn about building new Liquibase projects in Maven into practice.
We will start this blog by installing and configuring Liquibase.
Step: 1 Install Liquibase
We advise starting with the Liquibase Installer if you are new to Liquibase. Since the installer comes with everything you need to get going right away, getting started is made simple.
Step 2: Configure Liquibase
You can use the command line to specify settings in Liquibase. Instead of having to set default values in the CLI unless you want to, you can save them in a liquibase.properties file. A value supplied on the command line will always take precedence over a liquibase.properties file when used with Liquibase.
If you are installing Liquibase from Liquibase Installer, the example liquibase.properties files for both the XML and SQL formats are already included in your Liquibase download. Next, we advise that you continue reading about Using the Liquibase Installer.
Step 3: Set up your dashboard
Create a free Liquibase Hub account to view database release information for changes that occur in all environments in one location.
Although you’ll receive an API key after creating an account that you can use to link your changelogs to the dashboard. Once you’re connected, you can view in real time which changes are passing and failing in various scenarios.
Creating Liquibase project with Maven and PostgreSQL
Start by doing the subsequent actions to establish a Liquibase project in Maven that makes use of a PostgreSQL database:
- Create a new project folder and name it
MavenPostgreSQL
. - dbchangelog.xml should be created as a new plain-text file in the MavenPostgeSQL directory. Your database structure modifications will be tracked in this file, which will serve as your changelog. On the Working with Changelogs page, you may get additional information about them. You will manually add one change in this tutorial.
- Open the
dbchangelog.xml
file and update it with the following text. This is an empty changelog file.
<?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">
</databaseChangeLog>
- Create another plain text file in the same directory and give it a name like
liquibase.properties
. - Add the following properties to the newly created Liquibase properties file:
changeLogFile: dbchangelog.xml
url: jdbc:postgresql://localhost:5432/MYDATABASE
username: postgres
password: password
- To the changelog, add a changeset. Insert a fresh changeset into the dbchangelog.xml file. The creation of the department table is one of the modifications in this changeset.
<?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="bob">
<createTable tableName="department">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="active" type="boolean"
defaultValueBoolean="true"/>
</createTable>
</changeSet>
</databaseChangeLog>
- Create the Maven POM file for the project. Create a new plain-text file in the same directory named
pom.xml
. - Edit the
pom.xml
file and update it to have the following contents:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my-group.app</groupId>
<artifactId>LiquiPostgreSQL-app</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<propertyFile>liquibase.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
- Go to the MavenPostgreSQL directory by opening the command prompt and selecting it.
- Run the following command:
mvn liquibase:update
- Check your database updates under “MYDATABASE” via a database UI tool, such as “pgAdmin.” The database should now have a new “department” table.
Conclusion
In conclusion, in this blog, we covered the implementation of Liquibase with Maven and PostgreSQL.
References :
Liquibase documentation: https://docs.liquibase.com/home.html
Knoldus blog : https://blog.knoldus.com/