在Mac OS X上安装Ruby on Rails的PostgreSQL

9
我已经安装好了所有东西。但是当我运行 "rake db:create" 时,出现以下错误:
Ken-Vogts-MacBook:sixmonths ken$ rake db:create
(in /Users/ken/sixmonths)
rake aborted!
no such file to load -- pg

以下是我的 database.yml 文件:

development:
  adapter: postgresql
  encoding: unicode
  database: sixmonths_development
  pool: 5
  username: postgres
  password: xxxxxxxx

test:
    adapter: postgresql
    encoding: unicode
    database: sixmonths_test
    pool: 5
    username: sixmonths
    password: xxxxxxxx

production:
    adapter: postgresql
    encoding: unicode
    database: sixmonths_production
    pool: 5
    username: sixmonths
    password: xxxxxxxx

当我运行gem list时,我可以看到pg已经安装。

根据stackoverflow上的另一篇文章,我尝试将"postgresql"替换为"pg",但结果是这样的:

Ken-Vogts-MacBook:sixmonths ken$ rake db:create
(in /Users/ken/sixmonths)

听起来很酷,对吧?

不是的。接下来,我尝试运行“rake db:schema:dump”命令,结果出现了以下内容:

Ken-Vogts-MacBook:sixmonths ken$ rake db:schema:dump
(in /Users/ken/sixmonths)
rake aborted!
Please install the pg adapter: `gem install activerecord-pg-adapter` (no such file to load -- active_record/connection_adapters/pg_adapter)

当然没有“activerecord-pg-adapter”。我该怎么做才能使它工作?
Gemfile 内容:
source 'rubygems.org'
gem 'rails', '3.0.0' 
# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 
gem 'sqlite3-ruby', :require => 'sqlite3' 
# gem 'unicorn' 
# gem 'capistrano'
# gem 'ruby-debug'
# Bundle the extra gems: 
# gem 'bj' 
# gem 'nokogiri' 
# gem 'sqlite3-ruby', :require => 'sqlite3' 
# gem 'aws-s3', :require => 'aws/s3' 
# Bundle gems for the local environment. Make sure to 
# put test-only gems in this group so their generators 
# and rake tasks are available in development mode: 
# group :development, :test do 
  # gem 'webrat' 
# end

你的 Gemfile 文件中是否包含 gem 'pg' - Dylan Markow
@dmarkow 这是我的 Gemfile:(请看下一条评论) - Kenneth Vogt
源 'http://rubygems.org'gem 'rails','3.0.0'

捆绑边缘Rails:

gem 'rails',:git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby',:require => 'sqlite3'

gem '独角兽'

gem 'capistrano'

gem 'ruby-debug'

捆绑额外的宝石:

gem 'bj'

gem 'nokogiri'

gem 'sqlite3-ruby',:require => 'sqlite3'

gem 'aws-s3',:require => 'aws/s3'

捆绑本地环境的宝石。确保

将仅用于测试的宝石放在此组中,以便其生成器

和rake任务在开发模式下可用:

group :development, :test do

gem 'webrat'

end

- Kenneth Vogt
2个回答

12
Rails 3 只能让你访问在你的 Gemfile 中指定的 gem,因此即使你通过执行 gem install pg 在系统范围内安装了它,Rails 也无法找到它。
gem 'pg' 添加到你的 Gemfile 中,运行 bundle install,然后你就可以使用它了。

1
根据您的指示,我添加了gem'pg'。对于那些读到我之前尝试失败的人来说,可用的适配器是“postgresql”,而不是“pg”。现在我要开始学习postgresql(我是一个mysql程序员)。 - Kenneth Vogt
5
即使使用的宝石 gem 是 pg,在你的 database.yml 中仍然要使用 postgresql - Dylan Markow

0

我尝试了很多其他地方提供的解决方案,但对我有效的唯一方法是替换

gem 'sqlite3'

使用

gem 'pg'

在Gemfile中添加了gem 'pg'后,我的database.yml看起来和你的一样。

现在,>rake db:create>rake db:migrate都可以工作了。

这是在OSX Snow Lepoard、Rails 3.0.7、Postgres 9.0上测试的。

输出如下:

gem list --local

pg (0.11.0)
polyglot (0.3.1)
postgres-pr (0.6.3)
rack (1.2.2, 1.0.1)
rack-mount (0.7.2, 0.7.1, 0.6.14)
rack-test (0.6.0, 0.5.7)
rails (3.0.7, 3.0.5, 2.3.5, 2.2.2, 1.2.6)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接