Skip to content

Instantly share code, notes, and snippets.

@vitalbone
Last active August 17, 2022 15:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vitalbone/04e3831ca21608c7084f70952e902f99 to your computer and use it in GitHub Desktop.
Save vitalbone/04e3831ca21608c7084f70952e902f99 to your computer and use it in GitHub Desktop.
Restore a Heroku DB from an external DB source

Restore Heroku DB from an external DB source

1. Create an instance of a Heroku DB

$ heroku addons:create heroku-postgresql:plan -a app-name
# heroku addons:create heroku-postgresql:standard-0 -a cool-app-prod

2. Export external DB locally

$ pg_dump -Fc --no-acl --no-owner DATABASE_URL > DESTINATION
# pg_dump -Fc --no-acl --no-owner postgresql://postgres:postgres@hostname.com:5432/database-name?sslmode=require > ./db/exports/prod.dump

3. Upload exported DB to an AWS s3 bucket

$ aws s3 cp LOCAL_FILE S3_BUCKET/FILENAME
# aws s3 cp ./db/exports/prod.dump s3://bucket-name/prod.dump

4. Sign the bucket object

$ aws s3 presign s3://bucket-name/object-name
# aws s3 presign s3://MY-DBS/prod.dump

5. Restore the URL to Heroku DB

$ HEROKU_DEBUG=1 heroku pg:backups:restore 'WHATEVER_URL_S3_RETURNS' HEROKU_POSTGRESQL_COLOR -a app-name
# HEROKU_DEBUG=1 heroku pg:backups:restore 'https://xxxxxxxx.s3.us-east-1.amazonaws.com/xxxxxxxxx.dump?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXus-east-1%2Fs3%2Faws4_request&X-Amz-Date=XXXXXXXX&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=XXXXXXX' HEROKU_POSTGRESQL_PINK -a cool-app-prod

6. Promote Heroku DB to primary DB

$ heroku pg:promote HEROKU_POSTGRESQL_COLOR -a app-name
# heroku pg:promote HEROKU_POSTGRESQL_PINK -a cool-app-prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment