如何在Heroku上为Rails应用程序设置PostgreSQL?

3
我一直在本地开发我的应用程序,并使用sqlite3作为数据库,但后来得知在heroku上无法使用sqlite3。因此,我决定在开发和生产环境中都使用postgresql。有人告诉我在所有环境中使用同一个数据库会使生活更加轻松。
我已经安装了postgresql:
apt-get install postgresql

我的gemfile中相关部分如下:

group :development, :test do
 gem 'pg', '0.13.2'
end  

group :production do
 gem 'pg', '0.13.2'
end 

然后我运行了bundle install命令。

在database.yml文件中,所有三个环境看起来都是一样的:

development:
adapter: postgresql
encoding: unicode
database: mydb
pool: 5
username: postgres
password: a
host: localhost

我进行了一些调试,并使我的应用程序在本地使用了Postgres数据库。完美运行。我将它推送到Heroku。当我尝试在Heroku上访问我的应用程序时,我看到了这个:
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.

我知道这个问题与日志文件有关。

这是 heroku logs 的输出:

2012-04-28T18:54:22+00:00 heroku[web.1]: State changed from crashed to created
2012-04-28T18:54:22+00:00 heroku[web.1]: State changed from created to starting
2012-04-28T18:54:29+00:00 heroku[web.1]: Starting process with command `bundle exec rails server -p 21268`
2012-04-28T18:54:35+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2012-04-28T18:54:35+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2012-04-28T18:54:41+00:00 app[web.1]: => Booting WEBrick
2012-04-28T18:54:41+00:00 app[web.1]: => Rails 3.2.3 application starting in production on http://0.0.0.0:21268
2012-04-28T18:54:41+00:00 app[web.1]: => Call with -d to detach
2012-04-28T18:54:41+00:00 app[web.1]: => Ctrl-C to shutdown server
2012-04-28T18:54:41+00:00 app[web.1]: Exiting
2012-04-28T18:54:41+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/rack/log_tailer.rb:8:in `size': No such file or directory - log/production.log (Errno::ENOENT)
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/rack/log_tailer.rb:8:in `initialize'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:295:in `new'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:291:in `reverse_each'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:295:in `block in build_app'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:291:in `build_app'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
2012-04-28T18:54:41+00:00 app[web.1]:   from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>'
2012-04-28T18:54:41+00:00 app[web.1]:   from script/rails:6:in `require'
2012-04-28T18:54:41+00:00 app[web.1]:   from script/rails:6:in `<main>'
2012-04-28T18:54:42+00:00 heroku[web.1]: Process exited with status 1
2012-04-28T18:54:42+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-28T18:54:45+00:00 heroku[router]: Error H10 (App crashed) -> GET fierce-winter.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=

在SO上类似的问题说我只需要重新运行bundle install并在提交时包含gemfile和gemfile.lock文件。在找到那个问题之前已经都做了。来源:rails-3-2-from-sqlite-locally-to-postgres-on-heroku

编辑:错误日志显示我没有log/production.log文件。那不可能是问题,对吧?

编辑2: production.rb:

  SampleApp::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  config.logger = Logger.new(STDOUT)    ##
  config.log_level = :info      ##

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  # Defaults to Rails.root.join("public/assets")
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  config.force_ssl = true

  # See everything in the log (default is :info)
  # config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5
end

编辑3:将应用程序推送到Heroku时看到的日志

root@user-VirtualBox:/home/user/RoR/rtapr22# git push heroku
Enter passphrase for key '/root/.ssh/id_rsa': 
Counting objects: 52, done.
Compressing objects: 100% (41/41), done.
Writing objects: 100% (41/41), 8.21 KiB, done.
Total 41 (delta 13), reused 0 (delta 0)

