LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 2119 min read

Connecting to MySQL/PostgreSQL

Configure datasource properties and driver dependencies to connect Spring Boot to MySQL or PostgreSQL, and verify the connection works.

Introduction

You already connected to a database briefly back in Lesson 16 to get Spring Data JPA working end-to-end. This lesson goes deeper: the exact driver dependency and properties needed for both MySQL and PostgreSQL, how to confirm the connection is genuinely working, and how Spring Boot's default connection pool, HikariCP, fits into the picture.

What You Will Learn
  • Which driver dependency each database needs.
  • The datasource properties for MySQL and for PostgreSQL side by side.
  • How to verify your application actually connected successfully.
  • What HikariCP is and why Spring Boot uses it by default.

The Driver Dependency

Spring Boot's data JPA starter provides the JPA/Hibernate layer, but it does not include a specific database driver — you choose that separately, based on which database you're running.

<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

Only include the driver for the database you're actually using. The runtime scope means the driver is available when the application runs but isn't needed to compile your own code, since you interact with the database through JPA rather than the driver's API directly.

Datasource Properties for MySQL

spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Datasource Properties for PostgreSQL

spring.datasource.url=jdbc:postgresql://localhost:5432/studentdb
spring.datasource.username=postgres
spring.datasource.password=your_password
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Notice how similar the two blocks are — only the URL format, driver class, username convention, and dialect differ. The rest of your application code, including every @Entity and repository you've written, needs zero changes to switch between the two databases.

Verifying the Connection

The most reliable way to confirm a real connection is to start the application and watch the console. A successful connection shows HikariCP starting a pool and, if show-sql is enabled, any DDL statements Hibernate runs against the real schema.

Successful Startup Log

Click Run to see what this code prints.

Failed Connection Log

Click Run to see what this code prints.

Connection Pooling with HikariCP

Opening a brand-new database connection for every single request is slow and wasteful. Spring Boot automatically configures HikariCP, a fast, lightweight connection pool, as soon as it detects a datasource on the classpath — no extra dependency or configuration is required to get pooling. HikariCP keeps a set of open connections ready to reuse, which is why Spring Boot applications handle database-backed requests efficiently without any manual connection management in your code.

spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.connection-timeout=30000
Default Settings Are Usually Fine

HikariCP's defaults work well for most applications, including this course's examples. Tuning pool size and timeouts is something to revisit only once you have real load and metrics to justify a change, not something to guess at upfront.

Common Mistakes

Avoid These Mistakes
  • Forgetting to create the target database itself first — Spring Boot connects to an existing database by name, it does not create the database for you.
  • Mismatching the driver dependency with the datasource URL, such as leaving a MySQL driver in place while pointing the URL at a PostgreSQL instance.
  • Hardcoding real credentials directly in application.properties and committing them to version control.
  • Leaving out serverTimezone or useSSL parameters on older MySQL versions, which can cause obscure connection errors depending on server configuration.

Best Practices

  • Keep only the driver dependency you actually need in pom.xml — including both MySQL and PostgreSQL drivers unnecessarily adds bulk with no benefit.
  • Externalize credentials using environment variables (${DB_PASSWORD}) rather than plain text, especially before any deployment.
  • Watch the startup console output the first time you connect to a new database — connection issues surface immediately and clearly there.
  • Let Spring Boot's default HikariCP configuration stand until you have a concrete, measured reason to tune it.

Frequently Asked Questions

No. As soon as spring-boot-starter-data-jpa and a JDBC driver are on the classpath, Spring Boot auto-configures a HikariCP-backed DataSource with sensible defaults, with no extra setup required.

Yes, for standard JPA usage. Swap the driver dependency and update the datasource properties; your @Entity classes and repositories stay exactly the same, since JPA abstracts away the database-specific SQL dialect.

No. You must create the target database (e.g. studentdb) yourself beforehand, using a tool like MySQL Workbench, psql, or a CREATE DATABASE statement. Spring Boot and Hibernate only manage tables inside that existing database, controlled by ddl-auto.

It tells Hibernate which SQL dialect to generate — different databases have slightly different SQL syntax for things like pagination or auto-increment columns. In most modern Spring Boot versions Hibernate can auto-detect this correctly, but setting it explicitly removes any ambiguity.

Key Takeaways

  • Each database needs its own driver dependency: mysql-connector-j for MySQL, postgresql for PostgreSQL.
  • Datasource properties differ mainly in URL format, driver class name, and dialect — your JPA code stays unchanged.
  • The startup console log is the fastest way to confirm whether a database connection succeeded or failed.
  • Spring Boot auto-configures HikariCP connection pooling with no extra setup required.

Summary

You can now connect a Spring Boot application to either MySQL or PostgreSQL confidently, and verify the connection through the startup logs. In the final lesson of this batch, you'll learn how Spring Boot profiles let you keep separate configuration for development and production, and how to switch between them.

Next Lesson →

Spring Boot Profiles