Covered in this quickstart:
- Installing Liquibase and using it on the command line
- Configuring Liquibase so it can talk to your database
- Capturing your existing database schema
- Creating your first database change
- Executing your database change
- Rolling back a change
Download and extract Liquibase
Run the installer or extract the Liquibase files you downloaded.
Open a command prompt to view your new directory:
$ cd liquibase-4.5.0
Configure Liquibase
Create a liquibase.properties text file to specify your driver classpath, URL, and user authentication information for the database you want to capture.
You’ll also put your Liquibase Pro license key in this file.
Sign up for a free 30-day trial. Try the advanced capabilities of Liquibase Pro.
Here’s an example of a properties file for a PostgreSQL database:
changeLogFile:dbchangelog.xml url: jdbc:postgresql://localhost:5432/mydatabase username: postgres password: password classpath: postgresql-42.2.8.jar liquibaseProLicenseKey: licensekey
Take a snapshot of your existing database
Capture the current state of your database by creating a deployable Liquibase changelog.
liquibase --changeLogFile=mydatabase_changelog.xml generateChangeLog
You’ll get a changelog for your database that looks something like this:
<?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: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-4.4.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.5.xsd"> <changeSet author="lb-generated" id="1185214997195-1"> <createTable name="BONUS"> <column name="NAME" type="VARCHAR2(15)"/> <column name="JOB" type="VARCHAR2(255)"/> <column name="SAL" type="NUMBER(255)"/> </createTable> </changeSet> <changeSet author="lb-generated" id="1185214997195-2"> <createTable name="DEPT"> <column name="DEPTNO" type="INTEGER"/> <column name="DNAME" type="VARCHAR2(15)"/> <column name="LOC" type="VARCHAR2(255)"/> </createTable> </changeSet> <changeSet author="lb-generated" id="1185214997195-3"> <createView fullDefinition="false" viewName="myView2">SELECT "DEPT".DEPTNO, "DEPT".DNAME FROM "DEPT";</createView> </changeSet> <changeSet author="lb-generated" id="1185214997195-4"> <pro:createFunction functionName="myFunction" path="objects/function/myFunction.sql" relativeToChangelogFile="true"/> </changeSet> </databaseChangeLog>
Create your first database schema change
Now you can start to make database changes by creating your first changeset in your dbchangelog.xml changelog like this:
<?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: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-4.4.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.5.xsd"> <changeSet author="BobR" id="myIDNumber123"> <createTable tableName="actor"> <column autoIncrement="true" name="id" type="INTEGER"> <constraints nullable="false" primaryKey="true" primaryKeyName="actor_pkey"/> </column> <column name="firstname" type="VARCHAR(255)"/> <column name="lastname" type="VARCHAR(255)"/> <column name="twitter" type="VARCHAR(15)"/> </createTable> </changeSet> </databaseChangeLog>
Deploy your database change
Now you can deploy your database change by running the update command like this:
liquibase-4.5.0$ liquibase update
If all went well, you’ll see the following output:
Liquibase: Update has been successful.
Rollback a change
Now you can roll back your database last change by running the Liquibase rollback command like this:
liquibase-4.5.0$ liquibase rollbackCount 1
If all went well, you’ll see the following output:
Rolling Back Changeset:dbchangelog.xml::myIDNumber123::BobR
Liquibase: Rollback has been successful.
There’s a lot more Liquibase can do!
For starters, you can create database changes with Liquibase using plain SQL, XML, JSON, or YAML. You can also embed Liquibase in your application or in automation.
- View our Documentation
- Find the tutorials for your specific database
- Take free online training at Liquibase University
- Get your questions answered and participate in the Liquibase Community forums