-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.1.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       Fetching gem metadata from https://rubygems.org/.......
       Fetching gem metadata from https://rubygems.org/..
       Using rake (0.9.2.2)
       Using i18n (0.6.0)
       Using multi_json (1.3.2)
       Using activesupport (3.2.3)
       Using builder (3.0.0)
       Using activemodel (3.2.3)
       Using erubis (2.7.0)
       Using journey (1.0.3)
       Using rack (1.4.1)
       Using rack-cache (1.2)
       Using rack-test (0.6.1)
       Using hike (1.2.1)
       Using tilt (1.3.3)
       Using sprockets (2.1.2)
       Using actionpack (3.2.3)
       Using mime-types (1.18)
       Using polyglot (0.3.3)
       Using treetop (1.4.10)
       Using mail (2.4.4)
       Using actionmailer (3.2.3)
       Using arel (3.0.2)
       Using tzinfo (0.3.33)
       Using activerecord (3.2.3)
       Using activeresource (3.2.3)
       Using addressable (2.2.7)
       Using bcrypt-ruby (3.0.1)
       Using bootstrap-sass (2.0.0)
       Using will_paginate (3.0.3)
       Using bootstrap-will_paginate (0.0.5)
       Using coffee-script-source (1.3.1)
       Using execjs (1.3.0)
       Using coffee-script (2.2.0)
       Using rack-ssl (1.3.2)
       Using json (1.6.6)
       Using rdoc (3.12)
       Using thor (0.14.6)
       Using railties (3.2.3)
       Using coffee-rails (3.2.2)
       Using curb (0.8.0)
       Using orm_adapter (0.0.7)
       Using warden (1.1.1)
       Using devise (2.0.4)
       Using faker (1.0.1)
       Using nokogiri (1.5.2)
       Using loofah (1.2.1)
       Using sax-machine (0.1.0)
       Using feedzirra (0.0.24)
       Using launchy (2.1.0)
       Using netrc (0.7.1)
       Using rest-client (1.6.7)
       Using rubyzip (0.9.7)
       Using heroku (2.24.1)
       Using jquery-rails (2.0.0)
       Using libv8 (3.3.10.4)
       Using pg (0.13.2)
       Using bundler (1.1.2)
       Using rails (3.2.3)
       Installing rails_log_stdout (0.1.1)
       Using rb-readline (0.4.2)
       Using sass (3.1.15)
       Using sass-rails (3.2.4)
       Using therubyracer (0.10.1)
       Using uglifier (1.2.3)
       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       Compiled jquery.js  (2ms)  (pid 677)
       Compiled jquery_ujs.js  (0ms)  (pid 677)
       Compiled bootstrap-transition.js  (0ms)  (pid 677)
       Compiled bootstrap-alert.js  (0ms)  (pid 677)
       Compiled bootstrap-button.js  (0ms)  (pid 677)
       Compiled bootstrap-carousel.js  (0ms)  (pid 677)
       Compiled bootstrap-collapse.js  (0ms)  (pid 677)
       Compiled bootstrap-dropdown.js  (0ms)  (pid 677)
       Compiled bootstrap-modal.js  (0ms)  (pid 677)
       Compiled bootstrap-scrollspy.js  (0ms)  (pid 677)
       Compiled bootstrap-tab.js  (0ms)  (pid 677)
       Compiled bootstrap-tooltip.js  (28ms)  (pid 677)
       Compiled bootstrap-popover.js  (0ms)  (pid 677)
       Compiled bootstrap-typeahead.js  (0ms)  (pid 677)
       Compiled bootstrap.js  (97ms)  (pid 677)
       Compiled sessions.js  (84ms)  (pid 677)
       Compiled static_pages.js  (0ms)  (pid 677)
       Compiled users.js  (0ms)  (pid 677)
       Compiled application.js  (240ms)  (pid 677)
       Compiled Untitled Folder/custom.css  (1321ms)  (pid 677)
       Compiled Untitled Folder/sessions.css  (1ms)  (pid 677)
       Compiled Untitled Folder/static_pages.css  (0ms)  (pid 677)
       Compiled Untitled Folder/users.css  (0ms)  (pid 677)
       Compiled Untitled Folder/application.css  (1377ms)  (pid 677)
       Compiled custom.css  (512ms)  (pid 677)
       Compiled sessions.css  (1ms)  (pid 677)
       Compiled static_pages.css  (1ms)  (pid 677)
       Compiled users.css  (0ms)  (pid 677)
       Compiled application.css  (533ms)  (pid 677)
       Compiled jquery.js  (1ms)  (pid 677)
       Compiled jquery_ujs.js  (0ms)  (pid 677)
       Compiled bootstrap-transition.js  (0ms)  (pid 677)
       Compiled bootstrap-alert.js  (0ms)  (pid 677)
       Compiled bootstrap-button.js  (0ms)  (pid 677)
       Compiled bootstrap-carousel.js  (0ms)  (pid 677)
       Compiled bootstrap-collapse.js  (0ms)  (pid 677)
       Compiled bootstrap-dropdown.js  (0ms)  (pid 677)
       Compiled bootstrap-modal.js  (0ms)  (pid 677)
       Compiled bootstrap-scrollspy.js  (0ms)  (pid 677)
       Compiled bootstrap-tab.js  (0ms)  (pid 677)
       Compiled bootstrap-tooltip.js  (0ms)  (pid 677)
       Compiled bootstrap-popover.js  (0ms)  (pid 677)
       Compiled bootstrap-typeahead.js  (0ms)  (pid 677)
       Compiled bootstrap.js  (79ms)  (pid 677)
       Compiled sessions.js  (81ms)  (pid 677)
       Compiled static_pages.js  (0ms)  (pid 677)
       Compiled users.js  (0ms)  (pid 677)
       Compiled application.js  (217ms)  (pid 677)
       Compiled Untitled Folder/custom.css  (1291ms)  (pid 677)
       Compiled Untitled Folder/sessions.css  (1ms)  (pid 677)
       Compiled Untitled Folder/static_pages.css  (0ms)  (pid 677)
       Compiled Untitled Folder/users.css  (1ms)  (pid 677)
       Compiled Untitled Folder/application.css  (1311ms)  (pid 677)
       Compiled custom.css  (545ms)  (pid 677)
       Compiled sessions.css  (1ms)  (pid 677)
       Compiled static_pages.css  (0ms)  (pid 677)
       Compiled users.css  (0ms)  (pid 677)
       Compiled application.css  (566ms)  (pid 677)
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
-----> Discovering process types
       Procfile declares types      -> (none)
       Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size is 26.1MB
