Skip to content

abhip-10/spring-boot-postgres

Repository files navigation

Spring Boot + PostgreSQL (Docker) Tutorial Project

This repository contains a tutorial project demonstrating how to build and run a simple RESTful Spring Boot application connected to a PostgreSQL database using Docker Compose.

It follows a learning-oriented approach inspired by educational resources such as Amigoscode’s Spring Boot with PostgreSQL tutorials. The project is intended for educational and practice purposes, showcasing clean setup, integration, and deployment patterns.


Features

  • Build REST APIs using Spring Boot 3
  • Connect and persist data in PostgreSQL
  • Run PostgreSQL via Docker Compose
  • Manage dependencies with Maven
  • Demonstrate clean architecture: Controller → Service → Repository → Database

Tech Stack

Component Technology
Language Java 21
Framework Spring Boot 3.x
Database PostgreSQL (Dockerized)
Build Tool Maven
Containerization Docker & Docker Compose
IDE IntelliJ IDEA

Prerequisites

Before running this project, make sure you have:


Setup Instructions

1. Clone the Repository

git clone https://github.com/abhip-10/spring-boot-postgres.git
cd spring-boot-postgres

2. Start PostgreSQL using Docker Compose

docker compose up -d

This starts a PostgreSQL container named postgres-spring-boot on port 5332 (mapped to internal port 5432).

3. Verify the Container

docker ps

You should see something like:

CONTAINER ID   IMAGE         PORTS
abc1234        postgres:latest   0.0.0.0:5332->5432/tcp

Spring Boot Configuration

src/main/resources/application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5332/postgres
spring.datasource.username=abhi
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Run the Application

Option 1: Using Maven

mvn spring-boot:run

Option 2: Build a JAR and Run

mvn clean package
java -jar target/app.jar

Once started, open your browser or API client at: http://localhost:8080

Example API Request

Endpoint: POST http://localhost:8080/api/v1/software-engineers

Request Body:

{
  "name": "Abhinav",
  "techStack": "Java, Spring Boot, Spring"
}

Response: HTTP 200 OK (no body, simple insert)

Docker Compose File

docker-compose.yml:

services:
  db:
    container_name: postgres-spring-boot
    image: postgres:latest
    environment:
      POSTGRES_USER: amigoscode
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - db:/data/postgres
    ports:
      - "5332:5432"
    networks:
      - db
    restart: unless-stopped

volumes:
  db:

networks:
  db:
    driver: bridge

Learning Objectives

Through this tutorial project, you’ll learn to:

  • Set up a full Spring Boot + PostgreSQL environment
  • Connect Spring Boot to a database via JDBC and JPA
  • Manage PostgreSQL containers using Docker Compose
  • Write and test REST endpoints
  • Build, package, and containerize a Java backend service

Project Structure

spring-boot-postgres/
│
├── src/
│   ├── main/
│   │   ├── java/com/abhi/...
│   │   │   ├── controller/     # REST controllers
│   │   │   ├── service/        # Business logic
│   │   │   ├── model/          # Entities
│   │   │   └── repository/     # Data access layer
│   │   └── resources/
│   │       ├── application.properties
│   │       └── data.sql (optional for init)
│
├── docker-compose.yml
├── Dockerfile
├── pom.xml
└── README.md

License

This project is licensed under the MIT License. You are free to use, modify, and distribute it for educational purposes.


About

Spring-Boot + Docker Tutorial Project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages