Skip to main content

Sync production database to local

Example with holmen-quiz app on Fly

Proceed from in your project folder.

Fly app name: holmen-quiz
Fly database app name: holmen-quiz-db
Local database name: holmen_quiz_backend_2022_development

1. Preparation

Start proxy:

fly proxy 15432:5432 -a holmen-quiz-db

Run in new terminal window:

fly ssh console -a holmen-quiz

# Wait for connection...

echo $DATABASE_URL
> postgres://username:password@...

exit

Copy username and password into local terminal:

pg_dump postgres://username:password@localhost:15432 > dump.sql

2. Restore from dump

Clean existing database:

rails db:drop
rails db:create

Restore database dump:

psql -d holmen_quiz_backend_2022_development -f dump.sql

3. Cleanup

Remove database dump and close database proxy started in step 1.

rm dump.sql