Spring Boot and JDBC for PostgreSQL on Heroku - QuickStart

Shorter Version of the main tutorial

Author       : Rajdeep dua
Last Updated : April 14, 2016

This article shows how to run a Spring Boot app with PostgreSQL JDBC driver which connects to the PostgreSQL database. Runtime environment will be local as well as Heroku.

../../_images/spring-boot-psql.png

Figure 1 : Heroku Spring Boot Application Data flow

Figure 1 show the how Spring Boot App interacts with Heroku PostgreSQL the Http Client. App runs inside a Dyno and talkes to the PostgreSQL Add-On.

Note

Make sure you have Java 1.8, Maven 3.3.3 installed, PostgreSQL 9.4.4 or above and Heroku Toolbelt.

We will be using a Spring Boot Sample app available at heroku spring boot psql.

Clone the Source Code

Clone the source code using git clone and change into the cloned project directory heroku-spring-boot-jdbc-psql

$ git clone https://github.com/rajdeepd/heroku-spring-boot-jdbc-psql
$ cd heroku-spring-boot-jdbc-psql

Compile the App Locally

Execute the following commands to compile and build the jar file

$ mvn compile
$ mvn package

Procfile

There is already a Procfile which tells the Heroku what kind of Dyno is required and the source for the application.

web: java -Dserver.port=$PORT -jar target/heroku-spring-boot-psql-0.0.1-SNAPSHOT.jar

Deploying to Heroku

$ heroku create
$ git push heroku master

The App is deployed but table needs to be created in PostgreSQL.

$ heroku pg:psql

---> Connecting to DATABASE_URL

psql (9.4.4, server 9.4.6)
SSL connection (protocol: TLSv1.2,... compression: off)
Type "help" for help.

Execute the following command on psql prompt

create table cuser
(
     id SERIAL PRIMARY KEY not null,
     first varchar(100),
     last varchar (100),
     email varchar (100),
     company varchar (150),
     city varchar(100)
);

Output will be similar to the listing below

$ rd-heroku-spring-boot-psql::DATABASE=> create table cuser
rd-heroku-spring-boot-psql::DATABASE-> (
rd-heroku-spring-boot-psql::DATABASE(>      id SERIAL PRIMARY KEY not null,
rd-heroku-spring-boot-psql::DATABASE(>      first varchar(100),
rd-heroku-spring-boot-psql::DATABASE(>      last varchar (100),
rd-heroku-spring-boot-psql::DATABASE(>      email varchar (100),
rd-heroku-spring-boot-psql::DATABASE(>      company varchar (150),
rd-heroku-spring-boot-psql::DATABASE(>      city varchar(100)
rd-heroku-spring-boot-psql::DATABASE(> );

Open the Remote App in Heroku

$ heroku open

The App opens up in the default route / with the following view

../../_images/spring-boot-psql-remote-home.png

Show Users

Browse to URL http://{your-app-name}.herokuapp.com/users to see the list of user names.

../../_images/spring-boot-psql-remote-users.png

Create a User

Browse to URL http://{your-app-name}.herokuapp.com/createuserform to see the list of user names.

../../_images/spring-boot-psql-remote-create-user.png

Summary

In this tutorial we learnt how to configure a Pre-existing Spring Boot Application to work with Heroku. We used PostgreSQL JDBC driver for talking to the PostgreSQL database deployed on Heroku.