pulling single table from heroku postgresql - ruby-on-rails

Can someone please help me how can i pull single table from heroku database.
I have tried the following way:
heroku db:pull --tables table_name
with no luck as heroku db:pull is deprecated now.
Thanks in Advance

Not sure if you can pull a single table from heroku but there are certainly some options to pull whole database.You can use Herokus PG Backups add-on to import or export your database. First you'll have to add it as add on in your app by
$ heroku addons:add pgbackups
$ heroku update
After that you can download your database by
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
You can also use pg:transfer Heroku CLI plugin to transfer the data in a single step.
From your app's directory, copy your production database locally (assuming a local PG db), by installing the plugin and execute the pg:transfer command.
$ heroku plugins:install https://github.com/ddollar/heroku-pg-transfer
$ heroku pg:transfer

Not sure if this is still relevant, but you can achieve what you want pulling a CSV dump of a single table (or any other format) and then import it. The steps would be as follows:
# 1. Connect to psql on Heroku
$ heroku pg:psql --app your_app_name DATABASE
Then you can download a table like so:
# 2. Pull table_you_want_to_copy
your_app_name::DATABASE=> \COPY table_you_want_to_copy TO '~/local/path/your_table.csv' WITH (FORMAT csv, DELIMITER ',', HEADER true);
A simple version of an importer could look like this:
# 3. Seed local table from fetched CSV
CSV.foreach('~/local/path/your_table.csv, headers: true) do |row|
YourModel.create(row.to_hash)
end
In this block you can ofc define whatever other logic you might require.

Related

Ruby on rails load local csv data to heroku

I am trying to import data to a postgres database in heroku. I am able to upload the correct schema to heroku by using the command:
heroku pg:psql -a [app name] < db/structure.sql
so now when I run heroku pg:psql and connect to my database I can run:
\dt
and it lists all of the tables that I want. However, only the schema is uploaded, not the actual data. I have looked over a bunch of different ways to upload databases to heroku, but have been unable to get any to work. I was wondering if there is an easy way to import local csv data to heroku. Locally, I can run:
sqlite3 development.sqlite3
.mode csv
.import [table name] [filename]
And it imports the data that I want. Is there a similar way to do this for heroku databases? I know that heroku uses postgres and not sqlite3, but since I was able to load the schema easily, I thought there might be an easy way to upload data. This is my first ruby on rails project I've ever attempted to publish, so any guidance would be appreciated!
Thanks!
Upload your csv to heroku and run
COPY zip_codes FROM '/path/to/your_file.csv' DELIMITER ',' CSV;
in pg shell
Look https://gist.github.com/jboesch/5605747, Importing csv to heroku postgres how you can do it

How to clone production db on Heroku to development db

I'm on Ruby on Rails 3.2.13.
SQLite3 in local development environment.
Production environmet at Heroku (PostgreSQL).
What is the most updated (!= deprecated) and easiest way to pull the db from production to development with the above prerequisites?
you can do that with the taps gem
heroku db:pull
there are some posts about this topic at heroku as well:
https://blog.heroku.com/archives/2009/3/18/push_and_pull_databases_to_and_from_heroku
https://devcenter.heroku.com/articles/heroku-postgres-import-export
You can pull your latest production env from Heroku with a command like this:
curl -o latest.dump `heroku pgbackups:url`
From there you will want to convert the dump from PG to something that SQLite can read. The steps for that are:
Remove the lines starting with SET
Remove the lines starting with SELECT pg_catalog.setval
Replace true for ‘t’
Replace false for ‘f’
Add BEGIN; as first line and END; as last line
Lastly, you need to import that into SQLite.
sqlite3 db/development.sqlite3
sqlite> delete from schema_migrations;
sqlite> .read latest.sql
You can read details on this procedure here.

How to use "db:transfer" as a heroku command by heroku-valkyrie?

I tried to use heroku-valkyrie to transfer my local data to heroku databse.
https://github.com/ddollar/heroku-valkyrie
I installed the plugin:
heroku plugins:install http://github.com/ddollar/heroku-valkyrie.git
Installing heroku-valkyrie... done
and wanted to transfer data:
heroku db:transfer sqlite://db/development.sqlite3 postgres://my-apps-database-url/
but received this error message:
! `db:transfer` is not a heroku command.
! See `heroku help` for a list of available commands.
How can I use db:transfer as a heroku command?
According to Heroku documentation, you will have to export your local database as a dump file by using the pg dump tool and import the file using command.,
heroku pgbackups:restore DATABASE 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump'
Check for complete documentation here
Try running heroku plugins:install https://github.com/ddollar/heroku-pg-transfer first.
Then the command will be pg:transfer, not db:transfer.
However, you may run into issues since you're transferring from an sqlite database and this command only transfers from postgre.

How to take database backup on heroku without pgbackups addon?

I'm looking for a way to take database backup on heroku without using pgbackups addon. Is there any gem which can help in this situation? The gem should not also use command line command pg_dump.
You can save a copy to your development environment by using this command:
heroku db:pull
I set my env to (PGHOST, PGPORT, PGDATABSE, PGUSER, PGPASSWORD) to the Heroku instance and then run:
pg_dump --clean --blobs | sed 's/db_user_on_heroku/db_user_on_local/g' > /tmp/db.pg
I then set the env to point to my local DB, and run:
psql < /tmp/db.pg

Heroku and Rails: transferring db to staging and db:migrate

I've been working on a Heroku app for several months. We've recently set up a staging server and occasionally sync the staging db with the production db. The three main commands in use are:
$ heroku pgbackups:capture --app myapp
$ heroku pg:reset DATABASE --app myapp-staging --confirm myapp-staging"
$ heroku pgbackups:restore DATABASE `heroku pgbackups:url --app myapp` --app myapp-staging
The problem is that after running the third command, I need to run heroku run rake db:migrate --app myapp-staging. We have a few dozen migrations now, including some that refer to Ruby classes that we've deleted or renamed.
This causes the migrations to fail to fully run. What's the solution here? Should I delete the old migrations that fail and commit these changes to the git repo?
Re-running this script fixed the error, so it seems like the schema should just copy over. For anyone seeing a failing migration like I did, the pgbackups:restore command probably failed for you, so re-run that.
You can also checkout the transfer command now as part of pgbackups .. see this post
How do I transfer production database to staging on Heroku using pgbackups? Getting error

Resources