Skip to content

[Homework 5] PostgreSQL Setup and Spring Boot Connection #96

@yeagerereren

Description

@yeagerereren

🎯 Purpose: Connect the Spring Boot application to a real PostgreSQL
database.

1️⃣ Install PostgreSQL

  1. Download from postgresql.org/download.
  2. During installation: User: postgres Password: postgres Database: user
  3. Verify that the database is created using pgAdmin.

2️⃣ Configure the project

  1. Add the following to src/main/resources/application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/test_database
spring.datasource.username=postgres 
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=validate 
spring.jpa.show-sql=true
  1. Ensure that the following dependencies exist in pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

3️⃣ Test the connection

  1. Add a simple REST endpoint:
@RestController
class DbTestController {
  @Autowired DataSource ds;

  @GetMapping("/api/test/db")
  String test() throws Exception {
    try (var c = ds.getConnection()) {
      return "Connected to: " + c.getMetaData().getURL();
    }
  }
}
  1. Run the application and go to http://localhost:8080/api/test/db.
    ✅ Ifyou see “Connected to: jdbc:postgresql://localhost:5432/test_database”, the
    connection is successful.

Acceptance Criteria:

  • PostgreSQL installed and running.

  • Application connects successfully.

  • REST endpoint confirms DB connection.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions