How to Connect Heroku PostgreSQL with Android

Install PostgreSQL in Termux, Install Heroku PostgreSQL Android

Ever heard the saying “There’s an app for that?” Well, that stands true in today’s world since apps have become a way of life for most of us. In this tutorial, we will be looking at Heroku and how you can connect Heroku’s PostgreSQL with your Android device! If you aren’t already aware, Heroku is a platform for data as well as apps – the platform provides a secure, scalable database-as-a-service with tons of developer tools like database followers, forking, data clips, and automated health checks.

As you may know, while developing your web application you may have to connect your database with your project. Connecting your database with your project using your android device may sound like a daunting task. If you tried attempting this before, you might have been confused and wondering “Hmm. How do I connect my application to the database in order to test the application?”. The frustration this causes may lead you to drop the project altogether due to technical difficulty. Don’t worry, that is where we have come up with a solution that will allow you to connect your Heroku PostgreSQL Database with your Android Phone. This solution will enable you to easily manage your database queries from anywhere with your android phone! Sounds great, doesn’t it?

In this tutorial, we will be talking about, and walking you through the steps to connect Heroku’s PostgreSQL database to your application using your Android Device. This guide will solve your problems related to web applications and allow you to progress further with your project! Do note that this is an advanced tutorial aimed at programmers/developers.

Note: This guide applies to all frameworks. Whether it may be Flask or any other framework, the steps to connect your app will be the same so you can follow along and get your project finished!

What You need?

  • An Android Phone
  • Working Internet
  • Termux App (from Playstore or F-Droid)
  • A Heroku account

We have divided this tutorial into two parts. Part 1 of this tutorial will explain the process of creating a PostgreSQL Database, while Part 2 of this tutorial will explain the process of connecting to the database with your android device.

Part 1:

1. Create a Heroku PostgreSQL Database

To get started, you need a Heroku account. You can signup for a free account on Heroku’s website. Heroku is a free cloud-computing based web host which supports almost every programming language. You can check out their premium plans also if you need something a bit more powerful or reliable.

Heroku PostgreSQL, Heroku PostgreSQL Android

As mentioned above, you need a Heroku account, If you don’t have one, you can create one by going on to their website: heroku.com and following the steps shown above to get your account set up and ready to use.


2. Create Your App

Once you have signed up and verified your account, you can simply log in and create a new app by clicking Create new app button as illustrated below:

You may name the app whatever you want and you may choose whichever region you prefer. We have left it at the default region but if you want you could choose the European region as well.


3. Add The Heroku PostgreSQL Add-On To Your App

Once your app is created, click on it and navigate to the Resources option (Shown Below) of your Heroku App and Click on Find more add-ons. It will open up in a new tab. Thereafter you simply need to find Heroku PostgreSQL and add it to your created app.

Heroku PostgreSQL, Heroku PostgreSQL Android

4. Getting Your Credentials & URI

Once you have added the Heroku PostgreSQL add-on to your application, simply click on it. This will open up a new tab where you can see the credentials and URI of your Database in the Setting tab. Simply click the View Credentials button to view your URI and credentials which you will need in order to connect your Web app.

That’s it! You have successfully created your project on Heroku and retrieved your credentials and URI. It goes without saying that your credentials and sensitive data should not be made public! If someone has access to this sensitive information, they will have control over your database which is definitely not something you want to happen.


Part 2:

Installing PostreSQL on Your Android Device

Now that we have Part 1 completed, we can move on to the second part of this tutorial. To connect and use Heroku PostgreSQL on Android, we have to install the PostgreSQL package.

To do this, simply open your Termux App. This app acts as a command-line terminal on your Linux-based phone. If you haven’t updated your existing packages, now is the best time to do so. You can do this by running the command:

apt update && apt upgrade -y

If you don’t have GIT Packages installed on Termux, then install it by running the following command:

pkg install git -y

Now that we have updated our packages and installed GIT, we can proceed further!

Let’s start by installing the Heroku PostgreSQL package in Termux. To install it simply run this command:

pkg install postgresql -y

It’ll take a few minutes to complete and once it’s done, you will now have the PostgreSQL packages installed and ready to use! That’s it, Now we can use normal MySQL syntax to create or show our Database tables.


To connect your Heroku PostgreSQL database with the installed package on Termux, you need to run the following command:

psql your_db_uri

Replace your_db_uri with your own URI which you got from Part 1 of this tutorial.

Once you have done the above, you will now be logged in to your Heroku PostgreSQL Database with Termux. You can now simply run \dt to display your database tables if there are any present. You can also create your new tables in Termux if you need to.

To show the tables in your Database you can simply type this command:

\dt

If there are any tables preset in your Heroku PostgreSQL Database it will be displayed. If you would like to create a table called ‘test’, you can do so by executing the command below:

Create table test(
S.No SERIAL PRIMARY KEY,
Name TEXT(30) UNIQUE,
Roll INTEGER(20) NOT NULL UNIQUE,
Date TIMESTAMP NOT NULL UNIQUE);

This will create a table named ‘test’ inside your Database. To confirm that the table has been created, you can use the \dt command in Termux. To display the contents within the table, you can run the following command:

table test

You have now successfully created your PostgreSQL database on Heroku and connected to the database using your Android device alone!

Conclusion:

Now that you have your database set up and connected to it via Termux, you can use your URI anywhere you want like Flask or any web app which needs to use a database.

We hope you enjoyed this tutorial and found it useful & easy to follow. If you would a tutorial on how to host your web application on Heroku for FREE, comment below and we will create a step-by-step tutorial for that!

Leave a Reply

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