"Ecto.Migrator.run (RuntimeError) connect raised KeyError exception: key :database not found." in production

I’ve deploing my Phoenix project on a server manually multiple times and it works well. Now I need to be able to run seeds. Here’s a partial code:

defmodule MyApp.ReleaseTasks do
  @start_apps [
    :crypto,
    :ssl,
    :postgrex,
    :ecto,
    :ecto_sql
  ]

  @my_apps [
    :my_app
  ]

  @repos Application.get_env(:my_app, :ecto_repos, [])

  def seed do
    my_app_name = :my_app
    IO.puts "Loading #{my_app_name}..."
    # Load the code for my_app_name, but don't start it
    _ = Application.load(my_app_name)

    IO.puts "Starting dependencies.."
    # Start apps necessary for executing migrations
    Enum.each(@start_apps, &Application.ensure_all_started/1)

    # Start the Repo(s) for my_app_name
    IO.puts "Starting repos.."
    Enum.each(@repos, &(&1.start_link(pool_size: 1)))

    # Run migrations
    Enum.each(@my_apps, &run_migrations_for/1)

    # Run the seed script if it exists
    seed_script = Path.join([priv_dir(my_app_name), "repo", "seeds.exs"])
    if File.exists?(seed_script) do
      IO.puts "Running seed script.."
      Code.eval_file(seed_script)
    end

    # Signal shutdown
    IO.puts "Success!"
    :init.stop()
  end

I’ll run it as MIX_ENV=prod ./bin/my_app eval "MyApp.ReleaseTasks.seed" and it’ll cause an exception:

"Ecto.Migrator.run (RuntimeError) connect raised KeyError exception: key :database not found." in production

I know when such an error happens. However, the database production config files do exist and an app works well in production in general. It’s merely that running the command above will produce an error.

And an error won’t occur when calling the seed function via Elixir console in production, if I remember correctly

Why? How to fix it?

I don’t consider edeliver and the like.

Elixir 1.9. And I’m using config/prod.secret.exs