Can't establish connection to postgres database

I have followed the documentation for creating a postgres database cluster and attaching it to an existing fly app. Unfortunately I’m not able to establish a connection inside the fly app when deploying.

The App is a next.js application with prisma, by building the project I am also running a migration for the database. But the build fails because of failed connection.

#15 48.45 Error: P1001: Can't reach database server at `top2.nearest....`:`5432`

I have also set private_network = true in my fly.toml but with no success.

One thing to check before further debugging: is that postgres cluster within the same organization as your app? If one is in “personal”, and the other in “something-else”, the connection will fail. I made that mistake with one of mine once :slight_smile:

1 Like

Thank you for the hint! Just checked they’re both in “personal”

No problem. It’s not that then.

Next would be checking the database app is ok, running, etc. Try checking its status (scroll down a bit to see further status check commands e.g fly checks list -a name-pg-app), and see if there are any errors shown:

If not, not sure what else to suggest.

Database is running ok, all status looks good. Also have been able establishing a connection locally via WireGuard but still no success inside the fly app.

I bumped into this, the problem I had is that Ecto was not configured to use ipv6, so I changed runtime.exs to:

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

  config :myapp, MyApp.Repo,
    # other config ...
    socket_options: maybe_ipv6

The ECTO_IPV6 env var seems to be added already by fly.io, but you may need to add it yourself.

More here: Use Fly.io internal DNS for resolving Database_URL - Questions / Help - Elixir Programming Language Forum