Load Heroku Production DB locally

Creates, downloads, and loads your Heroku db to your dev db
Icons/chart bar
Used 66 times
Created by
A Alex Spencer

WARNING! This will overwrite anything you have in your local db! DO NOT RUN THIS if you do not understand what it is doing!!!

This script also assumes you are using Postgres as your db both locally and on heroku. It also assumes you have the Heroku CLI/toolbelt installand and are logged in. Be sure 

heroku login 

works before running.

It backs up your prod db, downloads it, then restores it as your local db. Great for making sure your app looks/feels just like prod <3 . 

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VB0sbL"
Template Source

Review the code before running this template on your machine.

  app_name = ask("What is the name of your app on heroku? i.e. heroku run ____ --app <what is this>?")
  db_name = ask("What is the name of your db? i.e. in database.yml <what is this>_development (usually it is your_app_name_development)?")
  if Rails.env.development?
    run "rails db:drop"

    run "heroku pg:backups:capture --app #{app_name}"
    run "heroku pg:backups:download --app #{app_name}"

    run "rails db:create"

    run "pg_restore -h localhost -d #{db_name} latest.dump"
    run "rm latest.dump"
    puts "All done!"
    puts "not in dev env. this could overwrite a prod db. aborting..."


Sign up or Login to leave a comment.