---
title: "How to load Heroku production database in development?"
author: "Yaroslav Shmarov"
date: "2020-11-16"
canonical_url: "https://blog.superails.com/ruby-on-rails-heroku-load-production-database-in-development"
markdown_url: "https://blog.superails.com/ruby-on-rails-heroku-load-production-database-in-development.md"
description: "Sometimes you want to run a copy of your production database in development. It can be really useful for debugging, or experimenting with potentially dangerous…"
tags: ["ruby-on-rails","heroku","postgresql"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
video_url: "https://www.youtube.com/watch?v=qSYLJhe2nO4"
---

# How to load Heroku production database in development?

Sometimes you want to run a copy of your **production database in development**. 

It can be really useful for debugging, or experimenting with potentially dangerous operations.

Here's how you can run your Heroku Postgresql database in development:

* 1 Create Heroku backup

```
heroku pg:backups:capture --app myappname
```

* 2 Download Heroku backup [(source)](https://devcenter.heroku.com/articles/heroku-postgres-backups){:target="blank"}

```
heroku pg:backups:download --app myappname
```

* 3 Reset local database

```
rails db:drop
rails db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=1
rails db:create
```

* 4 Populate local database from heroku download

(place development database name from database.yml)

```
pg_restore -h localhost -d myappname_development latest.dump
```

* 5 run migrations

```
rails db:migrate
```

* 6 Remove downloaded database from app repository:

```
rm latest.dump 
```

That's it! Now you have a copy of your production database running in development🥳!

****

# Troubleshooting

* [Update postgresql](https://www.postgresql.org/download/linux/ubuntu/){:target="blank"}

* Create a new postgresql user and password [(source)](https://www.a2hosting.com/kb/developer-corner/postgresql/managing-postgresql-databases-and-users-from-the-command-line){:target="blank"}

```
createuser --interactive --pwprompt
yaro
pass
```

* Specify newly created postgresql user for restoring database and restore

```
pg_restore -h localhost -U yaro -d myappname_development latest.dump
pass
```

or 

```
PGPASSWORD=pass pg_restore -h localhost -U yaro -d myappname_development latest.dump
```

or

```
set "PGPASSWORD=pass"
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U yaro -d myappname_development latest.dump
```

****

My case:
```
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U yaro -d saas_development latest.dump
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U yaro -d corsego_development latest.dump
```

Adding `latest.dump` to gitignore: 

```
echo 'latest.dump*' >> .gitignore
```
