Rails: Populate Heroku database with development.s

2019-03-31 18:55发布

I would not ask this if my peculiar case had already been answered on this forum so please pardon my re-asking.

I have a rails app which uses an sqlite3 database for development and it already contains all the data I need for the app. On pushing this app to heroku I was unable to view the app over there until I had to run heroku run rake db:migrate. Now I can view the app, but I can't login in with admin accounts or do any other thing that requires data from the database. i already did all that is required (I think) as shown in my gem file below:

    source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'bcrypt-ruby'
gem 'kaminari'
gem 'gon'
.
.
.
gem 'jquery-rails', '2.0.2'

group :development, :test do
  gem 'sqlite3', '1.3.7'
end

group :production do
  gem 'pg', '0.12.2'
end
.
.

I also tried heroku db:push but that didn't help either. The main issue is that i am working on this app with someone else and we employ github for version control. But whenever I we get updates form each other our sqlite database always seems to empty itself. So to solve this problem we usually have to email the updated database directly to the other, who then proceeds to use it and replace the empty on in his local repo. I suspect that the reason why heroku rakedb:migrate is this.

But giving as we've come this far in our development, we really cannot afford to start over. Is there a way we can populate the remote heroku database with the content of our local repo db? Thanks for your responses

1条回答
贪生不怕死
2楼-- · 2019-03-31 19:47

You should export your data from sqlite into a sql dump file and start using postgres locally. If you continue to use sqlite locally and postgres remotely, you will have a bad time.

After you've migrated locally, follow this guide on Importing and Exporting Heroku Postgres Databases with PG Backups.

Also, don't use db:push or db:pull; they are deprecated.

Update

To get the sql file in your local postgres, first install and setup postgres (Homebrew, macports, other...) then do something like so:

$ psql -U user -d database < file.sql

From there, you can use the pg_dump utility to export to a .dump file. You can also import the sql directly to Heroku Postgres:

# this will delete ALL data in your postgres, so make sure it's what you want
$ heroku pg:reset HEROKU_POSTGRESQL_COLOR --app app_name
$ heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql
查看更多
登录 后发表回答