How to Backup and Restore Databases on Heroku: A Step-by-Step Guide

Muhammad Ilyas
5 min readFeb 7, 2024

In the dynamic world of web development, managing databases is a pivotal task. Particularly in backup and restoration, a streamlined approach is essential. Heroku, a renowned cloud platform, offers such an approach for Postgres databases. This guide details the steps to backup a database from one project and restore it to another on Heroku, ensuring your data remains secure and transferable.

Step 1: Backup Database and Download the Backup File

Backing up your database is the initial and crucial step. This process involves creating a backup of your existing database and downloading the file to your local machine.

Navigating to the Database

  • Access the App Page: Start by logging into your Heroku account and navigating to your application page.
  • Select the Database: In the ‘Resources’ tab, click on the Postgres database you intend to backup.
  • Database Details: You will be redirected to the database detail page.

Creating a Backup

  • Durability Tab: Click on the ‘Durability’ tab in the database detail page.
  • Initiate Backup: You’ll find a list of past backups. Initiate a new backup by clicking on the ‘Create Manual Backup’ button.
  • Download Backup: Once the backup process is complete, refresh the page. You will see the new backup record at the top of the list. Click on the ‘Download’ button to save the backup file locally.

You will be redirected to the database detail page, click on the Durability tab.

Here you will see a list of past backups and a button to create a new Backup, click on Create Manual Backup button.

After a few seconds to a minute and then refresh the page. You will see a new backup record on top of the list, click on Download button.

The new backup file will be downloaded on your machine.

Step 2: Restore Backup File to Target Database

With the backup file in hand, the next step is to restore it to your target database.

Preparing for Restoration

  • Target Database Page: Navigate to the database detail page of the target database.
  • Settings Tab: Click on the ‘Settings’ tab.
  • View Credentials: Here, click on the ‘View Credentials’ button and note down the database credentials.
  • Taking notes: Now make note of credentials.

Restoration Methods

There are two methods to restore your database: using Local Postgres or Docker.

Local Postgres :-

  1. Install Postgres: Ensure that Postgres is installed on your machine.
  2. Run the following Restore Command :-
pg_restore - no-owner -h <Host> -p <Port> -U <User> -d <Database> -1 <path to backup file on your local machine>

Replace the following placeholders with your actual values:

  • <Host>: The hostname of your PostgreSQL server.
  • <Port>: The port of your PostgreSQL server.
  • <User>: The username for your PostgreSQL database.
  • <Database>: The name of the database where you want to restore the data.
  • <path to backup file on your local machine>: The name of your backup file.

This command mounts a local directory (containing your backup file) into the Enter the Password that we noted from the target database Settings tab When Promoted.

The database will be restored in a few minutes depending on the size of the backup file 🤗.

Docker :-

1) Use Docker: If you prefer not using Postgres locally, Docker is your go-to option.

2) Run the following Docker Command:

docker run -it --rm -v <path to backup folder on your local machine>:/backup postgres pg_restore --no-owner -h <Host> -p <Port> -U <User> -d <Database> -1 <backup file name>

Replace the following placeholders with your actual values:

  • <path to backup folder on your local machine>: The local directory path where your backup file is stored.
  • <Host>: The hostname of your PostgreSQL server.
  • <Port>: The port of your PostgreSQL server.
  • <User>: The username for your PostgreSQL database.
  • <Database>: The name of the database where you want to restore the data.
  • <backup file name>: The name of your backup file.

This command mounts a local directory (containing your backup file) into the Enter the Password that we noted from the target database Settings tab When Promoted.

The database will be restored in a few minutes depending on the size of the backup file 🤗.

Final Steps:-

  • Password Prompt: Enter the password from the target database’s Settings tab when prompted.
  • Completion: The database restoration will take a few minutes, depending on the backup file’s size.

Conclusion

The process of backing up and restoring databases on Heroku is straightforward and crucial for data management and protection. By following these steps, you can effectively safeguard your application’s data and ensure its portability across different project environments.

About the Author:

I’m Muhammad Ilyas, CTO and Co-Founder of GYB Commerce, with a rich history at Elastica (acquired by Symantec), AeroGlobe.pk, Voxlabs.io, and Zapdas Technologies. At GYB Commerce, we partner with startups to revolutionize their business operations. Our recent collaboration with McGrocer.com is a testament to this, where we joined forces to revolutionize the e-commerce landscape for online groceries.

Connect with Us

Looking to transform your business with cutting-edge tech solutions? Reach out to us at GYB Commerce, and let’s make your vision a reality. Interested in discussing your business problem with me? Choose your time here: meet me.

--

--