# Ecto :database Not Found

**URL:** <https://forum.elixirforum.com/t/ecto-database-not-found/20605>\
**Category:** Questions\
**Tags:** ecto, postgres, troubleshooting, ecto-migration\
**Created:** [March 4, 2019, 5:58am UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605 "2019-03-04T05:58:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgiesin](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pgiesin/32/12890_2.png) [@pgiesin](https://forum.elixirforum.com/u/pgiesin)\
**Post date:** [March 4, 2019, 5:58am UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/1 "2019-03-04T05:58:02Z")

</div>

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**

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

```

**Repo**

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

```

**Config**

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

```

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

```elixir
[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

---

<div class="post-metadata">

**Author:** ![scottming](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/scottming/32/13006_2.png) [@scottming](https://forum.elixirforum.com/u/scottming)\
**Post date:** [March 4, 2019, 7:54am UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/2 "2019-03-04T07:54:02Z")

</div>

Did you create the db?

```elixir
mix ecto.create

```

---

<div class="post-metadata">

**Author:** ![schaary](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/schaary/32/831_2.png) [@schaary](https://forum.elixirforum.com/u/schaary)\
**Post date:** [March 4, 2019, 10:05am UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/3 "2019-03-04T10:05:50Z")

</div>

I miss the line

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

```

in your `config/config.ex` file.

---

<div class="post-metadata">

**Author:** ![Sanjer](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@Sanjer](https://forum.elixirforum.com/u/Sanjer)\
**Post date:** [July 9, 2019, 1:17pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/4 "2019-07-09T13:17:54Z")

</div>

Ensure the below and give it a re-try.

**STEP 1. Define the dependencies required, mix.exs**

```elixir
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**

```elixir
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}**

```elixir
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.

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 2:46pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/5 "2019-12-27T14:46:02Z")

</div>

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, 😟  
i do have another app running fine in heroku, i followed the same setups but its not connecting for the new app

```elixir
(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

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 4:23pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/6 "2019-12-27T16:23:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 5:05pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/7 "2019-12-27T17:05:06Z")

</div>

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

```elixir
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

```

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 5:06pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/8 "2019-12-27T17:06:27Z")

</div>

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

Can you also show your `MyAppRepo.ex` file?

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 5:09pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/9 "2019-12-27T17:09:59Z")

</div>

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

```elixir
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.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 5:18pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/10 "2019-12-27T17:18:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 5:44pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/11 "2019-12-27T17:44:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 5:46pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/12 "2019-12-27T17:46:58Z")

</div>

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:

```elixir
System.get_env("DATABASE_URL")

```

And see what’s wrong.

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 6:14pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/13 "2019-12-27T18:14:39Z")

</div>

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

```elixir
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 👍

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 6:21pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/14 "2019-12-27T18:21:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 6:23pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/15 "2019-12-27T18:23:33Z")

</div>

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

```elixir
config :myapp, ecto_repos: [MyApp.Repo]

```

Additionally, have you tried actually adding the `database` key?

```diff
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"

```

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [December 27, 2019, 6:24pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/16 "2019-12-27T18:24:20Z")

</div>

> [@tenzil](#):
>
> which worked fine in elixir 1.8 phoenix app but not in 1.9 phoenix app

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`?

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 6:32pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/17 "2019-12-27T18:32:00Z")

</div>

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"`

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [December 27, 2019, 6:32pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/18 "2019-12-27T18:32:59Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [December 27, 2019, 6:33pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/19 "2019-12-27T18:33:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tenzil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tenzil/32/17367_2.png) [@tenzil](https://forum.elixirforum.com/u/tenzil)\
**Post date:** [December 27, 2019, 6:35pm UTC](https://forum.elixirforum.com/t/ecto-database-not-found/20605/20 "2019-12-27T18:35:41Z")

</div>

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

[Next page](https://forum.elixirforum.com/t/ecto-database-not-found/20605.md?page=2)
