简体   繁体   English

Rails:带有PG gem的Sqlite

[英]Rails: Sqlite with PG gem

I installed postgres on my Mac and tried it for the first time with Rails. 我在Mac上安装了postgres并且第一次使用Rails进行了尝试。 I included gem "pg" and removed the sqlite3 gem (after all, why would you need the latter if using the former). 我包括了宝石“pg”并删除了sqlite3 gem(毕竟,如果使用前者,为什么你需要后者)。 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? 所以我再次包含sqlite3 gem,现在服务器工作正常,但我实际上不知道我的测试应用程序是否使用sqlite3或pg?

a) Am I supposed to have the sqlite3 gem installed if I'm planning on using the pg gem? a)如果我打算使用pg gem,我是否应该安装sqlite3 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) b)如果我只应该安装两个中的一个,有没有办法找出我的测试应用当前正在使用哪一个(因为它们都在Gemfile中)

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'

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): 这是我的database.yml,当我使用pg gem时,适配器实际上称为postgresql,并且在设置中还有一些其他差异,如果你只是复制并粘贴下面的代码并更改数据库名称你应该很漂亮准备好了(我使用了heroku,这在那里工作):

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:

Currently you are installing two database in same environment - as per Gemfile 目前,您在同一环境中安装两个数据库 - 根据Gemfile

It may happen that you used sqlite and pg in different environment in one Gemfile. 您可能会在一个Gemfile中的不同环境中使用sqlite和pg。

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 所以现在我在开发模式和生产中使用sqlite3我正在使用pg,所以在你的database.yml中你需要放置两个连接,首先是开发模式和生产模式

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 如果您需要更多帮助,请告诉我

A) Am I supposed to have the sqlite3 gem installed if I'm planning on using the pg gem? A)如果我打算使用pg gem,我是否应该安装sqlite3 gem?

No, as you suspect you need the sqlite gem for sqlite and the pg gem for postgres 不,因为你怀疑你需要sqlite的sqlite gem和postgres的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) B)如果我只应该安装两个中的一个,有没有办法找出我的测试应用当前正在使用哪一个(因为它们都在Gemfile中)

Yes. 是。 Type: rails db and look at the output. 键入: rails db并查看输出。 Here's what you would get for postgres: 以下是postgres的内容:

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

C) Asked rhetorically: "Why would I need both?" C)反问道:“为什么我需要两者?”

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. 实际上,有几种情况你需要两者 ,其中包括一个数据库中的一些现有数据,另一个,将应用程序从一个转换为另一个,等等。然而,它通常是一个或另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM