0

I am setting up a Ruby on Rails app on Heroku. Heroku apparently does not support SQLite3, which is Rails' native database, but instead prefers PostgreSQL. So I'm switching production to that database.

database.yml

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

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

test:
  <<: *default
  database: db/test.sqlite3

production:
  url: <%= ENV['DATABASE_URL'] %>

Gemfile

(relevant portion)

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

And the error I'm getting in the browser is:

An unhandled lowlevel error occurred. The application logs may have details.

Digging into the application logs, it seems the error there is:

#<RuntimeError: Missingsecret_key_basefor 'production' environment, set this value inconfig/secrets.yml`>

I've confirmed that the correct DATABASE_URL environment variable is set in my Heroku settings. And I've got config/secrets.yml in .gitignore. I don't really want to track this file. I'm using Rails 5.0.1. What am I doing wrong here? The database credentials are included in the DATABASE_URL environment variable.

8
  • 1
    Did you look at the application logs? Commented Feb 21, 2017 at 18:36
  • Do you mean Activity Feed > Build Log? I am not seeing any errors there. Commented Feb 21, 2017 at 18:37
  • 1
    You can actually remove the whole production section from database.yml. The default behaviour in Rails is to use ENV['DATABASE_URL'] if its present. You can get the rails logs by running $ heroku logs Commented Feb 21, 2017 at 18:39
  • I'm using the web interface. Is there a terminal somewhere I'm missing to run that command? Commented Feb 21, 2017 at 18:40
  • 1
    Install the heroku CLI and follow the rails quickstart guide. You should also be using Postgres on your local machine. 12factor.net/dev-prod-parity Commented Feb 21, 2017 at 18:45

2 Answers 2

1

The best answer I can give is SWITCH EVERYTHING! :-)

Seriously, you're going to encounter never-ending headaches maintaining two separate databases for two different environments. Save yourself a lot of heartache and switch your entire application to postgres.

Here's a small sampling of the issues you could encounter by having two separate databases:

  1. Differences in supported data types.
  2. Differences in SQL syntax.
  3. Differences in performance optimization.

This is a great tutorial for making the transition: http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

Sign up to request clarification or add additional context in comments.

6 Comments

How does switching the DB I use for local development help solve my issue on production?
It does'nt but it will save you a lot of greif down the line when you push something that passes your tests but blows up on Postgres.
The point is, don't do what you're doing at all. Scrap the attempt to make the production switch and instead use the tutorial mentioned to switch your entire application. I can pretty much guarantee you your migration process will go smoothly.
@wogsland It eliminates the chance that the error is caused by your code being incompatible w/ PostgresQL.
This does not answer the question - it just simple makes a cheap point about dev/prod parity which is not the root issue.
|
1

Start by setting up Postgres on your local machine.

You can setup a new Rails application with postgres by using the --database=postgresql option.

rails new myapp --database=postgresql

To convert an existing app add the pg gem to your gemfile.

gem 'pg', '~> 0.19.0'

You want to use it in all environments so don't put it in a group.

This is all you need in your database.yml.

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development
test:
  <<: *default
  database: myapp_test

Then setup your local DB by running

$ bin/rails db:create
$ bin/rails db:migrate

Most guides add a ton of other useless cruft out of ignorance - Heroku will setup the prod database by using the ENV['DATABASE_URL'] env var so no production section is needed at all.

For development and test favor using ENV['DATABASE_URL'] to setup passwords and usernames rather than writing it into config/database.yml. This avoids stupid developer wars and separates the application from local configuration. The dotenv gem and direnv make this very simple to acheive.

9 Comments

The rails new myapp database=postgresql command appears to still create it with SQLite3?
So I fixed that, but bin/rails db:create is failing.
Giving up on local development for the time being, I pushed it to production but I'm still getting the same error regarding a RuntimeError: Missing secret_key_base.
Its --database=postgresql not database=postgresql. In shell commands -- means that you are passing a key/value option.
secret_key_base has nothing to do with Postgres. Its the key used to sign cookies - its set via config/secrets.yml or ENV["SECRET_KEY_BASE"]. I would try starting from scratch since it seems like you have followed a really bad set of instructions and totally botched your rails app.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.