Clicking one table on Heroku

I know the heroku pg:push command, which pushes the entire database to Heroku.

Now, when I launch my product, I would like to be able to squeeze only a specific table containing information collected locally, without overwriting existing tables (for example, users).

Is there a team that allows me only to click certain tables on the hero?

+8
database ruby-on-rails deployment heroku
source share
3 answers

My suggestion is to use PostgreSQL's dump / restore features directly using pg_dump and psql .

With pg_dump you can unload a specific table from the local database

 $ pg_dump --data-only --table=products sourcedb > products.sql 

Then take the Heroku PostgreSQL connection string from the configurations

 $ heroku config | grep HEROKU_POSTGRESQL # example # postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398 

and restore the table to the remote database using information obtained from Heroku.

 $ psql -h ec2-117-21-174-214.compute-1.amazonaws.com -p 6212 -U user3123 db982398 < products.sql 

You will need to configure the -p , -h and -U options, as well as the database name. Password will be requested using psql .

You can also use pg_restore to filter the dump and restore the table, but I personally prefer psql .

Please note that Heroku recommends using PostgreSQL tools in several documents, such as Import and Export for Big Data, or whenever the provided CLI commands are not distributed for example, in this question.

+10
source share

I wrote a script that retrieves DB url from heroku. Then it resets individual tables from production and restores them to development / localhost. Run it as follows:

 rake production_to_development:run\['users;news;third_table',my-sushi-app\] 

the code:

 namespace :production_to_development do task :run, [:tables, :app] => [:environment] do |t, args| tables = args["tables"].split(';') database_url = nil Bundler.with_clean_env { database_url = `heroku config:get DATABASE_URL --app=#{args["app"]}` } require 'addressable/uri' uri = Addressable::URI.parse(database_url) remote_database = uri.path[1,uri.path.length-2] # there is \n at the end of the path! tables.each do |table| backup_file = "tmp/#{table}.backup" #bin_dir = "/Applications/Postgres.app/Contents/Versions/latest/bin" bin_dir = "" dump_command = "PGPASSWORD=#{uri.password} #{bin_dir}/pg_dump --file \"#{backup_file}\" --host \"#{uri.host}\" --port \"#{uri.port}\" --username \"#{uri.user}\" --no-password --verbose --format=c --blobs --table \"public.#{table}\" \"#{remote_database}\"" `#{dump_command}` `psql -U 'root' -d my_table -c 'drop table if exists #{table}'` `pg_restore -d my_table --no-owner #{backup_file}` end end end 
0
source share

If I understand correctly, you just need one database table with locally generated data that will be transferred to your application to create Rails. This may be a simplified approach, but you can create a migration for your table and then populate with db/seeds.rb .

After you have filled the seeds.rb file and clicked your repo on the hero:

 heroku run rake db:migrate heroku run rake db:seed 

Also, if your local table contains a ton of data, and you are using Rails 4, check out the seed disk gem: https://github.com/rroblak/seed_dump . This will take your existing db data and display it in seed format.

-one
source share

All Articles