M1 Related! - Prisma: Can't reach database server at `database`:`5432`
Asked Answered
E

5

28

Since I have moved to the new Apple Silicon architecture my docker setup with nextjs and postgres is not working anymore. The database inside the docker cannot be found by the nextjs server where I am using prisma.

The prisma client can't reach the postgres database on port 5432.

Can't reach database server at test-postgres:5432

The migration also does not work and returns the same error above.

docker-compose run --publish 5555:5555 next npx prisma migrate dev

docker-compose.yml

postgres:
    container_name: 'test-postgres'
    restart: unless-stopped
    image: 'postgres:13'
    ports:
      - '15432:5432'
    volumes:
      - 'pgdata:/var/lib/postgresql/data/'
    environment:
      POSTGRES_PASSWORD: postgres

.env

DATABASE_URL="postgres://postgres:postgres@localhost:15432/postgres"

I have also added the arm binary target to the schema.prisma schema.prisma


generator client {
  provider        = "prisma-client-js"
  binaryTargets   = ["native", "debian-openssl-1.1.x", "linux-arm-openssl-1.1.x", "linux-musl"]
  previewFeatures = ["orderByRelation", "selectRelationCount"]
}

The postgres container is actually running and I can see it through the Docker Desktop Dashboard. One thing I have noticed inside the postgres container was this ERROR:

2021-07-21 12:52:58.927 UTC [76] ERROR:  relation "_prisma_migrations" does not exist at character 126

Have someone experienced it before and found a solution for it?

[EDIT]

How to reproduce

clone repo, follow README.md and see expected behaviour on a M1 Apple Silicon Machine: https://github.com/baristikir/prisma-postgres-M1

Ergotism answered 21/7, 2021 at 20:40 Comment(0)
E
76

Adding ?connect_timeout=300 to the connection string of the database did the trick.

DATABASE_URL="postgres://postgres:postgres@localhost:15432/postgres?connect_timeout=300"
Ergotism answered 23/7, 2021 at 7:6 Comment(10)
nice, worked for meHypoxanthine
Got this error for two weeks now, thank you so much!Affirm
It definitely does the trick. No idea why though.Bernetta
Dude, literally, I was looking for this like 4 hours or smth, thxManner
No way, how can this work? And it has 36 upvotes. How adding a connection timeout can solve the problem? Can you explain??Insurable
It works! Without knowing what's the cause. Thank you.Hyperparathyroidism
that's my championHeadachy
It doesn't work with my mariadb database (I'm using ssl). Fails instantly on my device when I add the option and on vercel it always times out instantly, too. However on my device it works if I don't specify anything.Lilytrotter
We were setting up our backend with prisma in 8 kubernetes pods Since we made so many connections at one go, it was timing out, and only ~4 pods were able to connect at one go. In our case, this param makes a lot sense, since it increased the timeout for connection from 30s to 5mins, and it worked!!!Brownley
All hail the saviour. HallelujahWeaponry
F
9

I had this issue on m1 mac mini, the solution was changing the version of node from v16.16.0(image node:16) to v17.9.1(node:17), we tried on node:18 it works also. I changed Dockerfile from

FROM node:16

to

FROM node:17
Fugue answered 22/7, 2022 at 21:7 Comment(2)
17 alphine version isn't working. must be 17 version 😂Sherburne
I spent so much time trying to figure this out. Im on an m2 mac book air, this fixed my issue for good. More people should be able to find this when they need it. thank you so much for this comment.Ethanol
D
2

I was using 16.17.0 version of Node. Just switched back to 16.13.2 and the issue is solved.

Dreibund answered 19/8, 2022 at 16:0 Comment(0)
R
0

I got the same issue and I added ?connect_timeout=300 and it worked. I realized it's taking some time, before connecting to the database especially when it's from a cloud provider, therefore the error.

Reynolds answered 9/11, 2022 at 11:3 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewAeneid
M
0

With me I just change the database URL:

DATABASE_URL="postgresql://root:root@<My IP>:5432/<MY DB Name>"

And it works fine.

Madelon answered 27/12, 2022 at 3:30 Comment(2)
What is "My IP"? Your public local IP or 127:0.0.1 ?Wenonawenonah
It's "Your public local IP" hope to help u.Madelon

© 2022 - 2024 — McMap. All rights reserved.