【问题标题】:Rails gives error when using PostgreSQLRails 在使用 PostgreSQL 时出错
【发布时间】:2014-09-17 15:47:33
【问题描述】:

我正在尝试在我的 Rails 应用程序中使用 PostgreSQL 而不是 SQLite3,因为 Heroku 需要 PostgreSQL,我想在其中部署应用程序。使用 Postgres 标志(即--databse=postgresql)启动我的应用程序可以正常工作。但是,在将gem twitter-boostrap-rails 添加到我的Gemfile 并运行bundle install 之后,我收到关于SQLite3 的错误:

active_record/connection_adapters/connection_specification.rb:190:in

rescue in spec': Specified 'sqlite3' for database adapter, but the gem is not loaded. Addgem 'sqlite3'` 到您的 Gemfile(并确保其 版本是 ActiveRecord 要求的最低版本)

这是我的Gemfile

source 'https://rubygems.org'

gem "therubyracer"
gem "less-rails"
gem "twitter-bootstrap-rails"


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster.              
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background.   
gem 'spring',        group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

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

# Use debugger
# gem 'debugger', group: [:development, :test]

gem 'rails_12factor', group: :production>

这是我的database.yml

----------


# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default

为什么会出现这个错误,我该如何解决这个问题?

【问题讨论】:

  • 你能告诉我一件事你想使用哪个适配器吗?在 gem 文件中我看到它是 pg 但你告诉你有 sqlite 3 的错误
  • 我想使用 postgresql,因为在 Heroku 上托管是必需的
  • 检查您的 Gemfile.lock 可能它与您安装的其中一个 gem 相关
  • No sqlite3 未在我的 Gemfile.lock 中列出

标签: ruby-on-rails ruby postgresql heroku sqlite


【解决方案1】:

尝试运行bundle update,然后运行bundle exec rails s - 你还需要在你的database.yml中指定更多的参数,比如你的主机等等应该看起来像这样:

development:
  adapter: postgresql
  encoding: unicode
  database: some_database_name
  pool: 5
  host: localhost
  username: your_username (or root) if just dev
  password: your_password

请记住,因为这都是开发人员,所以用户名/密码的安全性并不重要,因为它不包含任何重要数据/在您的机器之外无法访问

【讨论】:

  • 一切正常,直到我这样做:“rails g bootstrap:install”,我再次收到 sqlite3 错误
  • 试试bundle exec rails g bootstrap:install
  • 好的,但是为什么呢?现在,当我 rake db:create db:migrate 时,我得到“用户不存在”
  • 阅读此答案以了解其工作原理:) stackoverflow.com/questions/6588674/… 至于您的其他错误,可能是因为您的数据库用户名/密码组合错误。检查您如何安装 postgresql 并为其创建用户
  • @user1020372 你还有这个问题吗?我们可以结束这个问题吗? :)
【解决方案2】:

尝试明确地设置数据库配置以进行开发。看起来 database.yml 没有正确对齐。

development:
  adapter: postgresql
  encoding: utf8
  database: your_db
  host: 127.0.0.1
  pool: 5
  username: your_db_username
  password: your_db_password

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2016-01-11
    相关资源
    最近更新 更多