Error connecting to postgres from app

Hi!

I am evaluating fly and render for my new app and was having some build errors while deploying the remix blues stack on fly. Here is the repo: GitHub - yasoob/remix-fly-test

This is the error I encounter while launching the fly app:

 2023-02-22T11:23:52.602 app[5e95e2bf] ams [info] Starting init (commit: 08b4c2b)...

2023-02-22T11:23:52.630 app[5e95e2bf] ams [info] Preparing to run: `docker-entrypoint.sh npx prisma migrate deploy` as root

2023-02-22T11:23:52.645 app[5e95e2bf] ams [info] 2023/02/22 11:23:52 listening on [fdaa:1:4b20:a7b:23c2:5e95:e2bf:2]:22 (DNS: [fdaa::3]:53)

2023-02-22T11:23:55.190 app[5e95e2bf] ams [info] npm WARN exec The following package was not found and will be installed: prisma@4.10.1

2023-02-22T11:24:01.490 app[5e95e2bf] ams [info] Prisma schema loaded from prisma/schema.prisma

2023-02-22T11:24:01.554 app[5e95e2bf] ams [info] Datasource "db": PostgreSQL database "postgres", schema "public" at "[fdaa:1:4b20:0:1::4]:5432"

2023-02-22T11:24:01.767 app[5e95e2bf] ams [info] Error: P1001: Can't reach database server at `[fdaa:1:4b20:0:1::4]`:`5432`

2023-02-22T11:24:01.767 app[5e95e2bf] ams [info] Please make sure your database server is running at `[fdaa:1:4b20:0:1::4]`:`5432`.

2023-02-22T11:24:01.811 app[5e95e2bf] ams [info] npm notice

2023-02-22T11:24:01.812 app[5e95e2bf] ams [info] npm notice New major version of npm available! 8.19.3 -> 9.5.0

2023-02-22T11:24:01.812 app[5e95e2bf] ams [info] npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.5.0>

2023-02-22T11:24:01.812 app[5e95e2bf] ams [info] npm notice Run `npm install -g npm@9.5.0` to update!

2023-02-22T11:24:01.812 app[5e95e2bf] ams [info] npm notice

2023-02-22T11:24:02.651 app[5e95e2bf] ams [info] Starting clean up. 

I verified that I can connect to the db using fly postgres connect --app <db_app> and I also connected the db to my using fly postgres attach --app <app> <db_app>.

What might be causing this error? I have been trying to debug this for hours now and would really appreciate some help.

My app name is productotter and db app is productotter-db.

1 Like

It looks like your setup is having a hard time with the ipv6 address.

As a workaround, you can replace the ipv6 address within your DATABASE_URL to:
productotter-db.internal

You may need to manually detach the existing attachment and re-attach if you don’t have a reference to your connection string. The attach command will spit out your DATABASE_URL and it should look something like this:

postgres://<app-name>:<password>@[fdaa:0:2e26:0:1::6b]:5432/<app-name>?sslmode=disable

Swap out the ipv6 address with the following:

postgres://<app-name>:<password>@productotter-db.internal:5432/<app-name>?sslmode=disable

Then overwrite the DATABASE_URL by running:

fly secrets set DATABASE_URL=postgres://<app-name>:<password>@productotter-db.internal:5432/<app-name>?sslmode=disable
1 Like

Hi @shaun! This did the trick for me. Thanks!

I had the same issue but, the workaround from @shaun fixed it. Thanks.

Unfortunately this is not working for me:

Error: P1001: Can't reach database server at `productotter-db.internal`:`5432`
1 Like

Same here re. productotter-db.internal not being reached, did you find a solution?

Hi guys! It is not supposed to be productotter-db.internal for you. This is my DB app’s name with .internal added to it. For you it will be <your_db_app_name>.internal (whatever your DB app’s name is).

This name is the same as the one you use when you execute this command:

flyctl postgres create --name <your_db_app_name>

I hope this helps :slight_smile:

2 Likes

Ha what a muppet! Thanks @yasoob, I somehow missed the last line of your post and assumed productotter was an internal reference for Fly.io. All works for me now, excellent stuff. (Thanks also to @shaun for the solution originally).

We have a solve for this! New Feature: .flycast domains

Ran into the same problem in a next app. Changing the host from [APP].flycast to [APP].internal fixed it for me.