Heroku PostgreSQL

PostgreSQL is one of the most popular open-source relational database systems. Millions of companies use it as a fast, functional, and powerful transactional data store. Heroku provides production grade PostgreSQL databases as a service. It can be used by any language and framework, and it is very easy to connect your Java App to it.

First, in order to provision the database, you need to add it as an add-on to your Heroku app. This can be easily done using the Heroku CLI; open the terminal at the root directory of your Java project (in IntelliJ) and type the following command:

heroku addons:create heroku-postgresql

If all goes well, you will get a response similar to this:

Creating heroku-postgresql on ⬢ course-search-api-421... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-flat-96112 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

Heroku provides convenient access to the credentials and location of your database. In the terminal, type in the following command:

heroku pg:credentials:url DATABASE

You should get an output similar to this:

Connection information for default credential.
Connection info string:
   "dbname=d622k8geeonk8d host=ec2-52-6-178-202.compute-1.amazonaws.com port=5432 user=mdpjgqiddmasvi password=165039099f167946d2da221f58d0e8b222c4cddf38b40f5bace4c0d7993515ef sslmode=require"
Connection URL:
   postgres://mdpjgqiddmasvi:165039099f167946d2da221f58d0e8b222c4cddf38b40f5bace4c0d7993515ef@ec2-52-6-178-202.compute-1.amazonaws.com:5432/d622k8geeonk8d

Note the username and password in the above snippet are fake!

You need to store the Connection URL as an environment variable called DATABASE_URL in IntelliJ run configuration. (See this guideline if you've forgotten how we did this in Module-1.)