afaf12
afaf12

Reputation: 5426

How to setup postgresql for a rails application on heroku?

I have been happily developing my application locally and using sqlite3 as database only to learn that sqlite3 can't be used on heroku. So I have decided to use postgresql in both development and production environments. I was told using the same database in all environments makes life easier.

I installed postgresql:

apt-get install postgresql

Relevant part of my gemfile looks like this:

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

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

Then I ran bundle install.

In database.yml all 3 environments look the same:

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

I tinkered a bit and got my application to use postgres database locally. Works perfectly. I pushed it to heroku. When I try to access my application on heroku I see this:

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.

I know this problem is related to log files.

This is what heroku logs says:

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=

A similar question on SO says I only have to rerun bundle install and include gemfile and gemfile.lock in my commit. Had done both even before finding that question. Source: rails-3-2-from-sqlite-locally-to-postgres-on-heroku

Edit: error log says I don't have a log/production.log file. That can't be the problem, can it?

Edit 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

Edit 3: log I see when pushing app to 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 [email protected]:fierce-winter.git
   24d1b1f..78c2b86  master -> master

Upvotes: 3

Views: 5312

Answers (3)

afaf12
afaf12

Reputation: 5426

I fixed the problem by using a Procfile:

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

Upvotes: 1

Jonathan
Jonathan

Reputation: 11494

It should be as simple as:

Gemfile

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

group :production do
  gem 'pg'
end

Heroku generates the database.yml file for you. On development local in sqlite3 you will be fine. On heroku it will run production in postgreSQL. You don't have to have postgreSQL as your development environment although I agree that it would be better. But maybe you're making it harder on yourself than is necessary.

Upvotes: 0

user825623
user825623

Reputation:

This is indeed a logging problem, but the reported error message is misleading. By default, Rails writes logs to files, rather than streams. This doesn't work with Heroku, however. When deploying to Heroku, rails_log_stdout is installed automatically, fixing the issue in most cases. In your case, however, something is attempting to write to the log as a file.

It may be that rails_log_stdout is not being installed successfully; does

Injecting rails_log_stdout

appear when you deploy?

Also, it's possible that there's an extra logging statement somewhere; grep for Logger or Logger.new to check this (perhaps check case-insensitively). I've just grepped a new Rails project, and there are no occurrences of 'logger' in uncommented code.

The Heroku Logging page might also be useful.

Upvotes: 1

Related Questions