postgresql - 从 TypeORM docker 容器连接 PostgreSQL

标签 postgresql typescript docker typeorm

我正在尝试使用 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/

相关文章:

azure - 向 Azure 容器实例入口点提供参数

java - 将证书添加到 keystore 和信任库

node.js - 使用 Sequelize 和 Postgres 发送 POST 请求后,为什么我的外键列返回两次?

postgresql - 如何从Postgres 9.4转储大对象数据,然后导入到Postgres8.x?

angular - 在 Typescript 中使用 promise

javascript - 如何访问 Angular 模板中的 map 对象

linux - 有没有办法检查和清理在 Linux 下运行的 ASP.NET Core 应用程序的证书吊销列表缓存?

python - 将查询结果赋值给变量

sql - 在外键中添加 NULL

javascript - TS2304 : Cannot find name 'stripe'