我有一个包含 2 个镜像的 Docker 容器。我的应用程序和我的数据库(postgres)它们都在运行,应用程序在 :3000 上运行,postgres 在 :5432 上运行。当我运行 npx prisma migrate dev --name init
时,出现以下错误:
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`.
如果我检查,数据库已创建,但其中未创建任何表。我不明白这个错误,因为如果创建了数据库,显然路径是正确的,并且 prisma 至少能够运行查询。
我还使用 PgAdmin 连接到数据库,并且能够毫无问题地运行查询和创建表。
此外,如果我停止应用程序镜像,但保留 postgres 镜像并将数据库 URL 更改为 postgresql://admin:admin@localhost:5432/sales
我可以从我的应用程序运行迁移直接本地存储库,没有任何问题,并且表已创建。
这是我的 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[]
}
我的.env中的DATABASE_URL是postgresql://admin:admin@postgres:5432/sales
,尽管当我在docker之外运行应用程序时,我将其更改为localhost以进行测试(并且它有效)。
我认为问题可能出在 docker 内部的服务名称上,所以我用 container_name: postgres
指定它
我的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"
如果这是相关的,我正在 MacBook Air M1 上运行容器,并且必须稍微修改图像才能使其运行。
最佳答案
我在这里找到了解决方案
修复方法是在字符串末尾添加 ?connect_timeout=300
。所以就我而言 postgresql://admin:admin@postgres:5432/sales?connect_timeout=300
这似乎是 Node 16、prisma 和 mac M1 的问题。为什么添加 connect_timeout=300
有效?我不知道,而且我在网上找不到答案。但我可以运行我的迁移,现在一切都运行顺利。
关于node.js - Prisma 创建数据库但随后表示无法连接到该数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73970620/