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.
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
PostgresDocker image from DockerHub,
- sets the
POSTGRES_PASSWORDenvironment variable value to
- names (
--name) the Docker container to be
- maps container’s internal
5432port to external
5432port, so we’ll be able to enter it from outside,
- and enables us to run the Docker container in the background (
To check if the database container is running 👇🏾
$ docker ps
To interact with your database via the terminal 👇🏾
$ docker exec -it postgres-docker bash
exec, we’ve entered the postgres-docker container in detached mode
-itand started to run its bash app (
As a result, we’ve entered the command line of the Docker container, so we can login to the database as a
psql -U postgres
- Create a table
CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR);
- Show tables in database
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.
Start a Spring Boot app with PostgreSQL driver using IntelliJ
Create a new project using Spring Initializr on IntelliJ
Project settings details
Group, etc. Using
Maven for dependency management.
PostgreSQL Driver, and
Spring Data JPAdependencies.
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
Connect to your running database container with IntelliJ
The following steps are gotten from IntelliJ IDEA's docs and modified to fit this demo.
In the Database tool window (View | Tool Windows | Database ), click the Data Source Properties icon.
In the Data Sources and Drivers dialog, click the Add icon (+) and select PostgreSQL.
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.
Set port to
5432to communicate to your running docker container.
From the Authentication list, select User & Password. Following this demo, your details are:
Paste the JDBC URL
jdbc:postgresql://localhost:5432/postgresin the URL field.
To delete a password, right-click the Password field and select Set empty.
To ensure that the connection to the data source is successful, click Test Connection.
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
spring.datasource.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/postgres spring.datasource.username=postgres spring.datasource.password=postgres
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:
- Running Spring Boot with PostgreSQL in Docker Compose
- Docker: Zero to Hero (with Spring Boot + Postgres)
- Simple Rest API With Spring Boot, Postgres, and Docker
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.