Ecto :database Not Found

This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.

Dependencies

{:comeonin, "~> 5.1"},
{:postgrex, "~> 0.14.1"},
{:ecto_sql, "~> 3.0"},

Repo

defmodule AccountService.Repo do
    use Ecto.Repo, 
        otp_app: :account_service,
        adapter: Ecto.Adapters.Postgres
end

Config

config :account_service, AccountService.Repo,
    database: "worksolvr",
    username: "postgres",
    password: "REDACTED",
    hostname: "localhost"

When i attempt to run mix ecto.migrate I receive:

[error] GenServer #PID<0.202.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 if you wish to see all of the details
    (elixir) lib/keyword.ex:389: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:90: Postgrex.Protocol.connect/1
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol

Thanks,
Peter

Did you create the db?

mix ecto.create

I miss the line

...
config :account_service, ecto_repos: [AccountService.Repo]
...

in your config/config.ex file.

1 Like

Ensure the below and give it a re-try.

STEP 1. Define the dependencies required, mix.exs

defp deps do
    [
      {:ecto_sql, "~> 3.1"},
      {:postgrex, "~> 0.14.3"}
    ]
 end

STEP 2. Get the dependencies defined above

mix deps.get

STEP 3. Configure the Repo the application

config :helloapp, ecto_repos: [HelloApp.Repo]

Note: the :helloapp must be match the app name defined in the application.ex file. In my case I have created the application using the below command

mix new hello app --app hello app --sup

STEP 4. Configure the the Repo itself defined above

config :helloapp, HelloApp.Repo,
      host: "localhost",
      port: "5432",
      database: "helloappdb",
      username: "postgres",
      password: "postgres"

STEP 4. Define the HelloApp.Repo module in repo.ex file in /lib/{app name}

defmoudle HelloApp.Repo do
use Ecto.Repo,
    otp_app: :helloapp,
    adapter: Ecto.Adapters.Postgres
end

STEP 5. Now, lets create the database

mix ecto.create

The output should be as below.

Compiling 1 file (.ex)
Generated helloapp app
The database for HelloApp.Repo has already been created.

Hope it helps somebody.

1 Like

Hi folks, I facing the following error with heroku. in local the app connects with db and running fine, only with heroku its throwing the following error.
I am unable to execute mix ecto.setup, :worried:
i do have another app running fine in heroku, i followed the same setups but its not connecting for the new app

(KeyError) key :database not found in: [telemetry_prefix: [:mat, :repo], otp_app: :mat, timeout: 15000, pool_size: 10]
    (elixir) lib/keyword.ex:393: Keyword.fetch!/2
    lib/ecto/adapters/postgres.ex:128: Ecto.Adapters.Postgres.storage_up/1
    lib/mix/tasks/ecto.create.ex:53: anonymous fn/3 in Mix.Tasks.Ecto.Create.run/1
    (elixir) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:783: Enum.each/2
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2

Anyone facing the same error, I even updated my postgrex in mix

Post your entire prod.exs (without the secrets)? Seems like a straightforward case of not having the :database configuration key in the file.

Heroku uses url which has database, host, user, and password. which worked fine in elixir 1.8 phoenix app but not in 1.9 phoenix app

config :MyApp, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5"),
  ssl: true,
  url: System.get_env("DATABASE_URL"),
  show_sensitive_data_on_connection_error: true

Are you sure your app’s atom name is :MyApp and not :myapp?

Can you also show your MyAppRepo.ex file?

1 Like

yeah its “config :myapp, MyApp.Repo”
its a typo.

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :myapp,
    adapter: Ecto.Adapters.Postgres
  use Scrivener, page_size: 10
end

This works fine in dev env (local ), but it gets deployed in heroku as well, but just not getting connected with database.

I am assuming you have a DATABASE_URL environment variable in your Heroku configuration?

yes, i do have. dont know why its not connecting :worried:

Can you invoke a remote shell into your Heroku dyno?

heroku run -a your_app iex -S mix, I think. From there, you can try this:

System.get_env("DATABASE_URL")

And see what’s wrong.

I got this when i ran above command, this error " (RuntimeError) connect raised KeyError exception:" was raised 20 + times before terminating the process.

18:09:36.334 [error] GenServer #PID<0.458.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) lib/keyword.ex:393: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:92: Postgrex.Protocol.connect/1
    (db_connection) lib/db_connection/connection.ex:69: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
18:09:36.338 [info] Application myapp exited: shutdown
iex(1)> {"Kernel pid terminated",application_controller,"{application_terminated,myapp,shutdown}"}
Kernel pid terminated (application_controller) ({application_terminated,myapp,shutdown})

Crash dump is being written to: erl_crash.dump...done

Thanks @dimitarvp, i appreciate your help :+1:

At this point I recommend you generate a new Phoenix project with mix and using the proper switches (namely you want an Ecto Repo, use the same module and app name as your current project etc.) and then just search for the differences between your project and the freshly generated one.

BTW, do you also have this in your config file?

config :myapp, ecto_repos: [MyApp.Repo]

Additionally, have you tried actually adding the database key?

config :MyApp, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5"),
  ssl: true,
  url: System.get_env("DATABASE_URL"),
  show_sensitive_data_on_connection_error: true,
+ database: "your_db_name_here"

How do you deploy? And have you updated your config from Mix.Config to Config, and especially have you done that consequently for all files in config?

As its an heroku app, i just do
git push heroku master this does the trick.

And @dimitarvp, i haven’t tried that database: "your_db_name_here"

1 Like

I should have recommended that earlier, but I assumed you had it somewhere.

Basically, you can’t go without that. You need a database name in which your app to persist stuff! :slight_smile:

1 Like

That does not answer any of my questions…

Does your buildpack use releases or mix phx.server?

Have you properly updated your config files for 1.9?

No, it doesn’t use releases, I have web: MIX_ENV=prod mix phx.server in my procfile