How do I configure DATABASE_URL to add a list of host_names to database.yml on heroku?

Heroku generates database.yml from the DATABASE_URL environment variable like so: database.yml.erb · GitHub

I’d like the resulting database.yml to include the line

host_names:  
  - my.customdomain.com

How would I encode this into the query string on the DATABASE_URL?

Unfortunately, you cannot directly encode YAML-specific custom keys like host_names: into the DATABASE_URL environment variable, because the DATABASE_URL only supports standard connection parameters such as:

postgres://user:password@hostname:port/database_name?param1=value1&param2=value2

This URL is parsed by Rails (via ActiveRecord) and used to populate fields such as:

  • adapter
  • database
  • username
  • password
  • host
  • port
  • encoding
  • and certain supported options

Recommended Way to Add Custom YAML Fields

If you want custom fields like:

host_names:
  - my.customdomain.com

You must manually modify your config/database.yml or use ERB logic to inject it based on an additional environment variable (not DATABASE_URL).

Example: Use an ENV variable for host_names

Edit config/database.yml like this:

production:
  url: <%= ENV['DATABASE_URL'] %>
  host_names:
    - <%= ENV['CUSTOM_HOSTNAME'] || 'default.yourdomain.com' %>

Then, on Heroku, set the variable:

heroku config:set CUSTOM_HOSTNAME=my.customdomain.com

Why You Can’t Do This via DATABASE_URL

The DATABASE_URL query string is parsed into known database adapter settings. Extra keys like host_names are ignored and will not be reflected in the generated database.yml.

For example, this:

postgres://user:pass@host:5432/dbname?host_names=my.customdomain.com

Will result in host_names being ignored unless you explicitly parse it in custom code — but Rails and ActiveRecord won’t.


Summary

  • You can’t add arbitrary YAML keys (like host_names) via DATABASE_URL.
  • Use separate ENV variables and embed them via ERB in database.yml.