Last updated: 2024-05-02

  1. Learn
  2. Next Steps
  3. Database Configuration

Configuring the database connection of your Spring Boot app

In order to configure our database connection, we can simply edit the application.yml or application.properties respectivly. In the example below the settings are preferrably read from an environment variables (the part before the first colon, e.g. JDBC_DATABASE_URL). This can be very useful for deploying the app, as you don't have to save the credentials in your git repository. For our connection on localhost during development we can adjust the fallback behind the colon.

Adjusting our database connection

Adjusting our database connection

Using the local profile

If there are multiple developers working on the same project, we probably don't want to commit our local database settings to the version control. In the run configuration in IntelliJ we already entered -Dspring.profiles.active=local and thus the profile "local" is automatically active. Therefore we can also store our database settings in a new file application-local.yml.

Adding application-local.yml

Adding application-local.yml

This file is automatically picked up by Spring Boot when the application starts with the corresponding profile. Since this file is listed in .gitignore, it is not added to git and can be edited separately by each developer. Instead we may add another file application-local.example and add it to our repository, ready to be copied and customized.

Further readings

Connecting Spring Boot to a database
Profiles in Spring Boot