How to Seed a Local DB from a Remote Supabase DB: A Step-by-Step Guide
Image by Annamaria - hkhazo.biz.id

How to Seed a Local DB from a Remote Supabase DB: A Step-by-Step Guide

Posted on

Are you tired of manually populating your local database with sample data every time you want to test your application? Do you wish there was a way to effortlessly sync your remote Supabase database with your local environment? Well, you’re in luck! In this comprehensive guide, we’ll take you through the process of seeding your local database from a remote Supabase DB.

Why Seed Your Local DB from Remote Supabase DB?

Before we dive into the nitty-gritty, let’s quickly discuss the benefits of seeding your local database from a remote Supabase DB:

  • Consistency: By syncing your local database with your remote Supabase DB, you ensure that both environments have the same data, reducing errors and inconsistencies.
  • Efficiency: No more manual data entry! Seeding your local database automatically saves you time and effort.
  • Faster Development: With a seeded local database, you can focus on developing and testing your application without worrying about data integrity.

Prerequisites

Before we begin, make sure you have the following:

  • A Supabase account with a remote database set up
  • A local PostgreSQL database installed on your machine
  • pgAdmin or a similar PostgreSQL client tool
  • A code editor or IDE of your choice

Step 1: Create a Backup of Your Remote Supabase DB

In this step, we’ll create a backup of your remote Supabase database using the Supabase CLI. Install the Supabase CLI by running the following command:

npm install -g @supabase/cli

Once installed, log in to your Supabase account using the CLI:

supabase login

Next, create a backup of your remote database using the following command:

supabase db backup --instance  --db-name  --bucket  --key  --secret 

Replace the placeholders with your actual Supabase instance name, database name, bucket name, key, and secret.

Step 2: Download the Backup File

The backup process will create a ZIP file containing your database dump. Download the file from your Supabase bucket using the following command:

supabase storage cp /.zip .

Step 3: Restore the Backup to Your Local PostgreSQL DB

Unzip the downloaded backup file using your preferred unzip tool or command:

unzip .zip

Next, create a new database in your local PostgreSQL instance using pgAdmin or a similar client tool:

createdb mylocaldb

Now, restore the backup to your local database using the following command:

pg_restore -C -d mylocaldb .sql

Step 4: Verify the Data

Connect to your local PostgreSQL database using pgAdmin or a similar client tool:

psql -U  mylocaldb

Verify that the data has been successfully restored by running a simple query:

SELECT * FROM mytable;

Step 5: Automate the Seeding Process (Optional)

If you want to automate the seeding process, you can create a simple script using your preferred programming language. Here’s an example using Node.js:

const { exec } = require('child_process');

// Set environment variables
const SUPABASE_INSTANCE = 'myinstance';
const SUPABASE_DB_NAME = 'mydb';
const SUPABASE_BUCKET = 'mybucket';
const SUPABASE_KEY = 'mykey';
const SUPABASE_SECRET = 'mysecret';
const LOCAL_DB_NAME = 'mylocaldb';
const LOCAL_DB_USER = 'myuser';
const LOCAL_DB_PASSWORD = 'mypassword';

// Create a backup of the remote Supabase DB
exec(`supabase db backup --instance ${SUPABASE_INSTANCE} --db-name ${SUPABASE_DB_NAME} --bucket ${SUPABASE_BUCKET} --key ${SUPABASE_KEY} --secret ${SUPABASE_SECRET}`, (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }

  // Download the backup file
  exec(`supabase storage cp ${SUPABASE_BUCKET}/${SUPABASE_DB_NAME}-$(date +'%Y-%m-%d-%H-%M-%S').zip .`, (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`);
      return;
    }

    // Unzip the backup file
    exec(`unzip ${SUPABASE_DB_NAME}-$(date +'%Y-%m-%d-%H-%M-%S').zip`, (error, stdout, stderr) => {
      if (error) {
        console.error(`exec error: ${error}`);
        return;
      }

      // Restore the backup to the local DB
      exec(`pg_restore -C -d ${LOCAL_DB_NAME} ${SUPABASE_DB_NAME}-$(date +'%Y-%m-%d-%H-%M-%S').sql`, (error, stdout, stderr) => {
        if (error) {
          console.error(`exec error: ${error}`);
          return;
        }

        console.log('Seeding complete!');
      });
    });
  });
});

Conclusion

And that’s it! You’ve successfully seeded your local database from a remote Supabase DB. By following these simple steps, you can ensure that your local environment is always in sync with your remote database, making it easier to develop and test your application.

Tips and Variations

If you want to customize the seeding process, consider the following tips and variations:

  • Use a scheduling tool like cron jobs to automate the seeding process at regular intervals.
  • Modify the script to handle errors and edge cases more robustly.
  • Use a different programming language or toolchain to automate the process.
  • Seed specific tables or data ranges instead of the entire database.
Tools and Technologies Used Description
Supabase CLI Used to create a backup of the remote Supabase DB.
pgAdmin Used to create a new database in the local PostgreSQL instance and verify the restored data.
Node.js Used to automate the seeding process using a simple script.

By following this comprehensive guide, you should now be able to seed your local database from a remote Supabase DB with ease. Happy coding!

Here are the 5 Questions and Answers about “How to seed local DB from remote Supabase DB” in a creative voice and tone:

Frequently Asked Questions

Get ready to sync your local DB with your remote Supabase DB like a pro!

Q: What is the easiest way to seed my local DB from my remote Supabase DB?

A: You can use the `supabase CLI` to dump your remote DB and then restore it to your local DB. Simply run `supabase db remote:dump` to dump your remote DB, and then `supabase db local:restore` to restore it to your local DB. Easy peasy!

Q: Do I need to have Supabase CLI installed on my machine to seed my local DB?

A: Yes, you need to have Supabase CLI installed on your machine to use the `remote:dump` and `local:restore` commands. If you haven’t installed it yet, you can do so by running `npm install -g @supabase/cli` or `yarn global add @supabase/cli`. Once installed, you’re good to go!

Q: Can I select specific tables to seed from my remote Supabase DB?

A: Yes, you can! When running `supabase db remote:dump`, you can specify the `–tables` flag to select specific tables to dump. For example, `supabase db remote:dump –tables=my_table,another_table` will only dump the `my_table` and `another_table` tables. Neat, huh?

Q: Will seeding my local DB from my remote Supabase DB overwrite my existing local data?

A: By default, `supabase db local:restore` will overwrite your existing local data. If you want to preserve your local data, you can use the `–append` flag to append the remote data to your local DB instead. For example, `supabase db local:restore –append` will merge the remote data with your local data.

Q: Can I automate the process of seeding my local DB from my remote Supabase DB?

A: Absolutely! You can use a CI/CD pipeline or a cron job to automate the process of seeding your local DB from your remote Supabase DB. Simply write a script that runs the `supabase db remote:dump` and `supabase db local:restore` commands, and schedule it to run at a frequency that suits your needs. Boom!