skip to Main Content

I have a docker container that contains 2 images. My app and my database (postgres) They are both running, the app on :3000 and postgres on :5432 . When i run npx prisma migrate dev --name init I get the following error :

Environment variables loaded from .env
Prisma schema loaded from lib/database/myschema.prisma
Datasource "db": PostgreSQL database "sales", schema "public" at "postgres:5432"

Error: P1001: Can't reach database server at `postgres`:`5432`

Please make sure your database server is running at `postgres`:`5432`.

If I check, the database is created, but no tables are created inside of it. I don’t understand the error, because if the database is created obviously the path is correct and prisma is able to at least run a query.

I am also connected to the database with PgAdmin and I’m able to run query and create tables no problem.

Also, if I stop the app image but leave the postgres image up and change the database url to postgresql://admin:admin@localhost:5432/sales I can run the migrations from my local repo directly with no issues and the tables are created.

Here is my schema.prisma

datasource db {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}

generator client {
  provider = "prisma-client-js"
  binaryTargets = ["native", "linux-arm64-openssl-1.1.x"]

}

model Sale {
  id              Int     @id @default(autoincrement())
  saleId          Int
  uniqueSaleId    String
  createdAt       DateTime
  completedAt     DateTime
  storeId         Int
  vendorId        Int
  customerId      Int
  currency        String
  payment         String
  total           Float
  productSolds  ProductSolds[]
  accountId       String
  account         Account @relation(fields: [accountId], references: [id], onDelete: Cascade)

  @@unique([saleId, accountId])
}

model ProductSolds {
  id                         Int     @id @default(autoincrement())
  productId                  Int
  productSize                  Int
  quantity                   Int
  productPrice               Float
  productCurrency              String
  vat                          Float
  package                      Int
  points                       Int
  discount                   Float
  stockWithdrawal            Int
  creditNote                   Int
  creditNoteId               Int
  productComments              Float
  serialNumber               Float
  lineItemIdReturn           Int
  dateReturn                 Int
  kitchenPos                 Int
  productSupplyPrice           Float
  lineItemId                   Int
  detailCommandeId           Int
  saleId                     Int
  sale                       Sale @relation(fields: [saleId], references: [id], onDelete: Cascade)
}


model EndpointCalled{
  id                         Int     @id @default(autoincrement())
  accountId                  String
  year                       Int
  month                      Int
  day                        Int
  storeId                    Int
  @@unique([accountId,year,month,day,storeId])
}

model Account {
  id   String @id
  Sale Sale[]
}

my DATABASE_URL in my .env is postgresql://admin:admin@postgres:5432/sales although I change it to localhost for testing purposes when I’m running the app outside of docker (and it works) .

I though the issue might be with the service name inside docker, so I specified it with container_name: postgres

My docker-compose :

version: "3.7"
services:
  node:
    build:
      context: .
      dockerfile: devops/docker/dockerfiles/Dockerfile
      target: development
    volumes:
      - .:/home
      - ${GCP_SA_KEYS_PATH}:/gcp-sa-keys
    command: shell
    environment:
      adminBearerTokens: 1234
      GOOGLE_APPLICATION_CREDENTIALS: /gcp-sa-keys/${GCP_SA_KEY}
      DATABASE_URL: ${DATABASE_URL:-postgresql://admin:admin@postgres:5432/sales}
      HIBOUTIK_USER: ${HIBOUTIK_USER}
      HIBOUTIK_API_KEY: ${HIBOUTIK_API_KEY}
    depends_on:
      - postgres
    ports:
      - "3000:3000"
  postgres:
    image: postgres:14.1-alpine
    container_name: postgres
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
    ports:
      - "5432:5432"

In case this is relevant, I am running the containers on a macbook air M1 and had to modify the image a bit to make it run.

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution here

    The fix is to add ?connect_timeout=300 at the end of the string. So in my case postgresql://admin:admin@postgres:5432/sales?connect_timeout=300

    It seems to be an issue with node 16, prisma, and mac M1. why does adding connect_timeout=300 work ? I have no idea and I couldn't find an answer online. But I could run my migrations and everything now runs smoothly.


  2. The database url is not correct. You need something like this:

    DATABASE_URL=driver://username:password@hostname:port/database_name

    And you have

    DATABASE_URL: ${DATABASE_URL:postgresql://admin:admin@postgres:5432/sales}

    Instead of the hostname, you use "postgres". Unless you have a host like that, it’s not going to work. And the error message tells us that you don’t have this host. "localhost" works when connecting local, otherwise your need the ip address or hostname. That depends on your setup.

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