How to import Heroku application database locally - ruby-on-rails

I deployed one rails application at Heroku. Now, I want to copy the production database to local machine for analyzing. How should I proceed?

The heroku pg-extras plugin makes this pretty straightforward.
heroku pg:pull DATABASE localdbname --app myapp

First, tell Heroku to capture a new backup:
heroku pg:backups capture
Then retrieve the backup from Heroku:
curl -o latest.dump `heroku pg:backups public-url`
Now import latest.dump into your local database (replace myuser and mydb) with the name of your postgres user account and name of your local database:
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

Related

Rails 4: How do you run a Postgres database downloaded from Heroku on my local machine?

I downloaded a pg backup from Heroku (it came in as a TextEdit Document) and I want to run it from my development environment to test something out. How do I convert it/save it so that I can change my database.yml file to access it?
Meet heroku pg:pull.
Pulling without username/password
You can pull a Heroku PostgreSQL database locally by running:
heroku pg:pull $HEROKU_DATABASE $LOCAL_DATABASE [--app $APP]
where $HEROKU_DATABASE is the name of the Heroku database, $LOCAL_DATABASE is the name of your local database (it'll be created by pg:pull; the command will fail if it already exists in order to protect you from accidental overwrites), and $APP is your app name.
Pulling with username/password
If you need to provide username/password for your local PostgreSQL connection then you can pass it via PGUSER and PGPASSWORD like this:
PGUSER=$YOUR_USER PGPASSWORD=$YOUR_PASSWORD heroku pg:pull $HEROKU_DATABASE $LOCAL_DATABASE [--app $APP]
Why not backups?
Capturing a separate backup just to get the recent production data might not be the best idea because Heroku rotates backups so you'll lose one of older backups. Whether this is a problem depends on your circumstances.
$ heroku pg:backups capture -a app_name
this will be echoed in your screen
curl -oUse Ctrl-C at any time to stop monitoring progress; the backup
will continue running. Use heroku pg:backups info to check progress.
Stop a running backup with heroku pg:backups cancel.
DATABASE ---backup---> b001
Backup completed
then you need to download the dump using curl
$ curl -o latest.dump `heroku pg:backups public-url -a app_name`
then create a database in PG and make the dump import to that database
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U [username] -d [database name] latest.dump
Example
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U john -d new_dump_database_name latest.dump
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U john -d production-dump prod
now config Rails’s Database.yml
development:
<<: *default
# database: rails-app_development
database: new_dump_database_name
You should take a look at the Importing and Exporting Heroku Postgres Databases with PG Backups article from Heroku.
To download the PG backup:
$ heroku pg:backups capture
$ curl -o latest.dump `heroku pg:backups public-url`
And to restore to local database:
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

import export herokuapp data to localhost

I have an running on heroku which has data added via the rails admin panel.
I want to have that exact similar data locally on my machine.
With this method I am able to generate the latest dump of that heroku data.
How do I import this dump locally now?
Please help!
Note - I make an app/private directory and ignore it in git, but you can put the dump anywhere you like. I use this sequence of commands to get production data to development.
heroku pg:backups capture --app your_app_name
curl -o private/latest.dump `heroku pg:backups public-url --app your_app_name`
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d your_app_development private/latest.dump
Guess you're using postgres and locally you've the postgres installed. Run this command from command line with necessary details, such as hostname, databasename, file name to import the dump sql file.
psql -h hostname -d databasename -U username -f file.sql
// file.sql is the dump file you download from heroku

Pg backups curl latest dump from Heroku

How can I download the latest backup from Heroku in a single line command.
The code has recently changed and doesn't seem to be of much help.
https://devcenter.heroku.com/articles/heroku-postgres-import-export
This creates a public url that i can open and download
$ heroku pg:backups public-url | cat
I need to use the curl command to download it, but it does not work
$ curl -o latest.dump heroku pg:backups public-url | cat
I have also tried to use the following and it could not resolve host.
heroku pg:backups capture && curl -o latest.dump `heroku pg:backups public-url | cat`
heroku pg:backups public-url && curl -o latest.dump `heroku pg:backups public-url | cat`
Please assist with the full command line.
Ran into the same issue but was able to figure out a workaround:
$: heroku pg:backups capture --app APP_NAME
This will return a backup to something like b001
$: heroku pg:backups public-url b002 --app APP_NAME | cat
Returns url to dump
$ curl -o latest.dump URL_FROM_DUMP
$: pg_restore latest.dump -d YOUR_DB_NAME
This helped me import my Heroku Postgres db with all tables data(rows) to my local Postgres db:
Back up your database from Heroku
heroku pg:backups:capture -a your-app-name
Download the database from Heroku. The database will be downloaded as a latest.dump file to your computer
heroku pg:backups:download -a your-app-name
Import latest.dump to Postgres (your local database) (if this is the first dump, you don't need to specify the number of dump)
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U your_local_db_username -d your_local_db_name latest.dump.[number of dump]
For number of dump look carefully on your console during step 2. For some reason Heroku documentation doesn't say anything about the number of dump.

Heroku: running pg_restore from a local file returns errors

I am trying to load test db from a file store locally (db.sql), using
heroku run pg_restore -d foo db.sql
the error message I am getting says:
Error: You must install at least one postgresql-client-<version> package.
I tried to do sudo apt-get postgresql-client-9.4.1, and get:
bash: sudo: command not found
and without sudo, I get:
E: Invalid operation postgresql-client-9.4.1
You need to use a locally-installed pg_restore to restore your data into the Heroku-hosted database.
I usually run something like this:
pg_restore --verbose --clean --no-acl --no-owner -h [host address] -p [port] -U [username] -d [name] [file path]
More info here:
PostgreSQL Backup and Restore Commands

Heroku pgbackups restore resource not found

I am currently trying to migrate a database on one app to another in heroku by following this: https://devcenter.heroku.com/articles/migrate-heroku-postgres-with-pgbackups
After running:
heroku pgbackups:restore HEROKU_POSTGRESQL_TURQUOISE -a target-app \
`heroku pgbackups:url -a source-app`
I end up with a "Resource not found error"
I'm sure I will need to post more information/logs but I'm not quite sure what is needed.
To debug, try just doing heroku pgbackups:url -a source-app and entering that URL in your browser. This command should download and install into your local Postgres database db:
curl `heroku pgbackups:url -a source-app` | pg_restore --verbose --no-acl --no-owner -h localhost -U postgres -d db -n public"
Farther down in this article it references migrating between apps, which seems like what you are trying to do.
The example syntax given is: $ heroku pgbackups:transfer HEROKU_POSTGRESQL_PINK sushi-staging::HEROKU_POSTGRESQL_OLIVE -a sushi

Resources