2012-02-29 109 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)反問道: 「爲什麼?我需要既」

其實有幾種情況下,您將需要,其中包括中一個數據塊的一些在其他一些現有的數據,從一個轉換應用到其他等不過,它通常是一個或另一個。