How to import a Heroku PG dump into local machine

前端 未结 5 888
小鲜肉
小鲜肉 2020-12-04 12:25

I\'m trying to import my production Heroku database into my development machine.

My local db is PostgreSQL.

First, I\'m exporting the dump from Heroku to my

相关标签:
5条回答
  • 2020-12-04 13:08

    Follow these 4 simple steps in your terminal
    (Heroku Dev Center):

    1. Create a backup copy of your database:

      $ heroku pg:backups capture DATABASE_NAME
      
    2. Download the copy from Heroku (to your local machine) using curl:

      $ curl -o latest.dump `heroku pg:backups public-url`
      
    3. Load it*:

      $ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U YOUR_USERNAME -d DATABASE_NAME latest.dump
      
      • get YOUR_USERNAME and choose the desired database from your config/database.yml file.
      • DATABASE_NAME can be your development/test/production db (Ex. mydb_development)

    That's it!

    0 讨论(0)
  • 2020-12-04 13:12

    I wanted to avoid having to set up Postgres on my local machine (blowing away and recreating the database is a pain if you're just looking for quick instructions). I put together some exact instructions for doing this with a local Postgres database running Docker. I'm adding a link here, since Google kept bringing me to this question (and it's a possible solution, though probably not what you're looking for): https://gist.github.com/locofocos/badd43131f14b3e40c760741d5a26471

    0 讨论(0)
  • 2020-12-04 13:15

    Heroku export the .dump extension file of the db to import in any of the relational DB by having its own norms and conditions. While importing it to the local postgres DB, first you download the file latest.dump into your local machine and then run

    pg_restore -h localhost -p 5432 -U postgres_username -d db_name -v latest.dump
    

    and restart the rails server.

    0 讨论(0)
  • 2020-12-04 13:21

    Use the Taps gem. It allows you to run a simple heroku db:pull to populate a local database.

    0 讨论(0)
  • 2020-12-04 13:24

    You see errors because psql tries to interpret SQL queries when you're actually giving him a compressed dump (that's what heroku uses).

    While you can't read the dump, pg_restore -O latest.dump gives you valid SQL you could pipe to psql but the easy solution is the following one :

    pg_restore -O -d app_development latest.dump
    

    Notes :

    • Use -O because you probably don't use the random username of your remote heroku postgres db.
    • Heroku doesn't recommend to use taps but I don't know how really risky it is.
    0 讨论(0)
提交回复
热议问题