我正在尝试使用 typeorm 将 postgres 数据库连接到 nodejs。
我尝试在本地主机中同时使用 postgres 和 nodejs 来执行此操作,并且效果很好。但是,当我将 postgres 和 nodejs 放入 docker 容器时,我遇到了问题。
(postgres 容器的 docker inspect
中的“IPAdress”字段是 172.19.0.3)
Nodejs 错误:
web_1 | error: TypeORM connection error: Error: connect ECONNREFUSED 172.19.0.3:5433
docker-compose.yml
services:
web:
build: .
volumes:
- ./:/app
ports:
- "9001:9001"
links:
- pg_db
pg_db:
image: postgres
ports:
- "5433:5433"
env_file:
- docker.env
ormconfig.json
[
{
"name": "default",
"driver": {
"type": "postgres",
"host": "pg_db",
"port": 5433,
"username": "postgres",
"password": "test",
"database": "testDB"
},
"autoSchemaSync": true,
"entities": [
"src/controller/entity/*.js"
],
"cli": {
"entitiesDir": "src/controller/entity"
}
}
]
谢谢
最佳答案
PostgreSQL 的默认端口是5432。在您的 nodejs 配置中更改它。
你的 nodejs 不需要暴露端口,这只是将该端口链接到你的本地主机(或其他 IP):
ports:
- "5433:5433"
您可以删除它们。
但是,如果你无论如何都需要 postgres 监听 5433,你将需要一些定制:
pg_db:
...
environment:
- PGPORT=5433
...
关于postgresql - 从 TypeORM docker 容器连接 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186303/