Failed to connect to database cluster (non-existing domain)

I’m trying out Fly’s managed db but I can’t get it to connect with the Phoenix application. I tried using the clustered live counter (elixir-hiring-project/releases.exs at main · superfly/elixir-hiring-project · GitHub) but it doesn’t seem to have db-related configuration.

Error message:

[error] Postgrex.Protocol (#PID<0.379.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (app-postgres.internal:5432): non-existing domain - :nxdomain

I’m certain that the DATABASE_URL was set properly since I managed to log it. The database was created through flyctl postgres create -a app_name --postgres-app postgres_name. I also tried using the postgres user and password with the database name appended to the postgres URL. Nothing works. So this leads me to believe that it may be related to how the db cluster was orchestrated. But I’m not sure.

The app instance, and db cluster are also in the same organization. However, in different regions since I ran into an error where I’m only allowed to run one app in a region.

# config/runtime.exs
import Config

if config_env() == :prod do
  database_url =
    System.get_env("DATABASE_URL") ||
      raise """
      environment variable DATABASE_URL is missing.
      For example: ecto://USER:PASS@HOST/DATABASE
      """

  secret_key_base =
    System.get_env("SECRET_KEY_BASE") ||
      raise """
      environment variable SECRET_KEY_BASE is missing.
      You can generate one by calling: mix phx.gen.secret
      """

  config :app, App.Repo,
    ssl: true,
    url: database_url,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

  config :app, AppWeb.Endpoint,
    server: true,
    http: [
      port: String.to_integer(System.get_env("PORT") || "4000"),
      transport_options: [socket_opts: [:inet6]]
    ],
    secret_key_base: secret_key_base
end
#config/prod.exs
use Mix.Config

config :app, AppWeb.Endpoint,
  load_from_system_env: true,
  http: [port: {:system, "PORT"}],
  url: [host: "***.fly.dev", port: 443], # Intentionally omitted
  force_ssl: [rewrite_on: [:x_forwarded_proto]],
  cache_static_manifest: "priv/static/cache_manifest.json"

Any pointers would be great! I’ve been stuck with this for 4 hours. :frowning:

Hello! We’re working on a guide for this, you need to make your repo config something like this:

  config :app, App.Repo,
    ssl: false,
    socket_options: [:inet6],
    url: database_url,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

This disabled postgres ssl (which isn’t necessary on Fly, the network is encrypted) and enables IPv6. Will you see if that helps?

@kurt Thank you for the prompt response. I will give this a shot. As for the restriction of only having one app per region, is this intended or a bug?

Oh I missed that question! There’s no intentional restriction like that, are you getting an error when you create a new app or attempt to deploy one?

It’s alright I added that in an edit cause I thought it might’ve been a factor.

So I have one app instance in nrt (Japan). I’m trying to spin up a db in that region as well. I tried it just now and this is what I got:

> fly postgres create
# Fill out info for cluster; nrt region

Launching...⣟ Error Can't create volume, application is already using 1 of 1 zones in nrt

Choosing a different region works fine though.

Oh I see, that just means our provisioner thinks our other disk array is full in Tokyo. I’m pretty sure it’s not so that’s definitely a bug. Give us a bit to troubleshoot.

Found and fixed. Will you give it another try? Sorry about that.

@kurt Thanks for taking the time to do this! I’ve tried the changes you’ve mentioned previously and it works.

Confused with how I should do migrations though. I have a Procfile in rel/overlays/Procfile with the contents:

release: web: /app/bin/app eval "App.Release.migrate" && /app/bin/app start

I deployed but it doesn’t seem to be invoking it. I tried the SSH method to workaround this but I ran into this issue IPv4 application internal network - #12 by thomas. Couldn’t follow the last comment cause I’m unfamiliar with WireGuard unfortunately.

Edit: I messed up with release: web:. I’ll deploy again.

We have experimental support for migrations! We’ve been getting the plumbing in place to make Phoenix apps work really well on Fly for the last month. Try adding this to your fly.toml:

[deploy]
release_command = "/app/bin/app eval 'App.Release.migrate'"

We don’t do anything with the release process in a Procfile, but that config will run migrations on deploy.

1 Like