To connect a PostgreSQL database in a Spring Boot application, follow these steps:
✅ 1. Add PostgreSQL Driver Dependency
For Maven:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>For Gradle:
implementation 'org.postgresql:postgresql:42.7.1'✅ 2. Configure application.properties or application.yml
Option A: application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/devdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect✅ 3. Test the Connection
-
Start your Docker container with Postgres running.
-
Launch the Spring Boot app.
-
Look for this log line:
HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect -
If there’s a connection issue, Spring Boot will throw an exception on startup.
Let me know if you want to use connection pooling (e.g. HikariCP, which is default), Flyway/Liquibase migrations, or Spring Data JPA.