Simplest cleanest way for Spring Boot 2.x with Heroku & Postgres
I read all answers, but didn′t find what Jonik was looking for:
I'm looking for the simplest, cleanest way of connecting to Heroku
Postgres in a Spring Boot app using JPA/Hibernate
The development process most people want to use with Spring Boot & Heroku includes a local H2 in-memory database for testing & fast development cycles - and the Heroku Postgres database for staging and production on Heroku.
- First thing is - you don′t need to use Spring profiles for that!
- Second: You don′t need to write/change any code!
Let′s have a look on what we have to do step by step. I have a example project in place that provides a fully working Heroku deployment and configuration for Postgres - only for the sake of completeness, if you want to test it yourself: github.com/jonashackt/spring-boot-vuejs.
The pom.xml
We need the following depencencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- In-Memory database used for local development & testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Switch back from Spring Boot 2.x standard HikariCP to Tomcat JDBC,
configured later in Heroku (see https://stackoverflow.com/a/49970142/4964553) -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<!-- PostgreSQL used in Staging and Production environment, e.g. on Heroku -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
One tricky thing here is the usage of tomcat-jdbc
, but we′ll cover that in a second.
Configure Environment Variables on Heroku
In Heroku Environment Variables are named Config Vars
. You heard right, all we have to do is to configure Enviroment Variables! We just need the correct ones. Therefore head over to https://data.heroku.com/ (I assume there′s already a Postgres database configured for your Heroku app, which is the default behavior).
Now click on your application′s corresponding Datastore
and switch over to the Settings
tab. Then click on View Credentials...
, which should look something similar like this:
Now open a new browser tab and go to your Heroku application′s Settings
tab also. Click on Reveal Config Vars
and create the following Environment Variables:
SPRING_DATASOURCE_URL
= jdbc:postgresql://YourPostgresHerokuHostNameHere:5432/YourPostgresHerokuDatabaseNameHere (mind the leading jdbc:
and the ql
addition to postgres
!)
SPRING_DATASOURCE_USERNAME
= YourPostgresHerokuUserNameHere
SPRING_DATASOURCE_PASSWORD
= YourPostgresHerokuPasswordHere
SPRING_DATASOURCE_DRIVER-CLASS-NAME
= org.postgresql.Driver
(this isn′t always needed since Spring Boot can deduce it for most databases from the url, just for completeness here)
SPRING_JPA_DATABASE-PLATFORM
= org.hibernate.dialect.PostgreSQLDialect
SPRING_DATASOURCE_TYPE
= org.apache.tomcat.jdbc.pool.DataSource
SPRING_JPA_HIBERNATE_DDL-AUTO
= update
(this will automatically create your tables according to your JPA entities, which is really great - since you don′t need to hurdle with CREATE
SQL statements or DDL files)
In Heroku this should look like this:
Now that′s all you have to do! Your Heroku app is restarted every time you change a Config Variable - so your App should now run H2 locally, and should be ready connected with PostgreSQL when deployed on Heroku.
Just if you′re asking: Why do we configure Tomcat JDBC instead of Hikari
As you might noticed, we added the tomcat-jdbc
dependency to our pom.xml and configured SPRING_DATASOURCE_TYPE=org.apache.tomcat.jdbc.pool.DataSource
as a Environment variable. There′s only a slight hint in the docs about this saying
You can bypass that algorithm completely and specify the connection
pool to use by setting the spring.datasource.type property. This is
especially important if you run your application in a Tomcat container, ...
There are several reasons I switched back to Tomcat pooling DataSource instead of using the Spring Boot 2.x standard HikariCP. As I already explained here, if you don′t specifiy spring.datasource.url
, Spring will try to autowire the embedded im-memory H2 database instead of our PostgreSQL one. And the problem with Hikari is, that it only supports spring.datasource.jdbc-url
.
Second, if I try to use the Heroku configuration as shown for Hikari (so leaving out SPRING_DATASOURCE_TYPE
and changing SPRING_DATASOURCE_URL
to SPRING_DATASOURCE_JDBC-URL
) I run into the following Exception:
Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
So I didn′t get Spring Boot 2.x working on Heroku & Postgres with HikariCP, but with Tomcat JDBC - and I also don′t want to brake my development process containing a local H2 database described upfront. Remember: We were looking for the simplest, cleanest way of connecting to Heroku Postgres in a Spring Boot app using JPA/Hibernate!