Skip to content
Advertisement

Spring boot won’t connect to Postgres database

I am trying to learn Spring Boot with a tutorial. I have the code and the database, but I keep getting a Hibernate ERROR whenever I try to connect the two…

This is the Error I get:

 :: Spring Boot ::                (v2.7.2)


2022-08-04 19:23:59.263  INFO 13108 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1746 ms
2022-08-04 19:23:59.463  INFO 13108 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-08-04 19:24:00.560 ERROR 13108 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

I have the dependency set up in my pom.xml:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

The StudentService

    @Service
public class StudentService {
    public List<Student> getStudents() {
        return List.of(
                new Student(
                        1L,
                        "Mariam",
                        "mariam.jamal@gmail.com",
                        LocalDate.of(2000, Month.JANUARY, 5),
                        21
                )
        );
    }
}

And the StudentController class

    @RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {

    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping
    public List<Student> getStudents() {
        return studentService.getStudents();
    }
}

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/student
spring.datasource.username=postgres
spring.datasouce.password=password
spring.jpa.hibernat.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

Unfortunately I have no idea what’s the problem or what I can do to improve it. I googled, but only found “the password not filled in” (which is not the case) and “the application.properties not being in the resources folder” (which they are…)

Thank you so much already for looking through it and if I can clarify anything, I am happy to do so.

Edit: sorry for posting it as pictures I replaced everything but the error code if I should change that too just tell me

If I am correct, the version of the PostgreSQL is 14.4 the text from the pg_hba.config:

# PostgreSQL Client Authentication Configuration File
# ===================================================
#   


# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

Advertisement

Answer

I fixed it by now. The problem was that I wrote hibernat instead of hibernate in the application.properties…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement