Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Sqlite with PG gem

I installed postgres on my Mac and tried it for the first time with Rails. I included gem "pg" and removed the sqlite3 gem (after all, why would you need the latter if using the former). However, when I tried to start the server I got this error message

.rvm/gems/ruby-1.9.3-rc1@rails321/gems/bundler-1.0.22/lib/bundler/rubygems_integration.rb:143:in `block in replace_gem': Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.) (LoadError)

So I included the sqlite3 gem again and now the server works fine, but I actually don't have any idea if my test app is using sqlite3 or pg?

a) Am I supposed to have the sqlite3 gem installed if I'm planning on using the pg gem? b) If I'm only supposed to have one of the two installed, is there a way to find out which one my test app is currently using (since both of them are in the Gemfile)

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'devise'
gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
like image 753
Leahcim Avatar asked Feb 29 '12 02:02

Leahcim


3 Answers

Here was my database.yml when i was working with the pg gem, the adapter is actually called postgresql and there are a couple other differences in the settings, if you just copy and paste the code below and change the database names you should be pretty much ready (I used heroku, this worked there):

development:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_DEVELOPMENT
  pool: 5
  username: USER_NAME
  password:
  host: localhost

test:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_TEST
  pool: 5
  username: USER_NAME
  password:
  host: localhost

production:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_PRODUCTION
  pool: 5
  username: root
  password:
like image 66
Alex Marchant Avatar answered Nov 16 '22 16:11

Alex Marchant


Currently you are installing two database in same environment - as per Gemfile

It may happen that you used sqlite and pg in different environment in one Gemfile.

if you want to use

gem 'sqlite3'

group :production do
  gem 'pg', '0.12.2'
end

So now I am using sqlite3 in development mode and in production I am using pg, so in your database.yml you need to place two connection, first for development mode and production mode

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: pg (please correct the adapter)
  database: 
  user:
  password:

Let me know if you need more help

like image 3
AMIC MING Avatar answered Nov 16 '22 17:11

AMIC MING


A) Am I supposed to have the sqlite3 gem installed if I'm planning on using the pg gem?

No, as you suspect you need the sqlite gem for sqlite and the pg gem for postgres

B) If I'm only supposed to have one of the two installed, is there a way to find out which one my test app is currently using (since both of them are in the Gemfile)

Yes. Type: rails db and look at the output. Here's what you would get for postgres:

$rails db
psql (9.1.2)
Type "help" for help.

C) Asked rhetorically: "Why would I need both?"

Actually there are several scenarios where you would need both, which include, some existing data in one db some in another, converting an application from one to the other, etc. It's true however that it's usually one or the other.

like image 1
junky Avatar answered Nov 16 '22 18:11

junky