Using Spring Boot with PostgreSQL

Prerequisites

This tutorial builds on top of the Bootstrap a Spring Boot project guide.

Although it is not required to have te exact same setup, we will refer and highlight content from that guide.

Start a PostgreSQL instance

In our case we will use Docker, but it’s not required.

docker run --name testdb \
    -p 5432:5432 \
    -e POSTGRES_DB=testdb \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=postgres \
    -d postgres

Update sources

pom.xml

Located under: pom.xml

We need to add postgresql as a runtime dependency.

<dependencies>
    <!-- ... -->
    <!-- Add postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
<dependencies>

application.yml

Located under: src/main/resources/application.yml

We need to modify the following sections:

  • spring.datasource

  • spring.liquibase.change-log

spring:
  # ...
  datasource:
    driver-class-name: org.postgresql.Driver # update the driver class name
    url: jdbc:postgresql://localhost:5432/testdb # update the database url
    username: postgres # update username
    password: postgres # update password
  liquibase:
    change-log: classpath:model/@judo-model-name@-liquibase_postgresql.changelog.xml # use the generated postgresql-specific changelog
# ...

The connection details are mirroring our database setup. Do not use these in a production!

Re-run tests

After we are done with all the above, we should be able to run our tests against our PostgreSQL instance.

mvn clean install