Sync up Heroku Postgres to local Postgres on Gitpod IDE

Heroku Postgres to Gitpod Postgres

Background story

I plan to move one of my client projects to Gitpod Cloud IDE. It’s using Rails 2.3.6 (actually I’ve upgraded it to 2.3.8). For running the live site, it’s using Heroku. So, it makes sense to install Heroku CLI (or Heroku Toolbelt or whatever its name is) on the IDE. Here’s my story how I sync Heroku Postgres to Gitpod Postgres

This post assumes you, the readers, are familiar with Heroku CLI, running terminal, and Gitpod Cloud IDE.

Prepare Gitpod IDE

Prepare Gitpod IDE to run with Heroku CLI. If you’re not sure, take a look at my Dockerfile for installing Heroku CLI package on Gitpod.

Forget heroku pg:pull

It’s not working at the moment. You may try it yourself if you’re curious. Instead, I use these three commands as workaround:

heroku pg:backups:capture
heroku pg:backups:download
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

Explanation

Here are the explanations for every Heroku command above:

  • heroku pg:backups:capture
    This command will create the back up for your Heroku Postgres on the current state. Once it’s done, you can verify it by opening Heroku Postgres dashboard from your Heroku dashboard page. There will be new backup there.
  • heroku pg:backups:download
    This one will download the latest backup created by previous command. It will download to the current directory on your Gitpod terminal. Once it’s completed, you will see latest.dump file on your Gitpod IDE.
  • pg_restore...latest.dump
    This one will import latest.dump file into your local Postgres on Gitpod IDE.

To verify

Assuming you’re using Ruby on Rails, you can verify if the imported database is correct by running:

heroku run console -a <your_app_name>

Then, you can open another terminal and run the local rails console. Just run simple command like YourModel.count and compare the number of records on your local rails console and Heroku console.

Conclusion

Finally, I was able to sync Heroku Postgres to Gitpod Postgres and move a Ruby on Rails project with such old versions to Gitpod Cloud IDE:

  • Old Ruby 2.3.8 (formerly 2.3.6)
  • Rails 3.2 (formerly Rails 2.3.8)

With this, I’ll move more and more projects to Gitpod Cloud IDE. I am one step closer to move all projects I am working on to the cloud.

If you’re experiencing error with encoding

Then, you can read this gist on Github: https://gist.github.com/amolkhanorkar/8706915

Gitpod Cloud IDE: 30% off for first 3 months. CODE: KEVINHQ. Click here

Tags: