Connect external Vercel Nextjs 13 Prisma application to Fly.io Postgres with ipv6

Hello,

I cannot figure out how to reach my fly.io postgres container from a Nextjs 13 (Prisma DB) app deployed on Vercel.

I have followed the guide on external connections and added an ipv6 address. The ipv4 address costs 2$/month, so I opted for ipv6.

My (auto-generated) fly.toml looks like this:


app = "<my postgres app name>"
primary_region = "<region>"

[env]
  FLY_SCALE_TO_ZERO = "1h"
  PRIMARY_REGION = "<region>"

[[mounts]]
  source = "pg_data"
  destination = "/data"

[[services]]
  protocol = "tcp"
  internal_port = 5432
  auto_start_machines = true

  [[services.ports]]
    port = 5432
    handlers = ["pg_tls"]
  [services.concurrency]
    type = "connections"
    hard_limit = 1000
    soft_limit = 1000

[[services]]
  protocol = "tcp"
  internal_port = 5433
  auto_start_machines = true

  [[services.ports]]
    port = 5433
    handlers = ["pg_tls"]
  [services.concurrency]
    type = "connections"
    hard_limit = 1000
    soft_limit = 1000

[checks]
  [checks.pg]
    port = 5500
    type = "http"
    interval = "15s"
    timeout = "10s"
    path = "/flycheck/pg"
  [checks.role]
    port = 5500
    type = "http"
    interval = "15s"
    timeout = "10s"
    path = "/flycheck/role"
  [checks.vm]
    port = 5500
    type = "http"
    interval = "15s"
    timeout = "10s"
    path = "/flycheck/vm"

[metrics]
  port = 9187
  path = "/metrics"

I have deployed this new configuration and it shows up in my dashboard.

I can connect on my local dev machine to the Postgres app without having to set up a proxy.

The connection string is something like this:

postgres://postgres:<my postgres password>@<my postgres app name>.fly.dev:5432/<my database name>?schema=public

However, using the exact same connection string (copy/paste) on Vercel leads to an error in my runtime logs:

Can't reach database server at `<my postgres app name>.fly.dev`:`5432`

Please make sure your database server is running at `<my postgres app name>.fly.dev`:`5432`.
    at zr.handleRequestError (/var/task/node_modules/.pnpm/@prisma+client@5.0.0_prisma@5.

Any help is appreciated.

I don’t have much experience with Vercel, but AFAIK they run their Serverless Functions on AWS Lambda, which doesn’t support ipv6 for outbound connections - AWS lambda function not able to resolve or connect to ipv6 only domain | AWS re:Post

If dedicated IP cost is an issue I would recommend trying shared IPv4: Announcement: Shared Anycast IPv4

Thanks for the suggestions.

Unfortunately, I now cannot connect externally to the database with a shared ip v4 via connection string or psql without a proxy.

Proxying works fine, the database seems healthy and running.

Does the shared ipv4 allow for connecting an external (Postgres) app, or would I need a dedicated ipv4 for that?

You can assign the shared ipv4 but connect using the .fly.dev domain on your connection string and our proxy should handle everything for you.

That doesn’t seem to work for me, but I appreciate your response.

$ psql "sslmode=require host=<my app name>.fly.dev> user=postgres"
> psql: error: connection to server at "<my-app-name>.fly.dev" (xx.xxx.xxx.xxx), port 5432 failed: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

I was looking for old threads to see if I could spot a solution and found this:

Sorry I might have misled you.

Another possibility I can think of is that you could setup wireguard on Vercel so you have access there to our internal DNS but I’m not sure if this is possible too.

Ah, I understand. Thank you @lubien.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.