-----> Launching... done, v18
       http://fierce-winter.herokuapp.com deployed to Heroku

To git@heroku.com:fierce-winter.git
   24d1b1f..78c2b86  master -> master

你能否将你的Procfile添加到这个问题中? - Jesse Wolgamott
你的production.rb文件中,看起来你在日志记录器方面做了一些奇怪的事情。这不是导致问题的数据库问题。 - John Beynon
production.rb 已添加。而且我没有 procfile。 - afaf12
你不需要一个 Procfile - Heroku 会默认使用 web 进程,所以这也不是问题。 - John Beynon
我添加了错误的production.rb文件。非常抱歉。 - afaf12
3个回答

1

这确实是一个日志记录问题,但报告的错误信息是误导性的。默认情况下,Rails将日志写入文件而不是流。然而,在Heroku上无法正常工作。当部署到Heroku时,rails_log_stdout会自动安装,大多数情况下解决了该问题。但在您的情况下,某些东西正在尝试将日志记录为文件。

可能是rails_log_stdout未成功安装;是否

Injecting rails_log_stdout

在部署时出现了吗?

另外,可能有额外的日志记录语句; 使用grep查找LoggerLogger.new来检查这个问题(也许不区分大小写)。我刚刚在一个新的Rails项目中使用了grep,并且在未注释的代码中没有出现'logger'。

Heroku Logging页面也可能会有用。


推送应用程序到Heroku时出现注入rails_log_stdout的情况。为了确保,我添加了日志。我运行了grep -H -r "Logger" myapp以检查包含myapp中“logger”的文件。我发现2个文件,production.rb和development.rb。尽管如此,问题仍然存在,即使我取消了注释并再次进行了推送(或者我应该做些其他事情吗?)。你绝对把我引向了正确的方向,但我还是卡住了。 - afaf12
好的,如果你绝对确定已经注释掉了任何对Logger的调用(值得尝试在应用程序的根目录中运行grep -lir logger之类的命令进行检查-如果你得到结果,但不确定它们的含义,请随时将它们粘贴在这里 :)),那么很可能是寻找调用某个gem或类似的东西。我建议清除本地日志(它们具有.log扩展名-也许将它们复制到其他地方,以防它们有用时可以回顾一下),然后在本地启动应用程序(没有浏览器)。如果这样可以工作,立即检查日志。 - user825623

1
我通过使用 Procfile 解决了这个问题:
web: bundle exec rails server thin -p $PORT -e $RAILS_ENV

3
有关这个问题,您还有更多可以说的吗?很想知道。 - Jonathan

0

这应该很简单:

Gemfile

group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

Heroku 会为您生成 database.yml 文件。在本地开发中使用 sqlite3 是可以的。但在 Heroku 上,它将在 postgreSQL 中运行生产环境。您不必将 postgreSQL 作为开发环境,尽管我同意这样做会更好。但也许您正在使自己比必要的更加困难。


我可能让事情变得更难了吗?嗯,明天继续吧。早餐时解决它,美好的一天开始。希望如此 :) - afaf12
我的意思是,尝试让postgreSQL工作起来比只使用sqlite3(包括database.yml设置)更难,而且只需按照上面的规定进行设置,这样当heroku需要时就可以使用postgreSQL。缺点是在sqlite3中找不到的错误可能会出现在生产中,这可能会很麻烦,因此我的解决方案只是一个权宜之计。 - Jonathan
@defaye:或者,如果您正在尝试使用自定义查询进行开发,并依赖于更高级的功能,例如窗口函数 - 或者与SQLite相比,仅需干净地删除列。 ;) - user825623

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