node.jspostgresqldockerprismaapple-m1

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


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


Solution

  • 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"