How can I provide different database configurations with Spring Boot?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Spring Boot, “different database configurations” can mean two different problems. Sometimes you want one datasource that changes by environment, such as dev, test, and prod. Other times you want multiple datasources active in the same application.
Those are different designs and should not be mixed casually. The most common and simplest solution is environment-specific configuration using profiles.
Use Profiles for Different Environments
If the application uses one database at a time but the connection details differ by environment, use Spring profiles with separate property files.
Base configuration:
Development settings:
Production settings:
Then activate the profile:
This is the standard Spring Boot answer when you need different connection values per environment.
Why Profiles Are Usually the Best First Choice
Profiles keep one application design with one datasource bean, while allowing environment-specific details to change cleanly. That is much easier to maintain than building custom datasource selection logic when all you really need is different URLs and credentials.
For tests, you can also have:
That gives you lightweight test isolation without affecting dev or prod.
Environment Variables and External Config
Profiles are useful, but hardcoding secrets is not. In practice, many teams combine profiles with environment variables or externalized config values.
For example:
Now the profile can still control other behavior, while deployment-specific credentials come from the environment.
This is usually a better production strategy than committing passwords into profile files.
When You Actually Need Multiple Datasources
If the application must connect to two databases at the same time, profiles are not enough. You need multiple datasource beans.
Example:
And configuration:
This is a different design from environment switching. Do not introduce multiple datasources unless the application truly needs them simultaneously.
Use Profiles With Multiple Datasources Carefully
You can combine both ideas. For example, the app may always have a primary and reporting datasource, but each one needs different values in dev and prod.
That works, but it increases complexity quickly, so it is worth keeping the property structure clear and predictable.
The general rule is:
- use profiles for environment differences
- use multiple datasource beans only for true multi-database application architecture
A Practical Recommendation
For most projects:
- start with
application.yml - move environment-specific values into
application-dev.yml,application-test.yml, andapplication-prod.yml - keep passwords and sensitive values in environment variables or secret stores
- only add multiple datasource beans if the application genuinely needs more than one live connection target
That avoids unnecessary complexity while staying aligned with normal Spring Boot conventions.
Common Pitfalls
One common mistake is creating custom Java config for environment switching when Spring profiles already solve the problem.
Another mistake is copying entire datasource blocks across profile files without understanding which values actually differ. That creates drift and makes updates harder.
It is also easy to confuse “different database configurations” with “multiple datasources.” Those are not the same requirement.
Finally, do not store production credentials directly in committed config files. Externalize secrets even if the rest of the configuration lives in YAML.
Summary
- Use Spring profiles when the app needs one datasource with different settings per environment.
- Use separate property files such as
application-dev.ymlandapplication-prod.yml. - Externalize sensitive values with environment variables or secret management.
- Use multiple datasource beans only when the app truly connects to more than one database at the same time.
- Keep the design simple unless the architecture genuinely requires extra configuration layers.
Related reading
- How can I put a database under git version control?
- How can I query for null values in entity framework?
- How can I rename a database column in a Ruby on Rails migration?
- How can I rename column in laravel using migration?
- How can I read a large text file line by line using Java?
- How can I read all files in a folder from Java?
- How can I replicate between two tables with different names?
- How can I retrieve Id of inserted entity using Entity framework?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.