skip to Main Content

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

5

Answers


  1. Chosen as BEST ANSWER

    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"
    

  2. 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
    
    Login or Signup to reply.
  3. I was using 16.17.0 version of Node.
    Just switched back to 16.13.2 and the issue is solved.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. With me I just change the database URL:

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

    And it works fine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search