How to push local postgresql database built on rails to heroku? - ruby-on-rails

I have some data in my postgresql database that I entered using a very simple rails app. I deployed it to heroku and now am trying to send my local database to heroku because its very empty right now.
At first I thought doing heroku run rake db:migrate would actually do this but turns out it only creates tables in heroku?
These are the steps I took to transfer my local database to heroku.
1: heroku run rake db:migrate ( I did this to create tables which should be empty right?)
2: heroku pg:push my_db DATABASE_URL --app my-herokuapp-98989 (the actual syntax to transfer database)
Here, it said
"Remote database is not empty. Please create a new database or use heroku pg:reset"
4: heroku pg:reset DATABASE
"WARNING: Destructive action" "postgresql-amorphous-59192 will
lose all of its data" "To proceed, type my-herokuapp-98989 or
re-run this command with --confirm my-herokuapp-98989
5: my-herokuapp-98989
"Resetting postgresql-amorphous-59192... done"
After this I try to open my app but it gives off an error saying
"ActionView::Template::Error (PG::UndefinedTable: ERROR: relation
"drinks" does not exist" "2017-07-29T04:50:20.697828+00:00
app[web.1]: LINE 1: SELECT "drinks".* FROM "drinks""
"drinks" is the name of my table and I think it is giving this error because I just reset my database so there is no table or columns and rows?
So again I try pushing my database to heroku and it just freezes at this point
6: heroku pg:push my_db DATABASE_URL --app my-herokuapp-98989
"heroku-cli: Pushing my_db ---> postgresql-amorphous-59192"
...and it freezes here
What am I doing wrong? Please help.

are you sure it's freezing? or its just loading, because you need to upload the dumpfile to heroku.
you also can try this command
heroku pg:backups:restore 'https://url/where/heroku/can/download/yourdbdump.dump' DATABASE_URL
more about import export you can read here. https://devcenter.heroku.com/articles/heroku-postgres-import-export#import

Related

Deploying to Heroku with existing data

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.

Heroku deployment error RoR

When running my RoR app in Heroku shows this error:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
Checking the heroku logs, this shows http://pastebin.com/yTWtRMVD
My main page is https://salty-stream-26420.herokuapp.com/ and my callback URL is https://salty-stream-26420.herokuapp.com/auth/twitter/callback in OmniAuth.
It runs in Cloud9 perfectly but not in Heroku. What is wrong?
Thank you, Alex.
This:
2016-05-03T13:47:23.544506+00:00 app[web.1]: PG::UndefinedTable: ERROR: relation "submissions" does not exist
suggests to me that you have to simply migrate your database on the remote server.
Try running heroku run rake db:migrate
You will have to create environment variables in the config/database.yml file for the username, password and database name
Those same environment variables need to be defined under
Heroku login >> application >> settings >> reveal config vars
By default you will get a DATABSE_URL something as
postgres://wwwbirawxqwmws:7bef28c02299c9c6054a646b5ce19276bbab52feca6692dac8305b32dbd7d36c#ec2-81-XX-XXX-X26.compute-1.amazonaws.com:5432/d6n59y5r09ko99
where wwwbirawxqwmws is USER
and 7bef28c02299c9c6054a646b5ce19276bbab52feca6692dac8305b32dbd7d36c is PASSWORD
and d6n59y5r09ko99 is the DATABASE
These three variables needs to be assigned in the heroku config, then you will be able to run
heroku run rake db:create
heroku run rake db:migrate

Local sql database and heroku postgres database files differ, how to sync again? Ruby on Rails

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

Heroku won't reset my database

I have been trying to run $ heroku pg:reset from the command line but I believe I'm not putting in the database correctly. I've tried a number of variations.
I ran $ heroku config | grep POSTGRESQL to get the database name which prints as
HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
I've tried running everything from
#1
$ heroku pg:reset HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#2
$ heroku pg reset postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#3
$ heroku pg:reset db7eute4gu4mcb
and other variations. Please let me know how to correctly note this as I keep getting either an error or this text from the commmand line " ! Unknown database: db7eute4gu4mcb. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_PINK_URL"
I'm currently at 10.4 on the Ruby on Rails tutorial. Thanks!
You should specify a DATABASE when run heroku pg:reset. This is the syntax:
heroku pg:reset <DATABASE>
To know the value of , you can run:
heroku pg:info
It will return DATABASE_URL, something like: HEROKU_POSTGRESQL_GRAY_URL
Then you can reset your database:
heroku pg:reset HEROKU_POSTGRESQL_GRAY_URL
In your case, to reset database run:
heroku pg:reset HEROKU_POSTGRESQL_PINK_URL
this is how it worked for me
(replace app-staging with your app's name, do not replace DATABASE_URL, this is how heroku now finds the app's db)
heroku pg:reset DATABASE_URL --confirm app-staging
hope it helps
Assuming you have your authentication information exported to the shell environment, you should be fine with just passing the name of the database. For example:
PGPASSWORD='foobarbaz'
export PGPASSWORD
heroku pg:reset pink
There are certainly other ways to use the reset command, but IMHO this is the easiest.

Rails Heroku Database Population

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")

Resources