Search code examples
elixirecto

How to configure Ecto at runtime?


Following the setup instructions, I have the following Ecto configuration in my config/config.exs file :

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgresql://postgres@localhost/myrepo",
  size: 20

If my understanding is correct, the config.exs is evaluated at compile-time.

Is there a way to do this configuration step at runtime ?

This is for an app which will be distributed as a compiled binary (via exrm). The end-user should be able to customize the database url and pool size via flags or environment variables, not by editing sys.config


Solution

  • Loading from the system is possible by using {:system, "KEY" } e.g.:

    config :my_app Repo
       url: {:system, "DATABASE_URL" },
       size: {:system, "DATABASE_POOL_SIZE" }
    

    instead

    config :my_app, Repo,
       url: "ecto://postgres:postgres@localhost/ecto_simple",
       size: 20
    

    In this case you set up Ecto to use the system properties. Of course, a user has to configure it.