2012-02-29 97 views
5

我在我的Mac上安装了postgres,并且第一次尝试了Rails。我包括宝石“pg”并删除了sqlite3宝石(毕竟,如果使用前者,为什么还需要后者)。然而,当我试图启动服务器,我再次得到这个错误消息Rails:使用PG gem的Sqlite

.rvm/gems/[email protected]/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) 

所以我包括sqlite3的宝石,现在服务器工作正常,但其实我没有任何想法,如果我的测试应用程序是使用sqlite3或pg?

a)如果我打算使用pg gem,我应该安装sqlite3 gem吗? 二)如果我只应该有安装的两个中的一个,是有办法找出哪一个我测试应用程序目前正在使用(因为两者都是在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' 
+0

我可以在这里粘贴你的Gemfile吗?你正在使用哪种Rails版本? – 2012-02-29 02:47:07

+0

@Amit,rails 3.2.1,发布gemfile在OP – Leahcim 2012-02-29 02:48:51

+1

这听起来像它试图加载sqlite,是的Gemfile会有所帮助,也database.yml配置为使用PG? – 2012-02-29 02:52:01

回答

10

这里是我的database.yml当我与编程宝石工作,适配器实际上是所谓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: 
+0

我怎么知道我的用户名是用于postgres的? – Leahcim 2012-02-29 03:30:50

+0

多数民众赞成在你的本地主机上的postgres服务器完成,一旦你已经得到了postgres服务器和运行你应该创建一个用户和数据库ID检查在这里http://www.jonathandean.com/2011/08/postgresql -8-4-on-mac-os-x-10-7-lion /如果你正在运行mac – 2012-02-29 03:38:14

+0

谢谢,可惜我希望它会像sqlite3一样简单,它为你完成。但是,我尝试了CREATE USER并被告知未找到命令。如果你想获得更多的SO点数,我还提出了另一个问题。在此先感谢:) http://stackoverflow.com/questions/9493257/postgres-creating-a-username – Leahcim 2012-02-29 03:40:19

3

目前你在同样的环境下安装两个数据库 - 按照Gemfile中

可能发生,你在一个Gemfile中使用SQLite和皮克在不同的环境中。

,如果你想使用

gem 'sqlite3' 

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

所以我现在用在开发模式的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: 

让我知道如果你需要更多的帮助

1

A)如果我打算使用pg gem,我应该安装sqlite3 gem吗?

不,你怀疑你需要的SQLite SQLite的宝石和Postgres的

B)如果我只应该有安装的两个中的一个PG宝石,是有办法找出我的测试应用当前正在使用哪一个(因为它们都在Gemfile中)

是的。输入:rails db并查看输出。 这里,你会得到什么Postgres的:

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

C)反问道: “为什么?我需要既”

其实有几种情况下,您将需要,其中包括中一个数据块的一些在其他一些现有的数据,从一个转换应用到其他等不过,它通常是一个或另一个。