26

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

brstkr
  • 1,423
  • 3
  • 18
  • 28

5 Answers5

70

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"
brstkr
  • 1,423
  • 3
  • 18
  • 28
8

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
abel-mak
  • 121
  • 1
  • 4
  • 17 alphine version isn't working. must be 17 version – magentaqin May 29 '23 at 14:16
  • 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. – RashadArbab Aug 31 '23 at 19:08
1

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

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.

ASR4
  • 545
  • 1
  • 8
  • 24
Freedisch
  • 124
  • 1
  • 9
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33141226) – Josua Marcel C Nov 15 '22 at 18:33
0

With me I just change the database URL:

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

And it works fine.

Bob Nguyen
  • 11
  • 1