Not connecting to database since downloading ASDF

Hello all, I recently installed the asdf package to handle version management on my machine. When I downloaded the package it took some work to get all my projects up and running. I have them all going except for one. when I try to run mix phx.server I get this error.

[error] Could not create schema migrations table. This error usually happens due to the following:

  * The database does not exist
  * The "schema_migrations" table, which Ecto uses for managing
    migrations, was defined by another library
  * There is a deadlock while migrating (such as using concurrent
    indexes with a migration_lock)

To fix the first issue, run "mix ecto.create".

To address the second, you can run "mix ecto.drop" followed by
"mix ecto.create". Alternatively you may configure Ecto to use
another table for managing migrations:

    config :beswick, Beswick.Repo,
      migration_source: "some_other_table_for_schema_migrations"

The full error report is shown below.

[info] Application beswick exited: exited in: Beswick.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (RuntimeError) could not lookup Beswick.Repo because it was not started or it does not exist
            (elixir) lib/keyword.ex:504: Keyword.delete_key/3
            (elixir) lib/keyword.ex:501: Keyword.delete_key/3
            (elixir) lib/keyword.ex:501: Keyword.delete_key/3
            (elixir) lib/keyword.ex:493: Keyword.put/3
            (beswick) lib/beswick/repo.ex:11: Beswick.Repo.init/2
            (ecto) lib/ecto/repo/supervisor.ex:24: Ecto.Repo.Supervisor.runtime_config/4
            (beswick) lib/beswick/repo.ex:2: Beswick.Repo.config/0
            (ecto_sql) lib/ecto/migrator.ex:576: Ecto.Migrator.verbose_schema_migration/3
            (ecto_sql) lib/ecto/migrator.ex:411: Ecto.Migrator.lock_for_migrations/3
            (ecto_sql) lib/ecto/migrator.ex:335: Ecto.Migrator.run/4
            (beswick) lib/beswick/application.ex:30: Beswick.Application.start/2
            (kernel) application_master.erl:277: :application_master.start_it_old/4
** (Mix) Could not start application beswick: exited in: Beswick.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (RuntimeError) could not lookup Beswick.Repo because it was not started or it does not exist
            (elixir) lib/keyword.ex:504: Keyword.delete_key/3
            (elixir) lib/keyword.ex:501: Keyword.delete_key/3
            (elixir) lib/keyword.ex:501: Keyword.delete_key/3
            (elixir) lib/keyword.ex:493: Keyword.put/3
            (beswick) lib/beswick/repo.ex:11: Beswick.Repo.init/2
            (ecto) lib/ecto/repo/supervisor.ex:24: Ecto.Repo.Supervisor.runtime_config/4
            (beswick) lib/beswick/repo.ex:2: Beswick.Repo.config/0
            (ecto_sql) lib/ecto/migrator.ex:576: Ecto.Migrator.verbose_schema_migration/3
            (ecto_sql) lib/ecto/migrator.ex:411: Ecto.Migrator.lock_for_migrations/3
            (ecto_sql) lib/ecto/migrator.ex:335: Ecto.Migrator.run/4
            (beswick) lib/beswick/application.ex:30: Beswick.Application.start/2
            (kernel) application_master.erl:277: :application_master.start_it_old/4

I know this is a common error that I have seen a hundred times, but this time it is different. The database exists, I have reset it a bunch of times and run my migrations, but my app still cant seem to find the database since I started using asdf.

I have also deleted my build running rm -rf _build deps but with no luck.

on asdf I am running

elixir         1.6.6    (set by /Users/jacksobocinski/bowst/product-manager/.tool-versions)
erlang         21.3     (set by /Users/jacksobocinski/bowst/product-manager/.tool-versions)

along with
{:phoenix, "~> 1.4.0"}
{:plug_cowboy, "~> 2.0"}
{:ecto_sql, "~> 3.0"}

Any help would be greatly appreciated, thanks!

What did you do to cause this error?

Have you actually looked at the error message and suggested solutions?

I’d not expect this error to be seen after changing the elixir or erlang version.

Much more after upgrading the database…

Try this

mix ecto.drop

mix ecto.create

mix ecto.migrate

These commands will do the following:

mix ecto.drop will drop your current database
mix ecto.create will create a new database from scratch
mix ecto.migrate will build all your schema and migrations

You should be good to go

If not try to look in your ./config/dev.exs and verify your database credentials

For me these commands worked in an umbrella project

