Skip to content

Instantly share code, notes, and snippets.

@jacobat
Last active April 29, 2019 10:58
Show Gist options
  • Save jacobat/387ca0f5407c7adbd7ca29271b7c96ea to your computer and use it in GitHub Desktop.
Save jacobat/387ca0f5407c7adbd7ca29271b7c96ea to your computer and use it in GitHub Desktop.
Rails 6 database config gotcha

With the new multiple database support in Rails 6 connecting to more than one database from Rails becomes easier. There is a small gotcha to be aware of though. Take the following configuration:

development:
  primary:
    adapter: postgresql
    encoding: unicode
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    host: <%= ENV["PG_HOST"] || "/tmp" %>
    username: <%= ENV["PRIMARY_USERNAME"] %>
    password: <%= ENV["PRIMARY_PASSWORD"] %>
    database: "primary"
  secondary:
    adapter: postgresql
    encoding: unicode
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    host: <%= ENV["PG_HOST"] || "/tmp" %>
    username: <%= ENV["SECONDARY_USERNAME"] %>
    password: <%= ENV["SECONDARY_PASSWORD"] %>
    database: <%= ENV["SECONDARY_DATABASE"] %>

Now imagine you for some reason forget to set SECONDARY_DATABASE. In this case Rails will report that there is no configuration at all for the secondary database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment