Using Heroku to db:pull a new database - ruby-on-rails

I am trying to learn how to push and pull databases using Heroku's system, I just have a clarifying question.
My existing development database is called project_dev but I want to create a new database. I entered in the following command:
heroku db:pull mysql://root:mydbpassword#localhost/20110302heroku
I have a database.yml file that includes my development, test, and production dbs, and I got this response from Heroku:
Auto-detected local database: mysql://root:mydbpassword#localhost/project_dev?encoding=utf8
Does this mean I have to manually create a new database first if I want to pull from Heroku? Does it mean that I cannot pull at all unless the db is explicitly defined in my database.yml file?
Any pointers would be really helpful. I had a look around on Google, Heroku and SO, but I didn't find the answers I was looking for. Thank you!

Yes, you'll have to first create the new local database but you don't have to declare it in your database.yml file.
When I run heroku db:pull mysql://root:mydbpassword#localhost/newdb it correctly imports into the newdb database. I'm not sure why it auto-detects your local dev database. Do you use the latest heroku and taps gems?

Related

I would like to create new postgres sql DB for Heroku

I have deployed an application in Heroku. It is Rails applications. The DB is postgres. So this is the app in heroku. Now I want to create new app which will be a clone of the previous app. But it will have separate DB. How should I get going? I would also like to know about configuring the DB from heroku. I am not so techy so please go easy on me.
Thanks!
But it will have separate DB
So, just create a new heroku app, add PG add-on to it, and that's it. No more steps. It will have a separate database. I think you don't even have to add PG add-on, it probably is added by default.
If you wanted two apps use the same DB, then you'd have to do extra steps.
You can use Heroku Fork to clone your application. This will copy the app, environment variables and any postgres databases (and their data if you want it) to a separate Heroku application.
heroku fork --from old-app-name --to new-app-name

Syncing Heroku and localhost databases

Hey so I am following the One Month Rails guide to learning Ruby and I have hit a wall on one of the lessons. I have just finished uploading an image with Paperclip, and as I finished my work on my localhost and checked it on Heroku, something went wrong. The pin/image appears to have been pushed to my Heroku account, the only problem is that the username and password that works for my localhost:3000 won't work for my Heroku account. The same password should work for both, but for some reason something is wrong. I wish I could give you the action that is going on in my terminal, but the ruby rails is the only thing that has a continuous status flow. The problem may have been when I switched my password after not using my account on localhost for a few weeks, but i thought that once i "git pushed" that to heroku master, it would've synced. I have tried heroku run rake db:setup which didn't seem to do too much as well as wrestled by way through "Importing a Heroku Postgres Database with PG Backups", but I had some trouble working through that. Any ideas? Thanks for the help.
Your 'database.yml' should not be sent to Heroku, they take care of that, creating a new database.yml config file with the proper DB access details.
Try logging into your Heroku instance and deleting the file.
Edit: nevermind, assumed you were not able to connect to the DB, not to login into the website.
So if I'm understanding you correctly, you've deployed your application to Heroku and the login (to your application) that was working locally doesn't work on Heroku.
Deploying your application doesn't deploy data. Assuming you've run heroku run rake db:migrate then your database schemas will at least match.
At this point, you've got a couple of options.
Use a seeds.rb file which you can load with heroku run db:seed to setup some 'seed' data so that you can login.
Push your local database to Heroku - either via heroku db:push or using heroku pg:transfer provided by https://github.com/ddollar/heroku-pg-transfer
Use heroku run console to create your user account via the command line
User.create(email: 'someemail.com', password: 'somepassword', password_confirmation: 'somepassword')
I'd be inclined to go with the later option.
How did the user get in their in the first place? Perhaps going back to that step in the tutorial - just remember, if you are using rails console locally to use heroku run console on Heroku.

RoR: Beginner and Heroku

I've been trying to setup my first rails app--I'm okay at stumbling around on my local machine (OSX) and finding out how to do stuff...
I want to setup my local machine to deploy on Heroku.
I have a heroku account, and rails running on my mac...how do I get the database setup for both my local config and heroku? I have MySql locally, but it looks like heroku uses Postgre...
Can you give me a step-by-step process so that I can get to making my app and forget about server config?! :)
You should be ok if you havent written any mysql specific code. Rails handles converting the rails model/controller code to whatever database you are using. As it says here, you are probably going to want to type:
heroku db:push
to set up your database using the schema and import the data from your local database.
If you're not already using it, check out Michael Hartl's Ruby on Rails Tutorial.
Germane to your question: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying
Edited because I apparently forgot how to spell germane...
First, you would setup your database.yml file to use your MySQL on your local machine for development. Set this up however you like, a good example is http://wiki.rubyonrails.org/database-support/mysql#databaseyml_example
And than when you are ready to migrate your development database from your local machine, you would run:
heroku db:push
This will export your MySQL, SQLite or Postgres DB on your development machine and import it into your heroku instance. Your heroku instance will overwrite your database.yml file with the correct Postgres info and your application will have your development schema in the heroku Postgres DB.

