top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Deploying a Spring Boot Application to Heroku with PostgreSQL


In this article, let’s see how to deploy a simple Spring Boot Application on Heroku using the Heroku Postgres add- on to connect the application to the database.


What is Heroku?

Heroku is a cloud platform as a service (PaaS) that lets developers to build, run, monitor and operate applications entirely in the cloud. Heroku is useful in deploying and managing the application in the cloud. It supports several programming languages.


What is Deployment?

Application Deployment or Software Deployment is the process of installing, configuring, updating and enabling one application or suite of applications that make a software system available for use, like facilitating a certain URL on a server.


Prerequisites to deploy the app on Heroku :

List of prerequisites to deploy the Spring Boot Application on Heroku.

  • Heroku Account

  • Heroku CLI

  • GitHub


Steps to deploy Spring Boot Application to Heroku :

Let’s see in detail the step by step instructions on how to deploy

Step 1 : Create a Heroku Account.

Step 2 : Download and Install the Heroku CLI Installer.

Step 3 : Login to Heroku using Heroku CLI with the following command

heroku login

You will be prompted to enter your Heroku email and password. Enter the credentials and you will be logged in to Heroku. 


Step 4 : Create a Simple Spring Boot Application using CRUD operations.


In the application.yml file under resources folder, add the following configurations to connect the Spring Boot application to a PostgreSQL database instance using the specified credentials from Heroku.

spring: 
    datasource:      
  	  url:jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}      
       username: ${DB_USERNAME}     
       password: ${DB_PASSWORD}

Create system.properties file at the root of the project and add the following line of code and specify the version of Java that you are using.

java.runtime.version=17
maven.version=3.9.6

Package the application in a JAR file using mvn package -DskipTests.

Step 5 : Create an account in GitHub and push the project to the GitHub repository. Also set up git on your machine by configuring your username and email address in command prompt.


Step 6 : Login to Heroku website and Click the Create new app button on the dashboard. You can also create the app from Heroku CLI by using the following command. This will create an app with a randomly generated name.

heroku create




Once you click on the Create new app button on the dashboard, you will be prompted to enter the App name. Enter the app name in lower case and Click Create app button.

Step 7 : The app is created and now navigate to the Resources  tab to connect the PostgreSQL database to the app that we just created. In the Add-ons search box field, type Heroku Postgres and select it, the pop up will let you choose a pricing plan for the database. Choose the Essential 0 Plan name and Submit the Order Form. And just like that we created a free PostgreSQL database.


Heroku Postgres is a managed SQL database service provided directly by Heroku. It is a reliable and powerful database as a service based on PostgreSQL.



Step 8 : Now that the Heroku Postgres is added. Click on the Heroku Postgres add-on in the Resources tab of the app and navigate to the Settings tab of the Heroku Postgres to find the credentials and the connection URL for the PostgreSQL database. Click on View Credentials button in the Database Credentials section to see the credentials of the PostgreSQL database. 

We can use these credentials to connect to the PostgreSQL database from any PostgreSQL Client like psql .


Step 9 : Navigate to the app’s Settings tab and click on Reveal Config Vars button in the Config Vars Section. This is where we can manage the Heroku’s environment variables (also called Config Vars). In the key textbox field, add the environment variables that are configured in the Spring Boot application’s application.yml file and assign the corresponding database credentials of the PostgreSQL database to these variables in the value textbox field.


Step 10 : To configure GitHub integration, we have to authenticate with GitHub. By navigating to the app’s Deploy tab in Heroku Dashboard, we can configure GitHub integration. In the Deployment method section, Choose GitHub.

If prompted, Enter the credentials to login/connect to GitHub account to enable the code deploys. 


In the connect to GitHub section, once you are connected to the GitHub account, your username gets displayed and you can start searching for the repository to which you pushed your project/application to and Click on the Connect button next to your repository.


Step 11 : With Manual Deploys, we can create an immediate deployment of any branch from the GitHub repository that is connected to the app. We can control when changes are deployed to heroku.


With Automatic Deploys, Heroku builds and deploys all pushes to that branch.


I will be deploying the app Manually. In the Manual Deploy Section, Choose the branch that you want to deploy and Hit the Deploy Branch button. 

Now, the app is successfully deployed. Click View button to see the deployed app.


Note : To interact/connect to the Heroku Postgres using the psql command line tool, use the Heroku CLI credential from the database credentials. session. It will get connected to the Heroku Postgres database. Here we can run the SQL queries for creating tables, inserting data and retrieving the data from the table directly against the Heroku Postgres database.


I hope this step-by-step guide has been helpful in getting the Spring Boot application up and running on Heroku. 

36 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page