Heroku + SpringBoot Deployment example – How to Deploy SpringBoot + PostgreSQL on Heroku platform with Heroku CLI

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---feature-image

Heroku is a platform as a service (PaaS) that help us to build, run, and operate applications entirely in the cloud. So in the tutorial, we show you how to deploy SpringBoot web application with PostgreSQL in Heroku.

Related posts:
How to use Spring JPA with PostgreSQL | Spring Boot
How to Deploy Angular application on Heroku hosting with Git repository

Technologies

– Heroku
– Maven
– SpringBoot

Goal

We deploy a SpringBoot with PostgreSQL to Heroku platform:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---app+db

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---restapi-test-getall-customer-from-db

Practice

Setup Heroku CLI
Download & Install Heroku CLI

For detail go to the Download Heroku CLI

– Install Heroku CLI on Linux:

-> Details:

Login to Heroku

Use heroku login cmd to login:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---Heroku-Login-successfully

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---Heroku-Login

Add Heroku SSH keys

– To check your key was added, run heroku keys
– If your key isn’t there, you can add it manually by running heroku keys:add

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---Add-SSH-Keys

Deploy simple SpringBoot Web Application on Heroku
Create SpringBoot Project

Use SpringToolSuite to create a SpringBoot project.

– Add web dependency ->

– Create a simple RestAPI ->

Create a Git Repository

Go to Git Account, create a Git Repository:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---create-Git-Repository

– Add all of the code to Git by running these commands:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---add-code-to-git-repository-from-local

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---add-code-to-git-repository-from-local-UI

Create a new Heroku App

– Use the below commandline to create Heroku App:

heroku create

-> Details:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---create-heroku-app

-> It creates a new Heroku App with random name: stark-depths-44428

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---create-a-new-app-web

– We can rename the app using the cmd ->

$ heroku apps:rename --app oldname newname

-> Details:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---rename-heroku-app

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---web-app

Deployment

– Now deploy your code by commandline:

git push heroku master

-> Logs:

-> Due to the presence of a pom.xml file, Heroku automatically detects the application as a Maven/Java app.

Heroku installed Java 8 by default, but you can easily configure this with a system.properties. Details ->

– Create a file system.properties in your root folder of application.
– Set a property java.runtime.version=11 in the file. Accepted major version values are 1.7, 1.8, 9, 10, and 11.
– The Java default is 1.8

-> Visit the app’s URL by running this command: heroku open

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---deploy-successfully

SpringBoot Logs

– Use heroku logs --tail to see Logs ->

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---heroku-log

Deploy SpringBoot Web App with PostgreSQL on Heroku
Heroku Attach PostgreSQL Database

– Attach a PostgreSQL database to your application by cmd:

heroku addons:create heroku-postgresql

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---attach-postgresql

– List the configuration variables by cmd: heroku config & heroku pg

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---see-database-heroku-configuration

SpringBoot JPA Project

Modify above SpringBoot project as below structure:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---springboot-jpa-project

– Add JPA & PostgreSQL dependencies:

– Configure Spring JPA:

-> Open application.properties, add configuration:

– Create a Customer.java model:

– Create Spring JPA Repository Interface:

– Update RestAPIsController as below:

Deployment

– Add the change to Git Repository ->

-> Deploy code:

git push heroku master ->

– Results:

-> Logs Deployment:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---app+db

-> PostgreSQL database:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---postgresql-heroku

-> RestAPIs requests:

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---restapi-test-save-customer-to-db

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---restapi-test-getall-customer-from-db

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---restapi-test-find-a-customer

Deploy-SpringBoot-with-PostgreSQL-on-Heroku-hosting---restapi-test-get-customer-by-lastname

Sourcecode

SpringBootAPIs

By grokonez | December 14, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*