After deploying an app of Heroku I am having trouble getting the database populated with some of the data I need there. I ran heroku rake db:populate and it did create the initial admin user, but failed to put the rest of the data in.
I am populating the database with files from my local disk. I suspect the problem is caused because it sees nothing in the directory listed in the sample_data.rake file, as it in on my hdd and not the server. How can I get around this?
I figure that I have to either host all the files and change the directory to the servers directory, or find a way around this. I would guess there is an easy way to move the database from my computer to heroku? I'm obviously pretty new to this.
Thanks!
heroku commands have changed. now it should be:
heroku run rake db:push
heroku pg:reset DATABASE
heroku run rake db:seed
also data from local database can be uploaded to heroku by installing taps gem and pushing data from local PostgreSQL db.
gem install taps
heroku db:push
If you have a clean local database (i.e. just the good stuff, no silly testing data), then you could try heroku db:push:
db:push [<database_url>] # push a local database into the app's remote database
And please remember to develop on PostgreSQL if you're going to use the Heroku shared or dedicated databases, using the same database and version (version 8.3 for shared, 9.0 for dedicated) in both your development and production environments will save you much pain, suffering, and confusion.
References:
How can I import my existing data to Heroku?
Import: Push to Heroku
I setup the development db, and push it to Heroku.
rake db:reset
rake db:seed
heroku rake db:push
If you are using the default PostgreSQL, another option is heroku pg:reset
heroku rake db:seed
I use the following in the seed.rb file:
require 'pathname'
RailsRoot = Pathname.new(RAILS_ROOT).expand_path
print "Loading data..."
fileData = File.read (RailsRoot + "db/data-file.csv")
Related
Currently on Ch 2 of Hartl's tutorial in which I make a simple app with users and microposts.
I add a few users and microposts to the database.
Near the end of the chapter, it gets you to deploy the app to Heroku. When I view the app on Heroku however, there are no users or microposts anymore.
How do I get the existing data to be deployed as well?
Heroku has different database than your app locally. So every data you created in the local app will not be pushed to Heroku when you run git push heroku master
I would create a database dump (maybe the Seed Dump is interesting) so you can import this in Heroku or you could place the users and microposts in a seed file and run heroku run rake db:seed
http://railscasts.com/episodes/179-seed-data gives an brief explanation on how you could seed the db
Use YamlDb gem to dump data to a yaml file.
Create data dump by:
rake db:data:dump -> Dump contents of Rails database to db/data.yml
Push code to Heroku:
git push heroku master
Load data to heroku database:
heroku run rake db:data:load -> Load contents of db/data.yml into the database
If you're using postgres you can push your local database to your heroku app doing the following:
Checkout your config/database.yml file to see what the development database is named. For this example I'll call it cool_development. Then once you have run:
git push heroku master
Then run the migrations to create the database: heroku run rake db:migrate
Then push your database to heroku: heroku pg:push cool_development DATABASE_URL --app app_name_here
The database heroku creates for you is accessed using the DATABASE_URL environment variable so you don't need to change anything in the above line except for the local database name unless your app is named 'cool' lol.
I am currently doing the Ruby on Rails tutorial by Michael Hartl. Somewhere along the way I messed up my database. In my database file there is only 1 user, by the name of Bob.
Locally in cloud9 IDE, when I do 'rails console' and then do Users.first, I get a user with a name of "Bob".
However when I do 'heroku rails run console' and do Users.first, I get a user with a different name. (I probably changed the name somewhere along the way)
How do I get Heroku to see the correct local database file again? Should I clear the heroku database, then use pg:pull to pull the local sql database to heroku?
Not sure if you've gotten to Chapter 9 yet but section 9.3.2 of the current book deals with creating sample users. This is done through the db/seeds.rb file.
Running $ bundle exec rake db:reset then resets your DB followed by $ bundle exec rake db:seed to fill it with your new data.
You can run the same procedure on your production application with:
heroku pg:reset DATABASE
heroku run rake db:migrate
heroku run rake db:seed
It is, of course, also possible to transfer data between local and production databases with tools such as heroku-pg-transfer but that's a little advanced if you're only starting out, and I think somewhat unnecessary if you only have one user to transfer over.
Hope this helps.
You can use yaml_db gem to dump your local data to file and then upload it to heroku.
On your development machine:
rake db:data:dump
Then commit changes, push to heroku and run:
heroku run rake db:data:load
I have facing problems with migrating data to my heroku app which has Postgresql as database for my hosted site(Production). At my development site i have rails 3.2.13 with Sqlite3 as database. I have followed Ruby on rails Tutorial by Michael Hartl
i have used git push heroku to update my site at heroku. i also want to update database along with data. But heroku run rake db:migrate migrates schema not data. I tried db:push to push data to heroku but i get error
dependency.rb in 'to_specs' :Could not find sequel (~) 3.20.0
also i have searched and found that i should first my sqlite data to dump.sql and then run
heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql as answered in https://stackoverflow.com/questions/15371394/...
but it failed with
the local psql command could not be located
please tell me what i am doing wrong. or what is the right way to update heroku postgresql with my development sqlite3 data.
Thanks in Advance
It is not a good idea to fill the production database with the data that you have now in the developement database. Because, if you have problems with your production database in the future, and you need to refill it again, your development db may changed (e.g dropped), and you are not going to be able to do it again.
For this need, Rails provides seeds in db/seeds.rb file. You should create all the neccessary objects there.
Then when you push your code to Heroku, Heroku is going to prepare the database, create the schema, and seed it. If you need to seed the db manually, you can run bundle exec rake db:seed, if you want to run it in Heroku: heroku run bundle exec rake db:migrate
I just pushed my rails app to heroku, I actually have some fake data locally that I directly inserted into my table through rails console.
What is the equivalent of that in heroku, the tables are configured on heroku but there is no data.
I don't want to create a seed.rb in my db and then push it to github and then run rake db seed on heroku
can i do it manually?
You can enter your application console on heroku with:
heroku run rails console
Check out this gem. It is all ready dependency for heroku gem. With it bundled in your app you can do stuff like:
heroku db:pull
heroku db:push
Commands are quite self explanatory.
I had a working Active Admin app working on my local server, but after pushing to Heroku all my database tables are empty. I tried running heroku run rake db:migrate and then heroku restart but these both accomplished nothing. The tables are there, but they are empty.
Thanks!
If you want to have your Heroku app have the same database as local then you would you need to push your local database via heroku db:push to Heroku. This will replace the contents of the tables on Heroku with your local copy so use it carefully.
EDIT: heroku db:push is now heroku pg:push, the former is deprecated.