Getting Started with Liquibase and XML on Linux and macOS

After you install Liquibase, get started with our tutorial and learn how Liquibase works.

With Liquibase, SQL statements [1] are declared as Liquibase changesets [2] in a changelog file [3].

Liquibase [4] then uses the changelog to apply the changesets to target databases [5].

In this tutorial, you will use an example changelog to apply two rounds of updates to an H2 database that is included with the Liquibase installation files.

Applying updates to the database

  1. Copy the Liquibase /examples directory from the installation directory to another location on your machine.
  2. From a command line terminal, enter <your path>/examples/start-h2 to start the example H2 database. The database console opens automatically in a browser on port 9090.
  3. Open a new command line terminal and enter the following command:
  4. cd <your path>/examples/xml
    liquibase update

    Note: When prompted about the operation's report in Liquibase Hub, enter S to skip for now.

    Liquibase applies the following updates, which are specified as Liquibase changesets in example-changelog.xml.

    <changeSet id="1" author="your.name">
    <createTable tableName="person">
      <column name="id" type="int">
       <constraints primaryKey="true"/>
      </column>
      <column name="name" type="varchar(50)">
       <constraints nullable="false"/>
      </column>
      <column name="address1" type="varchar(50)"/>
      <column name="address2" type="varchar(50)"/>
      <column name="city" type="varchar(30)"/>
    </createTable>
    </changeSet>
    <changeSet id="2" author="your.name">
    <createTable tableName="company">
      <column name="id" type="int">
       <constraints primaryKey="true"/>
      </column>
      <column name="name" type="varchar(50)">
       <constraints nullable="false"/>
      </column>
      <column name="address1" type="varchar(50)"/>
      <column name="address2" type="varchar(50)"/>
      <column name="city" type="varchar(30)"/>
    </createTable>
    </changeSet>
    <changeSet id="3" author="other.dev">
      <addColumn tableName="person">
      <column name="country" type="varchar(2)"/>
      </addColumn>
    </changeSet>

    The author-id value pairs your.name:1 and your.name:2 prevent their respective changesets from accidentally being run multiple times as new changesets are added to the changelog for subsequent updates.

  1. From the database console, confirm that the Object view contains the PERSON and COMPANY tables that were created during the previous step.
  1. Using a text editor, open <your path>/examples/xml/example-changelog.xml and add the following changeset to the end of the file before the </databaseChangeLog> element:
  2. <changeSet author="your.name" id="4">
       <addColumn tableName="company">
       <column name="country" type="varchar(2)"/>
      </addColumn>
    </changeSet>
  1. Save and close example-changelog.xml.
  1. Enter the following command:
  2. liquibase update
  1. Confirm the update by refreshing the database console and verifying that the country column has been added to the COMPANY table.

Next steps