Getting started on web application with a Postgres database on Heroku

Getting started on web application with a Postgres database on Heroku

Introduction to using Heroku PostgreSQL with Heroku CLI

Heroku has three managed data services that can handle the database on your application. These are Heroku Postgres, Heroku Redis and Apache Kafka on Heroku. These services have a price range that varies from free to paid, depending on which pricing options you choose.

In this article, we are going to go through step by step, how to deploy a web application and then attach a PostgreSQL database to it.

First, let us understand what is Heroku, what is PostgreSQL, and how these two can work together to make your application database smooth, efficient, and easy to work with.

So...

What is Heroku?

Heroku is a platform as a service (When you purchase/rent resources from cloud providers,use them over the internet, and pay according to how much you use) company that provides developers with a faster and efficient way to create, deploy, scale, and manages applications in the cloud. Heroku supports multiple languages such as Ruby, Python, Scala, Node.js, PHP, Go, Java, and Clojure. To make PaaS even better, Heroku follows three principles...

  • Developer Centric. This is, Heroku keeps developers at its center. It is made by developers, for developers

  • App Centric. It focuses on ways to get the application in front of the user, faster and hassle-free.

  • Production Centric. Heroku focuses on every step of the application life-cycle, from build to deployment, and scaling. It ensures that every step in between is easy and efficient.

Cool, right?

And, PostgreSQL?

Well, also known as Postgres, this is a powerful open-source object-relational management system. It is best-known for its commitment to data integrity, reliability, and extensibility. It uses an extent of SQL language and many other powerful features to store and work with complicated workloads.

What is Heroku Postgres?

Heroku Postgres is a fully managed database backend service for developers. It provides nine cool features and tools, all of them being forks, followers, dataclips, trusted data integration, continuous protection, straightforward rollbacks, automated health checks, high availability, security compliance, data encryption, and Heroku Postgress via mutual LTS. You can find more information about these tools here

Prerequistes

  • Heroku account. You can sign up here

  • Heroku CLI. You can download it here

  • Git. You can download it here.

  • Github account. You can sign up here.

  • And and a web application on your local repository you wish to deploy to Heroku. I will be using a java web app for this demonstration.

  • I will be using Ubuntu 18.04 LTS as my operating system.

Ok. Ready?

Step 1: First off, login into Heroku with your login credentials.

$ heroku login -i

Step 2: cd into the local repository of your choice. The repository must contain your web application.

$ cd /Downloads/sampleapp/

Step 3: Install the necessary dependencies for the app

Because I am running a java app, I will run...

$ mvn clean install

Step 4: Test the web application locally.

You can test the application locally on Heroku with

$ heroku local web

Step 5: Create your application on Heroku

$ heroku create

Step 6: Push our app to Heroku

$ git push heroku master

If that does not work out for you, try..

$ git push heroku main

Step 7: Get detailed information about your deployment

$ heroku pg:info

Step 8: Create a Postgres database for your app

$ heroku addons:create postgres

Step 9: Go to Heroku dashboard to view your database credentials

$ heroku open

This is the Heroku dashboard. Click on the name of your application app1.png

Click on the Heroku Postgres addon that you just created app2.png

Go to settings

app3.png

Click on view credentials button app4.png

View your database credentials app5.png

You can use these credits to login onto your local PostgreSQL to connect your local database and your Heroku database.

Step 9: Write your SQL queries using Heroku CLI

$ heroku pg:psql

Step 10: When you are done querying, exit the psql platform

$ \q

To learn more about the Heroku platform, you can experiment more with the free resources. learn more on this link. To learn more about PostgreSQL, click this link. And if you are curious about Heroku Postgres, do not worry here is a full Heroku Postgres documentation.

Did you find this article valuable?

Support Thandiwe Khalaki by becoming a sponsor. Any amount is appreciated!