How do I use the command "heroku pg:transfer"? - ruby-on-rails

I am very new to heroku/ruby on rails and git. I just went through Michael Hartl's Ruby on Rails tutorial and want to push my local database to heroku but am having trouble.
After doing some research I found this article:
pg transfer is the new taps
It seems like it should work, but I do not understand how to set my env var DATABASE_URL:
$ env DATABASE_URL=postgres://localhost/someapp-dev heroku pg:transfer
Specifically I have no idea what I am supposed to directly copy and what to change. I believe I need to enter my own local host and my own database name.
Is this correct? If so how do I find my localhost and how do I find my database name?
My database.yml file looks like this:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

my understanding - you must use a Postgresql database on Heroku. When I pushed an application to Heroku I changed the databases to Postgresq al part of the push - including in Dev. Looking at the link you provided I get the impression it means the database you are copying from is also Postgresql (as opposed to SQLite).

Related

Adding a production database to a Rails 4 site on Heroku

Well I've deployed my site on Heroku. When I navigate to the app page it shows a database listed on the Add ons section
I navigate ahead to the database details and this is what I get to see
This is my production database code
production:
adapter: mysql2
encoding: utf8
database: dfu332icuvq6bs
pool: 5
username: cekapynykihldm
password: b1XuQq9m1m8Ok3zeyABEgNiiz3
socket: /tmp/mysql.sock
My question is when I fill in the details from Heroku into the production database and push it bask to the server, the app doesnt talk to the database as none of my routes except the home#index works. How do I make my app talk to the database on Heroku.
From your comment, it appears that you need to run the database migrations and then restart your instance:
heroku run rake db:migrate
heroku ps:restart
Guess I missed the most important step. I was able to fix the problem by running
heroku run rake db:migrate

How do I setup multiple database from for one rails app on Heroku

I want to have 2 databases on one app.
From Connecting Rails 3.1 with Multiple Databases
I know how to set it up on my local machine and I know how to link to a different heroku database by changing the env vars.
But since my database.yml looks like
development:
adapter: postgresql
database: first_database
log_development:
adapter: postgresql
database: second_database
production:
adapter: postgresql
I do not know how to link the production app to two different database since the production gets the location of the database from DATABASE_URL env.
I would need a DATABASE_URL2 and someway to tell production to use either env variable dynamically.
Heroku rewrites the database.yml on deploy. It uses the DATABASE_URL environment variable to create a database.yml file for you. There is no way to add a second config value that will create a second entry in the generated database.yml.
There are ways to update the database configuration so that you can use database.yml locally and have it work the way you want in production on heroku. One way to inject your database.yml into the app is with the heroku_db_env gem:
https://github.com/skryl/heroku_db_env
You would move your database.yml with the additional database configurations to a a file specified by the gem and then you would be able to access them in production on Heroku just like you can locally.

How to migrate a postgresql database

We had a developer developed a website under his local Mac, and the developer just transferred the zip file of rails site to me, but I am using Windows.
Could anyone help me over the steps on how to change from his database to my local postgresql database ? Sorry I am new at this.
So far:
1) I've edited the database.yml file, host is localhost port 5432 and database name is data1, so I went into pGAdminII and create the same database name.
2) I've created a super user and supplied the credential to database.yml.
Is that all I need to do ? Do I need to setup anything else ? thanks!
You'll need to change the adapter in the config/database.yaml file from whatever it was to postgres, something along the lines of:
development:
adapter: postgresql
database: data1
host: localhost
username: someone
password: something
You will also need to ensure you have the relevant gem in your gemfile.
gem 'pg'
That should just about do the trick. Run your rake db:version to test the connection, if all seems well go for a rake db:migrate. Post any errors you get.

rails 3 postgreSQL basic 'database does not exist'

OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword

Heroku App is Reading database.yml File

From what I can gather, Heroku is supposed to generate a database.yml file automatically, and ignore the local one. However, I am seeing an error where that is not true, and my changes to the local database.yml are affecting the Heroku app. This is problematic because I have no idea how I should setup production portion of the file so Heroku can find the right database.
For instance with the following
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
followed by the db:migration
$:~/Apps/DancingCupid/DancingCupid$ heroku rake --trace db:migrate
spits out
rake aborted!
unable to open database file
/app/.bundle/gems/ruby/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:27:in `initialize'
...
I can get different errors depending on what type of database I sent for production.
Besides deleting the app and making a new one, is there a way to fix this problem?
Heroku definitely rewrite your database.yml on push so it doesn't matter what is in there in source control.
To confirm this do heroku run bash which will connect you to a bash session in your app then look do a cat config\database.yml and you will see how they have rewritten it.
The other answers are NOT TRUE anymore as of Rails 4.1.
config/database.yml won’t be overwritten anymore when a Rails 4.1.0
RC1 app is detected. Instead, it will be merged with DATABASE_URL so
additional options like pool size can be set in config/database.yml.
To double-check the contents of your database.yml on the Heroku server, you can run remote bash via heroku run bash and then cat config/database.yml to see its contents on the server and compare with your local one.
I don't think you are insane! ( But I thought I was )
I have been beating my way around this problem for a few days, and finally found this article:
http://article.gmane.org/gmane.comp.lang.ruby.rails.heroku/1003/match=database+yml
It led me to believe that maybe it wasn't my code at all!
I then simply destroyed my heroku app and created a new one, and pushed to it. Suddenly everything works fine! I don't know how or when or why, but I think it is possible to overwrite or corrupt the database.yml file that Heroku creates.
Hope this helps!
Try removing database.yml from version control. It's good practice to make a copy of database.yml into something like database.yml.example and adding database.yml to your .gitignore file.
That way when you push to Heroku it won't have any database configuration to refer to.
You probably also don't want the sqlite3 gem in production. Make sure it's in the development/test groups in your Gemfile.
database.yml should not be pushed to Heroku. It will try to connect to that database, timeout and then crash.
Add it to your .gitignore so it doesn't get up there.

Resources