Connecting to Supabase database

When I try to connect to our Supabase database instance I get following error:

2022-04-26T14:40:52.358 app[3f13593c] lax [info] 14:40:52.357 [error] Postgrex.Protocol (#PID<0.2106.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db.xxx.supabase.co:5432): non-existing domain - :nxdomain

2022-04-26T14:40:55.630 app[3f13593c] lax [info] 14:40:55.629 [error] Postgrex.Protocol (#PID<0.2114.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db.xxx.supabase.co:5432): non-existing domain - :nxdomain

2022-04-26T14:40:57.079 app[3f13593c] lax [info] 14:40:57.077 [error] Postgrex.Protocol (#PID<0.2112.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db.xxx.supabase.co:5432): non-existing domain - :nxdomain

I am not able to figure why it won’t resolve the DNS. Anyone who can provide any information on this?

I have been fiddling with:

  • inet6 stuff
  • recreated the instance
1 Like

can you share your repo config from config/runtime.exs? (pruned sensitive info if necessary)

It is basically the default one:

import Config

if System.get_env("PHX_SERVER") do
  config :xxx, Xxx.Endpoint, server: true
end

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
      """

  maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []

  config :xxx, Xxx.Repo,
    ssl: true,
    url: database_url,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
    socket_options: maybe_ipv6

  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
      """

  host = System.get_env("PHX_HOST") || "example.com"
  port = String.to_integer(System.get_env("PORT") || "4000")

  config :xxx, Xxx.Endpoint,
    url: [host: host, port: 443, scheme: "https"],
    http: [
      ip: {0, 0, 0, 0, 0, 0, 0, 0},
      port: port
    ],
    secret_key_base: secret_key_base
end

Found the problem: Following was being appended in the Docker file

# Appended by flyctl
ENV ECTO_IPV6 true
ENV ERL_AFLAGS "-proto_dist inet6_tcp"

The database did not support IPv6

2 Likes

this helped me connect to a supabase db as well.
Specifically, I commented out ENV ECTO_IPV6 true

:person_tipping_hand: I had to do the reverse of the changes mentioned above, as Supabase now configure and provide a IPv6 hostname for DATABASE_URL. Adding an environment variable with ECTO_IPV6=true resolved the problem and my app now connects successfully to the Postgres database. :tada:

I had this exact problem, tried your solution adding it as secret with key ECTO_IPV6 and value true. Tried with ssl=true commented out and with it left in.

Still get this error:

23:52:52.386 [error] Postgrex.Protocol (#PID<0.150.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (aws-0-eu-west-3.pooler.supabase.com:6543): non-existing domain - :nxdomain

Apologies, I didn’t include all of the changes which I made so this may be useful for you to resolve your issue. You may also need to make some changes to config/runtime.exs if you’ve previously run mix phx.gen.release.

I’m deploying my application as a Docker container so I added the following environment variables:

ENV ECTO_IPV6=true
ENV ERL_AFLAGS="-proto_dist inet6_tcp"

If you’re deploying your application using another build method then you should be able to set these variables using fly deploy, e.g.

$ fly deploy --env ECTO_IPV6=true --env ERL_AFLAGS="-proto_dist inet6_tcp"

This combined with the extra configuration in config/runtime.exs was enough to enable my application to connect with the Supabase database host over IPv6.

Hope that helps!

Ah! @hagenek – I missed the “pooler” part of the hostname for your database. AFAIK, Supabase provide 2 hostnames in separate environment variables:

DATABASE_URL offers a direct IPv6 connection to your database. Use this URL from your Fly.io applications.

DATABASE_POOLER_URL runs connections through a connection pooler. Currently, the connection pooler runs outside of Fly.io and may introduce connection latency. Use this URL to test connection pooling behavior, or to connect from locations that don’t support IPv6, like many household ISPs.

(source)

From the look of it, you need to use a IPv6 connection string in DATABASE_URL. It appears like you’re trying to connect to a IPv4 address over IPv6 so I’d start there with your investigation.

1 Like

I’m hitting this issue from a fly deployed phoenix app that uses fly ext supabase integration - As far as I can tell, I have the correct settings for ipv6, and am using the DATABASE_URL printed out by fly.

Would modifying queue_target be a possible culprit?

  maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: []

  config :byline_by, MyApp.Repo,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
    socket_options: maybe_ipv6,
    url: database_url
    queue_target: 1000 # could this be the issue?

If I navigate to supabase, I see that that connection is still configured to use ipv4