How to use Ecto's url config?

config :dashboard, Dashboard.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "riina",
  password: "",
  database: "riina_dev",
  hostname: "localhost",
  pool_size: 10

I’m trying to convert this to a simple url config but everything i’ve tried just isn’t working, I’ve tried just about every url config imaginable and none work.

** (RuntimeError) Connect raised a KeyError error.

Any ideas what i’m doing wrong?

:wave:

Can you post the full errors you are getting and the URLs you’d tried which resulted in these errors?

I’d try

config :dashboard, Dashboard.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "postgres://riina:@localhost/riina_dev",
  pool_size: 10

Sure, this is the error I get when I try your url…

[error] GenServer #PID<0.320.0> terminating
** (RuntimeError) Connect raised a KeyError error. The exception details are hidden, as
they may contain sensitive data such as database credentials.

    (elixir) lib/keyword.ex:377: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:98: Postgrex.Protocol.connect/1
    (db_connection) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

Let me please ask a small question. Can you please tell us which version of ecto is specified in your mix.exs and mix.lock files?

    mix.exs
    {:ecto, "~>2.2.10"},
    
    mix.lock
    "ecto": {:hex, :ecto, "2.2.10", "e7366dc82f48f8dd78fcbf3ab50985ceeb11cb3dc93435147c6e13f2cda0992e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},

Sad :frowning: I had hoped you had for some reason a pre 2.0 ecto.

So the root cause is something else.

I took a look into the postgrex as well, and assuming you had 0.13.5, then the only thing that can raise on line 98 is Keyword.fetch!(opts, :database)

For me it looks as if database is extracted correctly from the URL when I look at the parse_url/1 from ecto. And for me it looks as if it is properly merged into the options for the adapter.

So I can’t really tell whats going on here.

Yeah its really weird. I don’t understand whats going on, but for now only solution is to split the config back to how it was originally and avoid using a database url:

Yeah I am using 0.13.5… oh well, thanks for trying to help!

For anyone in the future with this problem, it was a problem in my Repo.init/2

It was overriding the :url variable with a system variable that was undefined.

Apparently if you use Ecto as a standalone it generates repo code that automatically overrides :url with a system variable making the :url setting useless unless the code is removed.

This was patched in phoenix where it checks if the system variable exists before overriding, but not in ecto…

3 Likes