How to Connect to a Heroku Postgres Database With pgAdmin Using Docker | by Mahbub Zaman | Jan, 2021 | Towards Data Science | Towards Data Science

Heroku is a cloud platform to build, deploy, monitor, and scale applications. It supports a wide range of programming languages, including Ruby, Java, Python, and many more. On top of that, you can add many add-ons to your app. Heroku Postgres is one of them that supports PostgreSQL.
You can connect to PostgreSQL database using the command line or a graphical interface. For the latter purpose, we can use pgadmin4. It’s an open-source administration and development tool for PostgreSQL.
From this post, you’ll learn how to create a Heroku app with a Heroku Postgres add-on. After that, we will run pgadmin4 in our local machine using Docker. Finally, we will connect pgadmin4 to the PostgreSQL Database.
We will be using the cloud platform Heroku, Docker desktop for mac, and a Docker compose file for our setup.
You’ll need a Heroku account for the step. Now, go to the Heroku dashboard. From there, click the New drop-down menu and select Create new app.
Type the app name and click the Create app button.
Navigate to the Overview tab of your app and click the link called Configure-Add-Ons. You should see Heroku Postgres, as you type into the search bar.
Select Hobby Dev — Free. It can store 10,000 rows of data and has a limit of 20 Connections. Find more about plans & pricing here.
First, you will need to install Docker. Now download the docker-compose file from here, or you can copy and paste it to a file called docker-compose.yml.
version: '3.8'services:pgadmin:    container_name: pgadmin4_container    image: dpage/pgadmin4    restart: always    environment:      PGADMIN_DEFAULT_EMAIL: admin@admin.com      PGADMIN_DEFAULT_PASSWORD: root    ports:      - "5050:80"
I’ve talked about the individual elements of the docker-compose file. Have a look if you’re interested.
towardsdatascience.com
For the environment tag, the required variables are PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD. Read more about it here.
Let’s run the following command from the same directory where the docker-compose.yml file is located.
docker compose up
You can now access the pgadmin4 via your favorite web browser by vising the URL http://localhost:5050/. Use the admin@admin.com for the email address and root as the password to log in.
Congratulations!, you have successfully deployed a PostgreSQL database and running pgadmin4 on your machine using Docker. Now we need to connect them so we can do database administration from our local machine.
Click the Heroku Postgres link and navigate to the Settings tab. Now click View credentials. As you can see, there are multiple values, but we are only interested in Host, Database, User, and Password.
From pgadmin4 click Servers > Create > Server to create a server.
For the name, use any name. Now move to the Connection tab. Use the value of Host for Hostname/address. Use the value of Database for Maintenance database. Use the value of User for Username. Use the value of Password for Password, also tick the Save password? box if you don’t want to type the password every time you log in to pgadmin4.
Now for the SSL tab, select Require for SSL mode. Finally, move to the Advanced tab and use the value of Database (Maintenance database) for the DB restriction. Click Save, and you’re done!
This part is optional if you want to import some test data for testing. Click Servers > testdb > Databases > Database name > Schemas > Tables. Right-click on tables and select Query tool. Copy-paste the SQL query from here to Query Editor and click the play button. Now you should have two tables called students and marks with some test data.
Now you know how to connect to a Heroku Postgres database with pgadmin4 using Docker. If you find this post helpful, check out the posts below where I’ve talked more about Docker.
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Getting Started with Heroku, Postgres and PgAdmin — RUN__ON Part 2