Error: DBConnection.ConnectionError, ssl_not_started

Hi there, everyone.

Right now I have a few Elixir apps running on a Kubernetes environment and connecting to an external Postgres server, which requires SSL to connect. That’s fine, already had a bunch of those apps connecting with this database before with no issue.

But my latest app is kinda different from the others, it’s using Elixir 1.9.1 releases instead of the old way of doing it having Distillery as a dependency and etc.

Also this app is an umbrella.

That being said, here’s my mix.exs:

[...]
  def project do
    [
      apps_path: "apps",
      version: "0.1.0",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      releases: releases()
    ]
  end

  defp releases do
    [
      runr: [
        applications: [
          database: :permanent,
          engine: :permanent,
          gateway: :permanent,
          shopify: :permanent
        ]
      ]
    ]
  end

[...]

Here’s my config/config.exs file:

import Config

config :database, ecto_repos: [Runr.DataBase.Repo]

config :database, Runr.DataBase.Repo,
  database: System.get_env("POSTGRES_DB"),
  username: System.get_env("POSTGRES_USER"),
  password: System.get_env("POSTGRES_PASSWORD"),
  hostname: System.get_env("POSTGRES_HOST"),
  port: System.get_env("POSTGRES_PORT"),
  pool_size: 5

[...]

This config is used during development/debug. I have a config/releases.exs file just for the released environment:

import Config

config :database, Runr.DataBase.Repo,
  database: System.get_env("POSTGRES_DB"),
  username: System.get_env("POSTGRES_USER"),
  password: System.get_env("POSTGRES_PASSWORD"),
  hostname: System.get_env("POSTGRES_HOST"),
  port: System.get_env("POSTGRES_PORT"),
  pool_size: 10,
  ssl: true,
  prepare: :unnamed

[...]

Notice that for the releases.exs file I’m setting the ssl: true. Because the released app will be connecting using SSL, different from the other environments.

I also have a release tasks file for the database app:

defmodule Runr.DataBase.ReleaseTasks do
  @moduledoc """
  It's used to run common database tasks as Elixir releases commands.

  Check: https://github.com/elixir-ecto/ecto_sql/pull/113
  """

  @app :database

  def migrate do
    Application.ensure_all_started(@app)

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    Application.ensure_all_started(@app)

    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  def seed do
    Application.ensure_all_started(@app)

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &do_seed(&1))
    end
  end

  defp do_seed(repo) do
    seed_script = priv_path_for(repo, "seeds.exs")

    if File.exists?(seed_script) do
      Code.eval_file(seed_script)
    end
  end

  defp priv_path_for(repo, filename) do
    app = Keyword.get(repo.config, :otp_app)

    repo_underscore =
      repo
      |> Module.split()
      |> List.last()
      |> Macro.underscore()

    priv_dir = "#{:code.priv_dir(app)}"

    Path.join([priv_dir, repo_underscore, filename])
  end

  defp repos do
    Application.load(@app)
    Application.fetch_env!(@app, :ecto_repos)
  end
end

This module is used so I can migrate and seed the database as a command within the released app.

Where I run for instance:

$ /app/_build/prod/rel/runr/bin/runr eval "Runr.DataBase.ReleaseTasks.migrate()"

The problem is that when I run this command, that happens:

21:25:54.327 [error] Postgrex.Protocol (#PID<0.213.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.214.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.215.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.217.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.216.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.219.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.220.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.221.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.218.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:54.327 [error] Postgrex.Protocol (#PID<0.222.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:55.971 [error] Postgrex.Protocol (#PID<0.215.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:56.105 [error] Postgrex.Protocol (#PID<0.218.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:56.480 [error] Postgrex.Protocol (#PID<0.217.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:56.725 [error] Postgrex.Protocol (#PID<0.213.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:56.791 [error] Postgrex.Protocol (#PID<0.219.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
21:25:57.012 [error] Postgrex.Protocol (#PID<0.216.0>) failed to connect: ** (DBConnection.ConnectionError) ssl connect: Unexpected error: ssl_not_started - :ssl_not_started
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2970ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information
    (ecto_sql) lib/ecto/adapters/sql.ex:618: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql) lib/ecto/adapters/sql.ex:705: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql) lib/ecto/migrator.ex:567: Ecto.Migrator.verbose_schema_migration/3
    (ecto_sql) lib/ecto/migrator.ex:411: Ecto.Migrator.lock_for_migrations/4
    (ecto_sql) lib/ecto/migrator.ex:335: Ecto.Migrator.run/4
    (ecto_sql) lib/ecto/migrator.ex:89: Ecto.Migrator.with_repo/3
    (database) lib/runr/database/releases_tasks.ex:14: anonymous fn/2 in Runr.DataBase.ReleaseTasks.migrate/0
21:25:57.045 [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 :database, Runr.DataBase.Repo,
      migration_source: "some_other_table_for_schema_migrations"

The full error report is shown below.

I was thinking it was somehow related with me lacking some extra_applications, I don’t really know, do I’ve tried the following on the file apps/database/mix.exs:

[...]

  def application do
    [
      extra_applications: [:logger, :ecto_sql],
      mod: {Runr.DataBase.Application, []}
    ]
  end

[..]

And here’s the apps/database/lib/runr/database/application.ex:

defmodule Runr.DataBase.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: Runr.DataBase.Worker.start_link(arg)
      # {Runr.DataBase.Worker, arg},
      Runr.DataBase.Repo
    ]

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

But that didn’t work. I’m kinda lost here so any insight is welcome.

That’s it, thank you folks!

Have you tried adding :ssl to the extra applications?

2 Likes

You rock! Thank you so much. Strange that I don’t remember of adding this on the other applications that don’t use the new release method.

Anyway, fixed with:
apps/database/mix.exs

[...]

  def application do
    [
      extra_applications: [:logger, :ecto_sql, :ssl],
      mod: {Runr.DataBase.Application, []}
    ]
  end

[...]