You can use Config Vars which can be configured in the settings tab of your application at Heroku. The Config Vars will be exposed as environment variables to your application. Since Spring Boot can map environment variables to application properties you just need to set:
SPRING_DATASOURCE_URL
SPRING_DATASOURCE_USER
SPRING_DATASOURCE_PASSWORD
SPRING_DATASOURCE_DRIVER-CLASS-NAME
And they will be mapped to:
spring.datasource.url
spring.datasource.user
spring.datasource.password
spring.datasource.driver-class-name
Now all you have to do is to extract the relevant values. The complete data base configuration can be inspected at the heroku postgres management panel. Select the data base you want to connect to and you will see the values for SPRING_DATASOURCE_USER
and SPRING_DATASOURCE_PASSWORD
right away. The SPRING_DATASOURCE_URL
has to be constructed like this:
jdbc:postgresql://<Host>:<Port>/<Database>
Where <Host>
, <Port>
and <Database>
have to replaced by the corresponding values from the data base connection page. Last but not least the SPRING_DATASOURCE_DRIVER-CLASS-NAME
has to be set to org.postgresql.Driver
.
This way you can use the build in functionality of Spring Boot instead of adding environment specific configuration to your application. Note however, that Spring Boot has a specific order of reading external configuration. So you have to make sure there are no
- Command line arguments (passed via the Procfile)
- JNDI attributes from java:comp/env (don't know where those might be coming from in Heroku.)
- Java System properties (can also be passed via the Procfile as
-D
arguments)
since those would override the OS environment variables.