WL
Software Engineer
Wassim Lagnaoui

Tutorial 01: Project setup

Spring Boot + PostgreSQL setup for the Restaurant App series.

← Back to Tutorials

Project Bootstrapping: Spring Boot + PostgreSQL

This short guide helps you bootstrap the application: pick the right Spring dependencies, set up PostgreSQL, and configure application.yml so the app connects to the database successfully.


Step 1: Generate a Spring Boot project

Use Spring Initializr (start.spring.io) with:

  • Project: Maven, Language: Java, Spring Boot: 3.x, Java: 17
  • Dependencies:
    • Spring Web
    • Spring Data JPA
    • Validation
    • Spring Security (if you plan to add auth later)
    • PostgreSQL Driver
    • Lombok (optional but recommended)
    • Spring Boot DevTools (optional)

Download and unzip, or import directly into your IDE.

If you need to add dependencies manually

Ensure your pom.xml includes the following (only the relevant parts shown):

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
  <!-- Optional, useful in development -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>
</dependencies>

Step 2: Install and prepare PostgreSQL

Option A: Local install

  • macOS: brew install postgresql, then start the service.
  • Windows/Linux: install from the official site (or your package manager).

Option B: Docker (quickest)

docker run --name restaurant_postgres -e POSTGRES_PASSWORD=your_password -p 5432:5432 -d postgres:14

Create a database and user

-- inside psql (\c to connect), or any SQL client
CREATE DATABASE restaurant_db;
CREATE USER restaurant_user WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE restaurant_db TO restaurant_user;

Step 3: Configure Spring (YAML)

Put the base configuration in src/main/resources/application.yml and keep secrets in an optional application-local.yml that is imported by the base file.

application.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/restaurant_db
    username: restaurant_user
  config:
    import: optional:application-local.yml

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
jwt:
  expiration: 86400000 # (1 day in ms)

server:
  port: 8080

application-local.yml

spring:
  datasource:
    password: ${POSTGRES_PASSWORD:default_db_password}
jwt:
  secret: ${JWT_SECRET:default_jwt_secret}

Why this split?

  • application.yml is safe to commit and documents the connection details (without the password).
  • application-local.yml holds secrets and can read from environment variables; it’s optional and ignored in other environments.

Step 4: Run and verify

  1. Export environment variables (optional but recommended):
    export POSTGRES_PASSWORD=your_password
    export JWT_SECRET=dev_jwt_secret
    
  2. Start the app:
    ./mvnw spring-boot:run
    
  3. Watch the logs for a successful connection to PostgreSQL and Hibernate schema update messages.

Common issues

  • Connection refused: Check that PostgreSQL is running and that the URL/port are correct.
  • Auth errors: Ensure the DB user/password match what you created. If using Docker, the hostname in a multi-container setup may be db instead of localhost.
  • DDL auto-update: ddl-auto: update is for development. Plan migrations for production later.

That’s it. Your application is now bootstrapped with the right dependencies, a running PostgreSQL, and a working YAML configuration.