首页 > 解决方案 > Elixir/Phoenix - Connect to external database

问题描述

I connected the main database in the dev.exs and it works fine. But in my project I plan to use several databases. I know that in the file dev.exs can connect multiple databases but this option doesn’t suit me. Databases connections will be stored in the main project database. I want to know: how can I connect to different databases using the elixir code without using a file dev.exs?

标签: phoenix-frameworkelixir

解决方案


You can start multiple instances of your Repo with different connection options.

Then, use the Repo.put_dynamic_repo/1 function to tell the Repo which of the databases should be used for queries in the current process. (The documentation for this function also tells you how to start more of the same repo).

There's also a discussion document that goes more in-depth about this topic: https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html


推荐阅读