The only change ive made was downloading asdf. Postgres is running and when I psql in I can see the database for this app is there. I am also surprised as to why this is happening, Ive gotten this error before but its always been for the obvious reasons.

Ive tried all these approaches, my database is all set up right and running the following commands dont change anything. Ive been running this app for over a year and I havent changed anything about my database config so all that seems fine to me. But this is what it looks like if that helps

config :beswick, Beswick.Repo,
 adapter: Ecto.Adapters.Postgres,
 username: "beswick_dev",
 password: "",
 database: "beswick_dev",
 hostname: "localhost",
 pool_size: 40,
 timeout: 500_000_00,
 ownership_timeout: 500_000_00

One idea is your pool_size large enough?

Also is the Erlang and Elixir versions you installed compatible?

How did you install asdf? Have you installed the erlang and elixir plugins?

What OS do you have Linux Windows Mac?

Compatibility of Elixir and Erlang seems to be given, at least according to https://hexdocs.pm/elixir/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp.

When you try to mix ecto.drop, do you see an error or is nothing happening or is your DB contents dropped after that?

Updating pool size didnt seem to help.

I am on mac using Mojave.

All my other Phoenix apps work with asdf this is the only one that has stopped working since i installed it.

Here is a link to how I installed asdf https://asdf-vm.com/#/core-manage-asdf-vm

Yes the contents get dropped. All the database commands work as expected, I can drop, create, and migrate with no errors. The problem comes when I try to run the app.

OK then find application.ex it’s somewhere in lib there you should have the Repo in children map as a supervisor

Something like this:

defmodule ElixirApp.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Starts a worker by calling: ElixirApp.Worker.start_link(arg)
      # {ElixirApp.Worker, arg},
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: ElixirApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Did you use brew to install asdf?

Sorry for the confusion, This is not an umbrella app. when I say other apps I just mean other phoenix projects on my machine.

Basically in application.ex I have this

  def start(_type, _args) do
    import Supervisor.Spec

    # Define workers and child supervisors to be supervised
    children = [
      # Start the Ecto repository
      supervisor(Beswick.Repo, []),
      # Start the endpoint when the application starts
      supervisor(BeswickWeb.Endpoint, []),
      # Start your own worker by calling: Beswick.Worker.start_link(arg1, arg2, arg3)
      # worker(Beswick.Worker, [arg1, arg2, arg3]),
      # supervisor(Beswick.ImportSupervisor.start_link([]),[], [name: Beswick.ImportSupervisor])
      Beswick.SchedulAvailabilityImport,
      supervisor(Beswick.ImportSupervisor, [[]])
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options

    opts = [strategy: :one_for_one, name: Beswick.Supervisor]
    start_result = Supervisor.start_link(children, opts)

    # migrate app on start
    path = Application.app_dir(:beswick, "priv/repo/migrations")
    Ecto.Migrator.run(Beswick.Repo, path, :up, all: true)

    start_result
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    BeswickWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

Which I believe is all correct unless something has changed about the syntax in this file.

I git cloned asdf I did not brew install it.

I know this will sound stupid but is the oldest trick in the book:

Have you tried to restart your macbook and try after that and see if the app works?

Haha no that does not sound stupid, I have tried it a few times with no luck :\

OK then:

Do you have any process that is using the psql database or an iex open?

Also try to restart PostgresSQL process and service.(I don’t have a Mac so i don’t know how Mac is using it)

Please do me a favor and tripple check if the owner of the database is correct, and also do a permission check on your current user, is it allowed to read the schema_migrations table?

Does that table actually exist after running ecto.create or at least after running ecto.migrate?

And also as a matter of precaution: What version of ecto and ecto_sql are in your mix.lock?

The owner has permissions of Superuser, Create DB . I can also see that the schema_migrations does exist after I migrate.

In my mix.lock it looks like I have 3.1.4 for both ecto and ecto_sql

I am not exactly sure how to tell if any process is using the database but I feel like restarting my computer would kill that process. I tried restarting and updating postgres with no luck :\

OK: Does the version of phoenix installation support your Ecto version package?\

What version of phoenix do you have installed? The latest is phoenix 1.4.11?

Also try https://hexdocs.pm/mix/master/Mix.Tasks.Deps.Update.html

I am on phoenix 1.4 which require elixir 1.5 or later and I am on elixir 1.6.6 so I dont think that should be an issue.

I may try updating phoenix and see if that helps