How do I check the records of my heroku database?

I've just deployed my application to heroku and pointed my custom domain to the heroku servers. How can I check the records in my heroku database?
You can use heroku run rails console and look at your records with Model.all or any other method.
If you want to backup the database look at heroku PG backups, you then can import your database on your local machine and look at it there. Depending on your db adapter you could use sqlite browser for sqlite3 or phpmyadmin for MySQL.
I found a similar question like this and here is what #Chowlett says:
"You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table."
You can also type select * from tablename; to view the table contents.
How to view current database schema for Heroku app in Terminal?
heroku db:pull to pull your production DB locally to take a peek in it.
I'll give the method for connecting via a GUI tool
Run the following command to get the database credentials from Heroku:
heroku pg:credentials DATABASE_URL
Then you can use a GUI tool like PG Commander or PGAdmin to connect to the db
Heroku now has an add-on named PostgreSQL Studio (currently free & in beta) that would let you access your database from within the browser, without having to use CLI, much like PHP MyAdmin.
To attach this add-on to your application,
heroku addons:create pgstudio
Then go to the list of add-ons on Heroku, select PostgreSQL Studio, authorize it, select the database to connect with from the dropdown list of all databases and it will take you to the web-based interface to handle your selected database.
You may refer to this official article on Heroku:
https://devcenter.heroku.com/articles/pgstudio
The easy answer is:
heroku pg:info
You can also download a client side Postgres, like Postico, and using the information provided in that URL to enter password and database name etc, then you can create locally, just like phpMyAdmin.
I use the admin_data gem, works well in Heroku.
You can use heroku dataclips that allows to run queries online. Here you can find documentation https://devcenter.heroku.com/articles/dataclips.
Connect to the database using Sequel Pro. You can find your ClearDB url using heroku config command. The structure for connecting is as follows:
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true

Remote mysql database on Heroku app

Can I use mysql database from my personal web server instead of heroku's database?
I configured my production database like this:
production:
adapter: mysql2
database: somedatabase
username: someusername
password: somepassword
host: 1.1.1.1:1234
But, this doesn't work, my app still uses heroku's shared database.
This is old but in case anyone drops around looking for an answer, it's much easier than using the gem. Just provide a DATABASE_URL and SHARED_DATABASE_URL (not sure if the second is needed). The database url format is adapter://username:password#hostname:port/database, so for example, you would do:
heroku config:add DATABASE_URL=mysql://etok:somepassword#<your-server>:3306/etok
heroku config:add SHARED_DATABASE_URL=mysql://etok:somepassword#79.101.41.213:3306/etok
Then re-deploy your app. It will read your DATABASE_URL and generate the database.yml from that. The default port is already 3306 so it's not needed in the url in your case. When you deploy, you may notice that it generates your database.yml:
-----> Writing config/database.yml to read from DATABASE_URL
Then you're set (as long as your server accepts connections from your heroku host.
I've written a gem that may help with this. You can find it at:
http://github.com/nbudin/heroku_external_db
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
heroku config:add SHARED_DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
Then, do a
Heroku restart
that should do.
Important Note: I suggest you to use database host IP address than using giving the hostname directly, coz, with some shared hosting services like godaddy, the db hostname looks like user.345432.abcd.godaddy.com and it seems like heroku is unable to resolve it properly (personal experience), I resolved the hostname to IP address and using the IP directly worked like a charm ! Also, If your database password has special characters, make sure you escape them correctly (like '\!' for '!' and so on..)
Heroku ignores your database.yml. You will need to explore the Amazon RDS solution John Beynon suggested or some other similar addon (if there is one). IMO, you will either have to re-evaluate your need to use your MySQL db or find some other hosting.
Just in case you didn't already know it, the command:
heroku db:push
will duplicate both the schema AND data of your MySQL development database in heroku's Postgres database. So sticking with MySQL for dev is no problem.
I hope that helps.
have a look at Heroku Amazon RDS addon. I'm not saying use it, but it gives you an insight into what you need to do and how Heroku manages dataabases - basically you need to set a config variable to your mysql instance.
Yeah this is very straight forward and simple:
1 - create mysql db
2 - create mysql db user (set defaults)
3.1 - Go to your Heroku panel/Config Vars
3.2 - Click on "Reveal Vars" and edit (clicking on pencil icon) on the one you want to change in this case DATABASE_URL (if not present just a new one with DATABASE_URL as the name)
3 (#2) - Using command line
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:databaseserverport/databasename
then just
heroku restart
And remember the syntax:
DATABASE_URL
mysql://user:password#hostnameOrIPAddress:PortNumber/databasename
MySQL DBMS's default port number is : 3306
That's why you see examples mentioned previously using DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
Hope this helps!!!

Resources