Problem with Ecto/Postgres database url

This should be a basic Ecto problem but I’m banging my head against the wall here :021:

I’m writing an umbrella project with a single repo.

This configuration works:

config :games, Games.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "games_test",
  pool: Ecto.Adapters.SQL.Sandbox

This does not:

config :games, Games.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: "ecto://postgres:postgres@localhost/games_test",
  pool: Ecto.Adapters.SQL.Sandbox

I’m using Ecto 2.2.7 and Postgrex 0.13.3. Is there something I’m missing? Trying to follow:

Oh yes, and this is the error that I’m seeing after running mix test (a ton of them):

20:47:02.132 [error] GenServer #PID<0.6551.0> terminating
** (KeyError) key :database not found in: [pool: DBConnection.Poolboy, hostname: "localhost", username: "jason", types: Ecto.Adapters.Postgres.TypeModule, port: 5432, otp_app: :games, repo: Games.Repo, timeout: 15000, pool_timeout: 5000, adapter: Ecto.Adapters.Postgres]
    (elixir) lib/keyword.ex:371: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:76: Postgrex.Protocol.connect/1
    (db_connection) lib/db_connection/connection.ex:134: 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

In your error if you have username "jason" so are you sure you are running the code you are showing us?

Please check your implementation of init/2 callback for the repo module, if you’re not overriding the url option.


Indeed that was exactly right. These are the contents of my Repo currently:

defmodule Games.Repo do
  use Ecto.Repo, otp_app: :games

  @doc """
  Dynamically loads the repository url from the
  DATABASE_URL environment variable.
  def init(_, opts) do
    {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}

So basically this always over-writes any url you put in your config/config.exs. Judging from the output of mix --umbrella I think it would be nice to update the docs to give some sort of warning in this case. And perhaps that default generated code should only over-write :url if DATABASE_URL is actually set.


Sounds like:


Yeah, that’s exactly it. Glad that the generator is already fixed for the next version.