I very recently started learning Java and spring boot and I was following a youtube tutorial on how to build a small project, but when it got to the point of checking the connection to the database i got this message:
customer=# \dt
Did not find any relations.
And because i'm very much unfamilar with java and spring boot, i find myself debugging with no clear workflow. If anyone could guide me in the right direction I'd very much appreciate it.
Here is my code:
Main.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Objects;
@SpringBootApplication
@RestController
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
    @GetMapping("/")
    public GreetingResponse greeting(){
        GreetingResponse greetingString = new GreetingResponse(
                "Hello",
                List.of("Python", "Python", "Python"),
                new Person("Batool", 28, 500_000)
        );
        return greetingString;
    }
    record Person(String name, int age, double savings){
    }
    record GreetingResponse(
            String greeting,
            List<String> favProgrammingLanguages,
            Person person
    ){
    }
Customer.java
import java.util.Objects;
import jakarta.persistence.*;
@Entity
public class Customer {
    @Id
    @SequenceGenerator(name = "customer_id_sequence", sequenceName = "customer_id_sequence")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_id_sequence")
    private Integer id;
    private String name;
    private String email;
    private Integer age;
    public Customer(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public Integer getAge() {
        return age;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Customer customer = (Customer) o;
        return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age);
    }
    @Override
    public int hashCode() {
        return Objects.hash(id, name, email, age);
    }
    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}
docker-compose.yml
services:
  db:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: <>
      POSTGRES_PASSWORD: <>
      PGDATA: /data/postgres
    volumes:
      - db:/data/postgres
    ports:
      - "5332:5432"
    networks:
      - db
    restart: unless-stopped
networks:
  db:
    driver: bridge
volumes:
  db:
application.yml
server:
  port: 8080
spring:
  datasource:
    platform: postgres
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://db:5332/customer
    username: <>
    password: <>
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format-sql: true
      show-sql: true
    main:
      web-development-type: servlet
                You are telling Spring to connect to a database server with a hostname of db. But that db hostname will only work from within the docker bridge network created inside docker-compose. If you were running the Spring Boot application as another service inside the docker-compose file, then that address would work. But you don't appear to be doing that.
You most likely need to change the datasource URL in the Spring config file to this:
url: jdbc:postgresql://localhost:5332/customer
Because this line in your docker-compose file is exposing the database service on port 5332 on your local computer, or localhost:
    ports:
      - "5332:5432"
                        The jdbc connection has to find the schema name, and because an error that eventually bubbles up thru spring, to the application. This connects:
worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With