Phoenix / Ecto multiple repo

Hello, I try to use a different repo for each data model : User, Comment and News.

I created a separate file for each repo like this :

# file repo_news.ex
defmodule ScienceNews.NewRepo do
  use Ecto.Repo,
    otp_app: :science_news,
    adapter: Ecto.Adapters.Postgres
end

# file repo_comment.ex
defmodule ScienceNews.CommentRepo do
  use Ecto.Repo,
      otp_app: :science_news,
      adapter: Ecto.Adapters.Postgres
end

# file repo_user.ex
defmodule ScienceNews.UserRepo do
  use Ecto.Repo,
      otp_app: :science_news,
      adapter: Ecto.Adapters.Postgres
end

config.exs :

config :science_news,
  ecto_repos: [
    ScienceNews.NewRepo,
    ScienceNews.CommentRepo,
    ScienceNews.UserRepo
  ]

application.ex :

children = [
  # Start the Ecto repository
  ScienceNews.NewRepo,
  ScienceNews.CommentRepo,
  ScienceNews.UserRepo,

However running this I get this error :

[error] GenServer #PID<0.455.0> terminating
** (RuntimeError) connect raised KeyError exception: key :database not found. The exception details are hidden, as they may contain sensitive data such as database credentials. You may set :show_sensitive_data_on_connection_error to true when starting your connection if you wish to see all of the details
    (elixir 1.10.4) lib/keyword.ex:399: Keyword.fetch!/2
    (postgrex 0.15.5) lib/postgrex/protocol.ex:92: Postgrex.Protocol.connect/1
    (db_connection 2.2.2) lib/db_connection/connection.ex:69: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.13) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil

I restarted the server and I tried :
mix ecto.migrate
mix ecto.migrate -r ScienceNews.UserRepo

I test in local.

Have I missed some configuration ? Is this possible ? :blush: :blush:

Thank you so much for your help :sunflower:

Yes, it is. I need to dig little more into your post to help you (I am on mobile right now), but first the main question - why would you do that?

1 Like

You need to configure connection details for each repository separately, just like this:

config :t2, ScienceNews.NewRepo,
  username: "postgres",
  password: "postgres",
  database: "t2_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

config :t2, ScienceNews.CommentRepo,
  username: "postgres",
  password: "postgres",
  database: "t2_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

...
2 Likes

Thank you so much, it works, that is the missing part :sunflower:

I changed dev.exs like this :

# News
config :science_news, ScienceNews.NewsRepo,
  username: "postgres",
  password: "postgres",
  database: "science_news_dev",
  hostname: "localhost",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

# Comment
config :science_news, ScienceNews.CommentRepo,
       username: "postgres",
       password: "postgres",
       database: "science_news_dev",
       hostname: "localhost",
       show_sensitive_data_on_connection_error: true,
       pool_size: 10

# User
config :science_news, ScienceNews.UserRepo,
       username: "postgres",
       password: "postgres",
       database: "science_news_dev",
       hostname: "localhost",
       show_sensitive_data_on_connection_error: true,
       pool_size: 10

And then run :

mix ecto.gen.migration create_new_repo
mix ecto.migrate  
mix phx.server

Thanks :sunflower:

2 Likes