【问题标题】:Why is my 'pg' gem not being recognized? [duplicate]为什么我的 'pg' gem 没有被识别? [复制]
【发布时间】:2017-08-11 05:26:29
【问题描述】:

我知道这个问题以前曾提出过,但我已经从以前的回复中得到了建议,但我一直收到同样的错误。我不知道为什么我的 'pg' gem 仍然无法工作:

source 'https://rubygems.org'
    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end

代码:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.2'
# Use sqlite3 as the database for Active Record
gem 'bcrypt',  '~> 3.1.11'
gem 'bootstrap-sass', '3.3.7'
gem 'sprockets'
# gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
  gem 'rails-controller-testing'
  gem 'sqlite3'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :production do
  gem 'rails_12factor'
  gem 'pg', '0.20.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

【问题讨论】:

  • 错误是什么?到目前为止,您尝试过什么来解决它?
  • 这是错误:Gem::LoadError: Specified 'postgresql' for database adapter,但 gem 未加载。将gem 'pg' 添加到您的 Gemfile(并确保其版本为 ActiveRecord 要求的最低版本)。我不确定为什么它说 gem 没有加载。我每次都运行 bundle install ,现在我已经安装了很多次了。
  • 我即将提交答案,您应该编辑您的问题,使其包含错误。如果您想获得有用的答案,请始终提供相关信息,例如您收到的实际错误和已采取的步骤。有很多事情可能是错误的,所以你应该向其他人提供上下文:)

标签: ruby-on-rails postgresql


【解决方案1】:

您似乎正在尝试在本地环境中使用 Postgres,但您的 pg gem 在您的 production 组中。 Bundler 允许你为你的 gem 指定不同的环境组,并且你已经指定它应该只在生产中使用。如果您运行bundle install,它仍然会被安装,但它不会是required 并被应用程序使用。 (您可以通过运行 bundle install --without production 跳过在生产组中安装 gem)

如果您确实尝试在本地使用 Postgres,您没有说明但我在推断,您应该将 gem 'pg', '0.20.0' 移出 production 组。如果您不打算使用它,还应该删除 sqlite3 gem,它是新 Rails 项目的默认数据库适配器。

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.2'
# Use sqlite3 as the database for Active Record
gem 'bcrypt',  '~> 3.1.11'
gem 'bootstrap-sass', '3.3.7'
gem 'sprockets'
# gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# PostgreSQL database
gem 'pg', '0.20.0'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
  gem 'rails-controller-testing'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :production do
  gem 'rails_12factor'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

【讨论】:

    【解决方案2】:

    从此 = gem 'pg', '0.20.0' to this = gem 'pg', '~> 0.20.0'

    这应该是一个简单的语法错误。

    希望对您有所帮助。

    【讨论】:

    • 我把它改成了那个,但我得到了同样的错误。我很困惑为什么它说它不在我的 gemfile 中。
    • 尝试将其更改为 == gem 'pg', '~> 0.20'
    • 没有语法错误
    • 我知道这一点,只是看看它是否与这个问题相似。参考:stackoverflow.com/questions/39261996/…
    猜你喜欢
    • 2021-02-22
    • 2013-09-04
    • 2020-11-03
    • 1970-01-01
    • 2021-07-30
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多