Skip to content

Read database settings from ENV and warn about .env_overrides.rb - #228

Open
mikehale wants to merge 3 commits into
masterfrom
claude/rails-database-yaml-config-4954c4
Open

mikehale wants to merge 3 commits into
masterfrom
claude/rails-database-yaml-config-4954c4

Conversation

@mikehale

@mikehale mikehale commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why?

  • Database settings from .env.local. A developer whose PostgreSQL runs in Docker or on another computer had to edit config/database.yml to connect. Now they set DB_HOST, DB_PORT, POSTGRES_USER and POSTGRES_PASSWORD in .env.local.
  • Warning for a leftover .env_overrides.rb. Since Switch from env.rb to .env #224, apps keep local settings in .env.local and no longer load .env_overrides.rb. Git ignores the old file, so each developer's copy stays behind and its settings are silently ignored; the app now warns at startup until the developer deletes it.
  • Pool size and timeouts. A 15-second statement timeout stops one slow query from tying up a connection. A 15-second connect timeout gives a new Heroku dyno time to reach a shared database. A 10-second lock timeout makes a statement that waits on a locked row fail instead of hang. The pool size is now RAILS_MAX_THREADS plus 2, up from RAILS_MAX_THREADS, so background threads such as GoodJob's don't take connections the web threads need. To change them, set DB_POOL, DB_CONNECT_TIMEOUT, DB_STATEMENT_TIMEOUT and DB_LOCK_TIMEOUT.

If DB_HOST, DB_PORT, POSTGRES_USER and POSTGRES_PASSWORD are unset, the app connects to the same server as before. On Heroku, DATABASE_URL still overrides those four keys. The statement and lock timeouts are new behavior: a statement that runs longer than 15 seconds, or waits more than 10 seconds for a lock, now fails. To raise the statement timeout for a slow migration or import, set DB_STATEMENT_TIMEOUT to a PostgreSQL duration such as 30min, or to 0 to turn it off. The dotenv README has examples.

What Changed

  • Read the four database keys in the default block of config/database.yml
  • Warn in development and test when .env_overrides.rb still exists
  • Set the pool size, connect timeout, statement timeout and lock timeout from ENV, with defaults

Pre-merge checklist

  • Update relevant READMEs

Notes

Please don't commit version changes within your PR. Versioning, releasing, and changelog entries are managed through the Release workflow.

The dotenv generator now adds DB_HOST, DB_PORT, POSTGRES_USER and
POSTGRES_PASSWORD to the default block in config/database.yml, read with
EnvValue. Unset keys keep PostgreSQL's own defaults, and DATABASE_URL
still wins on Heroku.

It also adds a startup warning in development and test when a leftover
.env_overrides.rb exists, since Git ignores that file and nothing loads
it any more.
@mikehale mikehale self-assigned this Nov 23, 2026
A user and password need a TCP connection. Without a host, PostgreSQL
uses the local socket and its peer or trust login instead.
The dotenv generator now also adds these to the default block of
config/database.yml:

- max_connections (or pool) from DB_POOL, else RAILS_MAX_THREADS plus 2
- connect_timeout from DB_CONNECT_TIMEOUT, default 15 seconds
- statement_timeout from DB_STATEMENT_TIMEOUT, default 15s
- lock_timeout from DB_LOCK_TIMEOUT, default 10s

The README explains each key and how to raise the statement timeout for
a slow migration or import.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant