migrating to postgresql with an existing rails app using postgres.app - ruby-on-rails

I have been using the default sqlite3 to date, however as I would ultimately like to deploy using postgres I think it would be best to try it out in my development environment rather than launching myself into production untested.
Several points
a) once i have the pg.app running, how do test it?
b) in my config/database.yml file
development:
adapter: postgresql
encoding: unicode
database:
pool: 5
username:
password:
how do i know what the default name of my database is, and how would I got about setting up a user, is this even necessary?
Finally, when i check
which psql
I am told that
/usr/bin/psql
I think that this PATH is incorrect that I will need to modify it, is this so and if so why?
Im sure this is relatively simple but thanks in advance for any help you can offer.
EDIT 1:
During my error googling, and prior attempts, I shot up a similar error to this chap
Repairing Postgresql after upgrading to OSX 10.7 Lion
and thought that it may be a similar problem despite the fact that he is using homebrew?
EDIT 2:
My .bash_profile file
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
export PATH=/usr/local/bin:$PATH

A few things you need to do to get postgres.app up and running. It looks like you are using the default postgres install with Mac OS X /usr/bin/psql which is good as it should be easy to get this fixed.
The main action is to make sure that the path to the postgres.app is set in your PATH. Your .profile, .bashrc or .zshrc (whichever you use) should have the following appended to the start of your Path with the following.
export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
This will ensure that postgres.app is used in preference to any other install by prepending it to your current path. You shouldn't need any other config,
The second action is to set up your database.yml. Postgres.app comes pre-configured with your local Mac username so you don't need to add or change it for everything to just work. You also don't need to specify it in database.yml. Development should work with the following:
development:
adapter: postgresql
host: localhost
encoding: unicode
database: appname_development
Postgres.app will auto create the database that has been specified when you migrate the database for the first time. The name of your database is up to you but the convention is application_name underscore environment.

Related

How do I use the command "heroku pg:transfer"?

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

Connecting rails app to Amazon RDS server. Works in console, but now page wont load

I am trying to use a RDS database with my rails app since eventually I want to put it on heroku. (Database is about 10gb). I had trouble getting it to connect but it seems to be working. If I go into the console I can run sphinx searches and all that I need to but when I start the server it seems to freeze or something. If I click "About your application’s environment" nothing happens. I do not get any messages in the console or anything. If I try to go to another page it just tries loading the page but does not go anywhere.
Here is what my database.yml looks like.
# development:
# adapter: postgresql
# encoding: unicode
# database: musicbrainz_post
# pool: 5
# username: postgres
# password:
development:
adapter: mysql2
#encoding: utf8
host: musicbrainz.somestuff.amazonaws.com
#port: 3306
#reconnect: false
database: musicbrainz
username: myusername
password: mypass
If I comment out my old database configuration it works and the pages load and everything. But I want to use the amazon database.
Does anyone know why this is? Or is there another database/host I should be using?
Any help would be great!
After experiencing the same problem I found out what the problem was. The problem is that you need to use Ruby 1.9.3 as opposed to Ruby 1.8.7. I think the bug is occuring in the mysql2 gem.
Therefore the fix is to install Ruby 1.9.3. Here are the steps I followed:
NOTE: Before starting any of these steps, set up your terminal to "Run command as a login shell". For Ubuntu, open the terminal go to Edit -> Profile Preferences. Go to "Title and Command". Check the box next to "Run command as a login shell"
Installed RVM on my system with ruby and ruby on rails bundled: https://rvm.io/rvm/install/ Command: \curl -L https://get.rvm.io | bash -s stable --rails
Command: source ~/.rvm/scripts/rvm
Used RVM to install OpenSSL: rvm pkg install openssl
Reinstall all rubies: rvm reinstall all --force
Because you are now using RVM to manage both your rubies and your gems your gem environment will be new. Therefore you must reinstall any gems. Do so by going to your project and running: bundle install
Run your rails app: bundle exec rails s
Good luck!

Postgresql Rails issues

I need to convert an app from sqlite to Postgresql in order to use search with thinkingsphinx. I have run the following steps what am I missing?
I added the pg gem to my gemfile, ran brew install postgresql, and have configured my database.yml file as follows:
development:
adapter: postgresql
database: example_development
username:
password:
host: localhost
encoding: UTF8
I have also run the commands that homebrew suggests on installation:
If this is your first install, create a database with:
initdb /usr/local/var/postgres
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
What do I put for my username and password? Is there a file I need to edit or do I need to create a database from the command line? There must be something simple that I am missing. I haven't run any custom sql queries, and have stuck to active record defaults.
You need to specify the username and password of the account on the Postgres database you are using.
If you do not have a local postgres installation running, then there is no database to connect to, so you will have to follow some instructions online that outline installing and setting up a postgres database on your development machine.

Error creating Rails DB using rake db:create

I'm attempting to get my first "hello world" rails example going using the rails' getting started guide on my OSX 10.6.3 box.
When I go to execute the first rake db:create command (I'm using mysql) I get:
simon#/Users/simon/source/rails/blog/config: rake db:create (in /Users/simon/source/rails/blog) Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"blog_development", "pool"=>5, "password"=>nil, "socket"=>"/opt/local/var/run/mysql5/mysqld.sock"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I found plenty of stackoverflow questions addressing this problem with the following advice:
Verify that user and password are correct (I'm running w/ no password for root on my dev box)
Verify that the socket is correct - I can cat the socket, so I assume it's correct
Verify that the user can create a DB (As you can see root can connect and create a this DB no problem)
simon#/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blog_development;
Query OK, 1 row affected (0.00 sec)
to ensure that this wasn't a charset issue I also tried:
mysql> create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
Note: here is my database.yaml:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password:
socket: /opt/local/var/run/mysql5/mysqld.sock
# host: localhost
Note that I tried switching socket to localhost with no effect.
Any idea on what might be going on here?
Thanks for all the help guys. Looks like the problem had to do with my install of the mysql gem under OSX.
#tim after I proceeded to the next step and got up and going I got an error on the console, so I did a bit of searching and found this helpful thread.
After I uninstalled my ruby gems gem uninstall mysql I installed the proper mysql gems using this command (from the thread):
export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
After executing this one I was able to successfully run rake db:create and proceed.
Thanks!!
It could be a number of things.
Is your database set for utf8 character set?
Is the path to the socket correct, since it varies from OS.
Have you reinstalled the mysql gem? sudo gem install mysql
It might be MySQL, you might want to downgrade the version to 5.0
Other than that, I'm not sure.
You should post here your database.yml
To make your test better, i would try to create a database UTF-8 to see if your database supports utf-8
create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci
Does the blog_development database already exist?
If so, you can just continue to the next step.
Try running ruby script/server in the blog/ directory.
If it doesn't error out, then you should be able to navigate to localhost:3000 and continue the tutorial from here http://guides.rubyonrails.org/getting_started.html#hello-rails.
Leave me a comment if ruby script/server errors out.
I just had the same issue : it was an old mysql gem which was not up to date. Re-installing the mysql gem did the trick.
Re-install mysql-server and mysql-client using this command:
sudo apt-get install mysql-server mysql-client
and then some libraries you need to install to make MySQL available to ruby:
sudo apt-get install libmysql-ruby
This all solved my problem. Try it !!! :)

Error when trying to create database with Rails, rake db:create

I'm using Ruby1.9 and Rails 2.3.4 and I have mysql-ruby (2.8.1) gem installed.
when I try rake db:create I get the following
Couldn't create database for
{"adapter"=>"mysql",
"database"=>"war_development",
"username"=>"root", "password"=>nil,
"host"=>"localhost"}, charset: utf8,
collation: utf8_unicode_ci (if you set
the charset manually, make sure you
have a matching collation)
My database.yml has the following for development
development:
adapter: mysql
database: war_development
username: root
password:
host: localhost
Any ideas what's going wrong?
Thanks,
Tam
Are you allowed to use the root user without password? Is the mysqlserver listening on 127.0.0.1 or localhost?
Strange to see some behaviour changed, and I don't know if it is specific to snow leopard. I use tiger and got the same error. I added
host: 127.0.0.1
to the following (ofcourse with a correct mysql root/pw)
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password: *********
socket: /tmp/mysqld.sock
And it worked (on tiger/gem mysql-2.8.1/mysql Ver 14.14 Distrib 5.1.30). Hope it helps.
I followed the steps in another SO thread. However, the command for my installation is different from that of there, as I am not upgrading an existing db but installing a fresh one. So instead of advised one
$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
I typed the following
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/bin/mysql
PS: host: localhost remains as is, no need to change to 127.0.0.1
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/bin/mysql
works for me. Thank you!
You will check out your usrname and password whether its correct or not.
Also you will check out whether your mysql server is working correctly.
and also some time found the problem of socket path most of the time its is inside /tmp directory but sometimes it may not be. so try with with the default path.
Hopefully if you will try this it will work for you.

Resources