簡體   English   中英

從typeorm docker容器連接postgres

[英]Connecting postgres from typeorm docker container

我正在嘗試使用typeorm將postgres數據庫連接到nodejs。

我試着用localhost中的postgres和nodejs做這個,它工作正常。 但是當我將postgres和nodejs放入docker容器時,我遇到了問題。

(對於postgres容器,docker docker inspect的“IPAdress”字段是172.19.0.3)

Nodejs出錯:

web_1    | error: TypeORM connection error:  Error: connect ECONNREFUSED 172.19.0.3:5433

泊塢窗,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 config中更改它。

端口暴露是沒有必要為您的NodeJS,這只是該端口鏈接到你的本地主機(或其他IP):

    ports:
      - "5433:5433"

你可以刪除它們。

但是,如果你需要postgres聽5433,你需要一些定制:

  pg_db:
    ...
    environment:
    - PGPORT=5433
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM