How to make Ecto Repo configuration work in both local and production using environment variables?
0 reputation · 11 Apr 2022, 14:09 UTC
In a Phoenix project the config/dev.exs file uses config :my_app, MyApp.Repo, url: \"postgres://localhost/my_app_dev\" and the app runs fine locally. In production I switch to a release and change the config to config :my_app, MyApp.Repo, url: System.get_env(\"DATABASE_URL\"). The release starts but the database connection fails, even though DATABASE_URL is set on the host.
It seems the environment variable is not being read when the release boots, or the configuration order is wrong. I also tried setting the variable inside config/prod.exs with System.put_env(\"DATABASE_URL\", \"postgres://...\") but that does not help.
What is the correct pattern for loading environment variables into the Ecto Repo configuration so that it works both locally and when the application is packaged as a release? Are there any pitfalls with Mix.Config precedence or release startup that I should be aware of?