"After all, the engineers only needed to refuse to fix anything, and modern industry would grind to a halt." -Michael Lewis

Enable Massive Growth

How to Create an Embedded PostgreSQL Database With Spring Boot

Apr 2019

You can see the sample code for this post on GitHub.

PostgreSQL is still my favorite database, and if a project I'm working on makes sense as a relational database model, it's always what I reach for.

Automating database tests, and maintaining consistency between environments, is one of the biggest pain points between working locally and deploying to higher environments. In particular, when you need to take advantage of native features of the database you're using (since consistency between vendors on some of the finer details is nearly a pipe-dream at this point), using a general in memory database (like H2) just doesn't cut it.

To start using a PostgreSQL in memory database (with as little spring boot magic as possible), you will first need to ensure that you have a PostgreSQL dependency. If you're using Maven, that's:


We can then resort to using opentable as the in memory engine by adding another dependency:


Finally, wherever we want the embedded database, we can spin it up with default settings like:

package com.nickolasfisher.flywaystuff;

... imports ...

public class DevConfig {

    public DataSource inMemoryDS() throws Exception {
        DataSource embeddedPostgresDS = EmbeddedPostgres.builder()
        return embeddedPostgresDS;

Next up, we'll look at using Flyway to run idempotent database migration scripts against our database on application startup, giving the application flexible and full control over the state of the schemas it owns.

Nick Fisher is a software engineer in the Pacific Northwest. He focuses on building highly scalable and maintainable backend systems.