Setting up a Liquibase project with Maven and PostgreSQL

The purpose of this document is to guide you through the process of creating a new Maven project with PostgreSQL on a Linux/Unix/Mac machine. In this tutorial, you will generate an example project and follow the instructions to apply and learn concepts associated with creating new Liquibase projects within Maven.

Prerequisites

Installing Liquibase with Maven on Linux/Unix/Mac/Windows if you have not done so already.

Maven – Liquibase project tutorial

To create a Liquibase project within Maven that uses a PostgreSQL database, begin with the following steps:

  1. Create a new project folder and name it MavenPostgreSQL.
  2. Create a new plain-text file named dbchangelog.xml in the MavenPostgeSQL directory. This file will be your changelog, a file that will keep track of all the changes you make to your database structure. You can learn more about them on the Changelog page. In this tutorial, you will manually add a single change.
  3. Open the dbchangelog.xml file and update it with the following text. This is an empty changelog file.
  4. <?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>
  5. Create another plain text file in the same directory and give it a name like liquibase.properties.
  6. Add the following properties to the newly created Liquibase properties file:
  7. changeLogFile: dbchangelog.xml
     url: jdbc:postgresql://localhost:5432/MYDATABASE
     username: postgres
     password: password

    Note: If you have a Liquibase Pro license key, add the following property to the Liquibase properties file: licenseKey: <paste license key>.

  8. Add a changeset to the changelog. In the dbchangelog.xml file, add a new changeset. This changeset includes a change to create a table named department.
  9. <?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>
  10. Create the Maven POM file for the project. Create a new plain-text file in the same directory named pom.xml.
  11. Edit the pom.xml file and update it to have the following contents:
  12.  <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>
  1. Open the command prompt and navigate to the MavenPostgreSQL directory.
  2. Run the following command: mvn liquibase:update
  3. From a database UI Tool, for example: “pgAdmin” check your database changes under “MYDATABASE”. You should see a new “department” table added to the database.

Also, you should see two more tables:

  1. DATABASECHANGELOG - This table keeps a record of all the changesets that have been deployed. The next time you run the update command, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking table, and only the new changesets not found in the DATABASECHANGELOG will be deployed. You will notice that a new row was created in that table with the changeset information you have just deployed.
  2. DATABASECHANGELOGLOCK - This table is used internally by Liquibase to manage access to the changelog table during deployment.