简体   繁体   English

从typeorm docker容器连接postgres

[英]Connecting postgres from typeorm docker container

I am trying to connect a postgres db to nodejs using typeorm. 我正在尝试使用typeorm将postgres数据库连接到nodejs。

I tried doing this with both postgres and nodejs in localhost and it worked fine. 我试着用localhost中的postgres和nodejs做这个,它工作正常。 However I am having problems when I put postgres and nodejs into docker containers. 但是当我将postgres和nodejs放入docker容器时,我遇到了问题。

(The "IPAdress" field in docker inspect for postgres container is 172.19.0.3) (对于postgres容器,docker docker inspect的“IPAdress”字段是172.19.0.3)

Error from Nodejs: Nodejs出错:

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

docker-compose.yml 泊坞窗,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 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"
    }
  }
]

Thanks 谢谢

The default port of PostgreSQL is 5432 . PostgreSQL的默认端口是5432 Change it in your nodejs config. 在nodejs config中更改它。

The port exposing is not necessary for your nodejs, that is only to link that port to your localhost (or other IPs): 端口暴露是没有必要为您的NodeJS,这只是该端口链接到你的本地主机(或其他IP):

    ports:
      - "5433:5433"

You can remove them. 你可以删除它们。

But, if you need to postgres listen to 5433 anyway, you will need some customization: 但是,如果你需要postgres听5433,你需要一些定制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM