我有一个码头容器,其中包含2个图像。我的应用程序和我的数据库( 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中,我的.env是postgresql://admin:admin@postgres:5432/sales
,尽管我为了测试目的将它更改为本地主机,当我在docker之外运行这个应用程序时(而且它可以工作)。
我想问题可能是在docker内部的服务名称,所以我用container_name: postgres
指定了它。
我的船坞-写作:
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 M1上运行这些容器,并且必须对图像进行一些修改才能使其运行。
发布于 2022-10-06 09:39:46
我在这里找到了解决办法
修复方法是在字符串末尾添加?connect_timeout=300
。所以在我的例子中postgresql://admin:admin@postgres:5432/sales?connect_timeout=300
这似乎是节点16、prisma和mac M1的一个问题。为什么添加connect_timeout=300
起作用?我不知道,我在网上找不到答案。但我可以运行我的迁移,现在一切顺利。
https://stackoverflow.com/questions/73970620
复制相似问题