Ecto database connection with third party library

I’m pretty new to phoenix/elixir. I’ve ported a service from ruby to phoenix (it wasn’t using ecto or a relational database) and looking to do the same with another existing api (which does use a relational database).

I’m looking to do something that would be relatively simple in python (although I know I’m not in python or ruby). I’m in AWS so looking to use amazon secrets manager for storing secrets including the database url. I’ve been searching the forum and anywhere I can but it doesn’t look like this is an option.

Getting the database_url using ex_aws outside of the config is simple enough but it doesn’t look like I can use any dependencies in the config. I’m wondering if there is some way to define the connection outside of the config or some other way to make this work?

You would like to dynamically configure the Ecto repo without using the config method, is that correct? Yes, that is possible. You can do so in your repo module within the init/2 function. The init function should return {:ok, keyword}. Check out the Ecto.Repo docs for more info. Here is an example:

defmodule PointOfSale.Repo do
  use Ecto.Repo,
    otp_app: :point_of_sale,
    adapter: Ecto.Adapters.Postgres

  def init(_type, config) do
    {:ok, Keyword.put(config, :url, System.get_env("DATABASE_URL"))}
  end
end
5 Likes

Thanks! I’ll give this a try today.

1 Like

I’m glad that solution worked out for you :+1:

1 Like

It did! Thanks again and sorry for the late reply

1 Like