如何在heroku上为rails应用程序设置postgresql?

时间:2012-04-28 19:07:53

标签: ruby-on-rails postgresql heroku

我一直很高兴在本地开发我的应用程序并使用sqlite3作为数据库,只是为了了解sqlite3不能在heroku上使用。所以我决定在开发和生产环境中使用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中,所有3个环境看起来都是一样的:

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日志所说的:

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

3 个答案:

答案 0 :(得分:1)

这确实是一个日志记录问题,但报告的错误消息具有误导性。默认情况下,Rails将日志写入文件而不是流。但是,这不适用于Heroku。部署到Heroku时,会自动安装rails_log_stdout,在大多数情况下解决问题。但是,在您的情况下,有些东西试图将日志作为文件写入。

可能是rails_log_stdout未成功安装;确实

Injecting rails_log_stdout
部署时会出现

吗?

此外,某处可能还有一个额外的日志声明; grep for LoggerLogger.new来检查这个(也许检查不区分大小写)。我刚刚开始研究一个新的Rails项目,并且在未注释的代码中没有出现“logger”。

Heroku Logging页面也可能有用。

答案 1 :(得分:1)

我使用Procfile修复了问题:

web: bundle exec rails server thin -p $PORT -e $RAILS_ENV

答案 2 :(得分:0)

应该如此简单:

的Gemfile

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

group :production do
  gem 'pg'
end

Heroku为您生成database.yml文件。在sqlite3本地开发你会没事的。在heroku上它将在postgreSQL中运行生产。您不必将postgreSQL作为您的开发环境,尽管我同意它会更好。但也许你在自己​​身上做得比必要时更难。