Ecto apparently not finding config.exs

I’m connecting Ecto to a Postgres container (that I was able to connect using python) but I get the following errors as I have not mt config file

00:02:07.027 [error] Postgrex.Protocol (#PID<0.267.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.260.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.265.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.259.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.258.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.263.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.261.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.266.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.264.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

00:02:07.027 [error] Postgrex.Protocol (#PID<0.262.0>) failed to connect: ** (ArgumentError) missing the :database key in options for Friends.Repo

my config file inside config dir:

import Config

config :testes_pay, ecto_repos: [MyApp.Repo],
  database: "friends",
  username: "pedri",
  password: "1234",
  port: 8080,
  hostname: "localhost"

my repo.ex inside my_app dir

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :testes_pay,
    adapter: Ecto.Adapters.Postgres
end

my application.ex inside my_app dir:

defmodule TestesPay.Application do

  use Application

  def start(_type, _args) do
    children = [
      MyApp.Repo,
    ]

    opts = [strategy: :one_for_one, name: Myapp.Supervisor]  # Add a space after name:
    Supervisor.start_link(children, opts)
  end

end

my mix.exs file:

defmodule TestesPay.MixProject do
  use Mix.Project

  def project do
    [
      app: :testes_pay,
      version: "0.1.0",
      elixir: "~> 1.16",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger],
      mod: {TestesPay.Application, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:req, "~> 0.5.0"},
      {:ecto_sql, "~> 3.6"},
      {:postgrex, ">= 0.0.0"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

I also get the following error when trying to run mix ecto.create

 ** (KeyError) key :database not found in: [
  telemetry_prefix: [:my_app, :repo],
  otp_app: :testes_pay,
  timeout: 15000,
  pool_size: 10
]
    (elixir 1.16.3) lib/keyword.ex:599: Keyword.fetch!/2
    (ecto_sql 3.11.3) lib/ecto/adapters/postgres.ex:179: Ecto.Adapters.Postgres.storage_up/1    
    (ecto 3.11.2) lib/mix/tasks/ecto.create.ex:58: anonymous fn/3 in Mix.Tasks.Ecto.Create.run/1
    (elixir 1.16.3) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
    (mix 1.16.3) lib/mix/task.ex:478: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.16.3) lib/mix/cli.ex:96: Mix.CLI.run_task/2
    c:/Program Files/Elixir/bin/mix:2: (file)

Configs should be set on repo:

config :testes_pay, MyApp.Repo,
  database: "friends",
  username: "pedri",
  password: "1234",
  port: 8080,
  hostname: "localhost"
6 Likes

Sorry I didnt understood, can you elaborate more? Ecto getting started says the config for connecting must be located at config/config.exs

Your first post lacks some essential file structure information. You’d better create a minimal Git repo to reproduce your issue, and post the link here. Then we can provide you with more targeted assistance.

1 Like

You posted what you had in config.exs. @c4710n posted what you should have in config.exs instead. He isn’t suggesting a different file.

1 Like

Ok, ill try to represent better next time. Just to close, the problem was I miss read the docs, it Said “Add this line config :friends, ecto_repos: [Friends.Repo]”, Instead I replaced it for the first config line and was not correctly setting on repo. I added at the bottom and worked just fine

import Config

config :testes_pay, MyApp.Repo,
  database: "friends",
  username: "pedri",
  password: "1234",
  port: 8080,
  hostname: "localhost"

config :testes_pay, ecto_repos: [MyApp.Repo]

1 Like

Everyone makes mistakes. Anyway, welcome to the Elixir community. :wink:

(And, keep moving.

2 Likes