Fresh rails app默认为Postgres而不是SQLite3

我刚刚在这台机器上安装了rbenv,ruby 2.2.3和rails 4.2.4。 我已经启动了我的rails应用程序而没有更改任何代码,只使用rails new .的默认生成文档rails new . 然后我用rails server启动了rails server

当命中http://localhost:3000我收到以下错误:

“为数据库适配器指定’postgresql’,但未加载gem 'pg' 。将gem’pg gem 'pg'添加到您的Gemfile中(并确保其版本达到ActiveRecord所需的最小值)。”

我从之前的Node项目中安装了postgres,但是我的database.yml仍然按照你对新应用程序的期望进行读取:

 # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3 

我现在不想真正使用Postgres,我刚刚开始,我宁愿用SQLite3来保持简单。 有谁知道可能会发生什么以及我可以做些什么来使用SQLite3来获取它以便此错误停止?

问题是,当你启动服务器时,它正在寻找可能设置为postgres的环境变量DATABASE_URL,这优先于database.yml文件。 您可以删除环境变量,它应该可以工作,或者您可以将其重置为SQLite。

还有另一种解决方案,对于那些实际需要保留DATABASE_URL环境变量而不影响Rails的人(比如我)。 您可以使用url子键:

 development: <<: *default url: sqlite3:db/development.sqlite3 

这在此处记录 。

这是一个古老的post,但也许有人会觉得这很有用。

在我的例子中,我在〜/ .bash_profile中导出了DATABASE_URL变量

 # Postgres installation export DATABASE_URL=postgres:///$(whoami) 

在Linux上,它可能在~/.bashrc文件中。

如果你不再使用Postgres,只需删除或注释掉导出行,或者如果你不使用AlienBishop的解决方案。

正如IliaAptsiauri所提到的,没有DATABASE_URL变量应用程序应该使用database.yml文件中的设置。