fschuindt

fschuindt

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!

Marked As Solved

NobbZ

NobbZ

Have you tried adding :ssl to the extra applications?

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement