How to Set Up a Spring Boot Project with PostgreSQL: A Step-by-Step Guide

This post links to: Building a Spring Boot CRUD App With Postgres from Scratch: The Complete Guide.

This guide will show you how to set up a Spring Boot project from scratch and then set up and connect to a PostgreSQL database on your local machine.

Follow these Java/Spring Boot prerequisites before starting.

1. Creating the project with Spring Initializr

Firstly, we will need to navigate to Spring Initializr.

Spring Initializr is a tool where we can specify project requirements like what version of Java and Spring Boot we want to use, any metadata about our project and any Spring dependencies we want to bring into this project.

Once configured, the "GENERATE" button will output a basic Spring Boot project scaffold into a ZIP file, which we can use to start creating our CRUD application.

For this project, we will need the following configuration set:

spring-init.png

After clicking "GENERATE", a ZIP file should be downloaded.

You can now open the ZIP in whatever integrated development environment (IDE) or editor of your choice. I'll be using IntelliJ.

2. Project Structure

Once the ZIP file has been opened, you should see a similar structure to below:

.
├── HELP.md
├── README.md
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java/full/bearded/dev/crud/app
    │   │                               └── Application.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java/full/bearded/dev/crud/app
                                        └── ApplicationTests.java

Gradle

Spring Boot & Java

Application.java

This is where our application will run from:

// Application.java
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Resources

ApplicationTests.java

This is an integration test using the @SpringBootTest annotation. This will bring up the Spring ApplicationContext so we can test our application loads and test any dependencies between components. This could include test cases like: can we connect to a database, or does our app return the correct info when we query it via an API.

@SpringBootTest
class ApplicationTests {

	@Test
	void contextLoads() {
	}

}

3. Install & Run PostgreSQL

We will also need a PostgreSQL database running locally. We can do this using Docker which you can install following the Java/Spring Boot prerequisites Docker section.

Once you have Docker installed, you can run a PostgreSQL instance using the following command in the terminal:

docker run --name postgres \
  -e POSTGRES_USER=dev \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DB=test_db \
  -p 5432:5432 \
  -d postgres

You can verify Postgres is running using the following command in the terminal:

docker ps

If Postgres is up and running, you should see a similar output:

CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                                         NAMES
38d318ace277   postgres   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp   postgres

4. Configure Spring to Use PostgreSQL

Now we should have our initialised Spring Boot app and local version of Postgres running in Docker. The next part is to connect our application to our database.

We can do that by configuring the following options in the application.yml file:

# application.yml
spring:
  application:
    name: crud.app
  datasource:
    url: jdbc:postgresql://localhost:5432/test_db
    username: dev
    password: password
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

5. Run the App

The last step is to run our application to check that it can connect to our Postgres instance.

To run the application you can do the following in the terminal:

./gradlew bootRun
# or if using Maven:
./mvnw spring-boot:run

You should see an output that resembles this:

Tomcat started on port(s): 8080
Started Application in 2.5 seconds

If so your app is running and connected to Postgres!

What's Next?

So to recap we:

GitHub Example

Up Next: Creating Your First JPA Entity and Repository in Spring Boot!