/supabase-tutorials

How to backup Supabase database?

Learn to backup your Supabase PostgreSQL database using pg_dump with our step-by-step guide. Automate, secure, and restore your data easily!

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to backup Supabase database?

 

Step 1: Set Up Environment Variables

 

Before you begin, ensure you have the necessary environment variables. These will include your Supabase project URL, the anon key, and the service role key. Typically, these can be stored in a .env file for easy access:

SUPABASE_URL=https://your-project-ref.supabase.co
SUPABASE_ANON_KEY=your-anonymous-api-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key

 

Step 2: Install pg_dump

 

Supabase uses PostgreSQL as its database, so you will need to use pg_dump to back up your database. If you haven't installed it already, do so using a package manager.

For MacOS, you can use Homebrew:

brew install postgresql

For Windows, you may download PostgreSQL from the official website and ensure pg_dump is in your PATH.

 

Step 3: Connect to the Supabase Database

 

To connect to your Supabase database, you'll need your database URL, user, and password. You can find these details in your Supabase project dashboard under the "Settings" and then "API" tab.

Set up your connection string as follows:

PG_CONNECTION_STRING=postgresql://postgres:[your_password]@db.[project_ref].supabase.co:5432/postgres

Make sure to replace [your_password] and [project_ref] with your actual database password and project reference.

 

Step 4: Create a Backup

 

Execute the following command in your terminal to create a backup of your Supabase database:

pg_dump $PG_CONNECTION_STRING --format=custom --file=supabase-backup.dump

This will create a supabase-backup.dump file in your current directory. The --format=custom option specifies a custom archive suitable for use with tools like pg_restore.

 

Step 5: Automate Backups (Optional)

 

You might want to automate your backups using a cron job or a similar task scheduler. Here's an example of a script you could run daily with cron on a Unix-based system:

#!/bin/bash
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
pg_dump $PG_CONNECTION_STRING --format=custom --file=backups/supabase-backup-$timestamp.dump

To schedule this script, you could add a cron job by using crontab -e and inserting a line like:

0 2 * * * /path/to/backup_script.sh

This sets the script to run daily at 2 AM.

 

Step 6: Store Backups Safely

 

Ensure your backups are stored safely, possibly in a different physical location or cloud storage to avoid data loss due to failures at the primary site. You can use AWS S3, Google Cloud Storage, or any other cloud provider. Here's an example using AWS CLI to upload your backup:

aws s3 cp supabase-backup.dump s3://your-bucket-name/backups/

Don't forget to configure your AWS CLI with aws configure before running this command.

 

Step 7: Restore a Backup (Optional)

 

To restore a backup, use the pg_restore command. Make sure your database is ready to accept the restore:

pg_restore --clean --no-owner --dbname=postgresql://postgres:[your_password]@db.[project_ref].supabase.co:5432/postgres supabase-backup.dump

Modify the connection string and file path as needed for your environment. The --clean option cleans (drops) database objects before recreating them, which ensures the restore process is clean.

 

Conclusion

 

By following these steps, you'll ensure that your Supabase database is regularly backed up and can be restored when needed. Remember to keep your backups in a secure location and test your restore process periodically to confirm it works as expected.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022