How To Setup A Springboot App with PostgreSQL Database Using Docker & IntelliJ IDEA Ultimate Edition.

How To Setup A Springboot App with PostgreSQL Database Using Docker & IntelliJ IDEA Ultimate Edition.

IntelliJ Integrated Development Environment Amplification (IDEA) Ultimate Edition is the paid version of the IDEA. If you're a student, you can get it for free via GitHub Student Developer Pack.

In this step-by-step guide, you will learn how to setup a Spring Boot app with a PostgreSQL database using Docker and IntelliJ IDEA.

At the end of this guide, you would've learned about Docker, why it's used, and how to interact with a running Docker container via terminal. Also, you would've known about Spring Boot database connection using IntelliJ.

What is PostgreSQL?

PostgreSQL(or Postgres for short) is a relational database. It stores data points in rows, with columns as different data attributes.

Postgres isn’t just relational; it’s object-relational and supports complex structures and a breadth of built-in and user-defined data types. It provides extensive data capacity and is trusted for its data integrity.

What is Docker? & Why Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

With Docker, you can manage your infrastructure in the same ways you manage your applications.

Why? Docker provides a way out of the mess of version clashes, esoteric build failure messages, and missing dependency errors by reducing the task of installing and running software to as little as two commands (docker run and docker pull). Source

More on docker see Docker explained.

What is IntelliJ IDEA?

IntelliJ IDEA (or IntelliJ for short) is one of the most powerful and popular Integrated Development Environments (IDE) for Java.

IntelliJ is an IDE for Java; it also understands and provides intelligent coding assistance for various other languages such as SQL, JPQL, HTML, JavaScript, etc.

Demo: Using IntelliJ & Docker to setup a Springboot app with a PostgreSQL database.

To get the most out of this guide, you ensure a few requirements:

  • Basic understanding of Java & Maven
  • Docker installed on your machine(Installation guide here)
  • IntelliJ IDEA(Ultimate Edition) installed on your machine(Installation guide here)
  • PostgreSQL Client installed on your machine( Installation guide here)

Create a Postgres database on Docker

Open your terminal and run this command 👇🏾

$ docker run --name postgres-docker -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

What it does:

  • It pulls the Postgres Docker image from DockerHub,
  • sets the POSTGRES_PASSWORD environment variable value to postgres,
  • names (--name) the Docker container to be postgres-docker,
  • maps container’s internal 5432 port to external 5432 port, so we’ll be able to enter it from outside,
  • and enables us to run the Docker container in the background (-d). Source

To check if the database container is running 👇🏾

$ docker ps

To interact with your database via the terminal 👇🏾

$ docker exec -it postgres-docker bash

And with exec, we’ve entered the postgres-docker container in detached mode -it and started to run its bash app (bash).

As a result, we’ve entered the command line of the Docker container, so we can login to the database as a postgres user. Source

We can:

  • Login psql -U postgres
  • Create a table CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR);
  • Show tables in database \dt

Screenshot from 2021-03-16 20-38-22.png

If you aren't a fan of the terminal, you can use a GUI to interact with your database check this article for a list of great GUIs.

Speed up your working with Docker and Postgres with these cheatsheets: Docker, Postgres.

Start a Spring Boot app with PostgreSQL driver using IntelliJ

Create a new project using Spring Initializr on IntelliJ

Screenshot from 2021-03-16 19-56-44.png

Screenshot from 2021-03-17 03-25-53.png

Project settings details Artifact, Group, etc. Using Maven for dependency management.

Screenshot from 2021-03-17 03-26-16.png

  • Add Spring Web, PostgreSQL Driver, and Spring Data JPA dependencies.

Screenshot from 2021-03-17 03-26-54.png

Let Spring Initializr and IntelliJ do their work, and in a few minutes, your project is ready; now, let's connect to your running Postgres docker container.

Connect to your running database container with IntelliJ

The following steps are gotten from IntelliJ IDEA's docs and modified to fit this demo.

  1. In the Database tool window (View | Tool Windows | Database ), click the Data Source Properties icon.

  2. In the Data Sources and Drivers dialog, click the Add icon (+) and select PostgreSQL.

  3. At the bottom of the data source settings area, click the Download missing driver files link. As you click this link, IntelliJ IDEA downloads drivers that are required to interact with a database. To decrease the size of the installation package and keep driver versions up-to-date, drivers are not bundled with the IDE.

    You can specify your drivers for the data source if you do not want to download the provided drivers. For more information about creating a database connection with your driver, see Add a user driver to an existing connection.

  4. Set port to 5432 to communicate to your running docker container.

  5. From the Authentication list, select User & Password. Following this demo, your details are:

    • User: postgres
    • password: postgres
  6. Paste the JDBC URL jdbc:postgresql://localhost:5432/postgres in the URL field.

    To delete a password, right-click the Password field and select Set empty.

  7. To ensure that the connection to the data source is successful, click Test Connection.

Screenshot from 2021-03-24 16-03-03.png

One last thing, you have to configure the data source of the Spring Boot app with URL, username, and password of the Postgres database in your application.properties file.

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

Summary

In this post, you've been able to create a Postgres database on Docker.

Also, you've seen how to connect your running database container to IntelliJ and put everything together to run your Spring Boot with a database.

You can find the following resources useful:

Thank you for reading. I mostly write tutorials on the problems I have faced and whatever new tech I explore, so stay tuned for the next article.

If you found this article helpful, react and share. Cheers.

And you can follow me on Twitter.