2017-05-25 150 views
1

我想使用typeorm将postgres数据库连接到nodejs。从typeorm码头集装箱连接postgres

我试着在localhost中同时使用postgres和nodejs,它工作正常。不过,当我将postgres和nodejs放入泊坞窗容器时,我遇到了问题。

(在docker inspect为postgres的容器中的 “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" 
    } 
    } 
] 

谢谢

回答

0

PostgreSQL的默认端口是。在你的nodejs配置中更改它。

端口暴露是需要为您的NodeJS,这只是该端口链接到你的本地主机(或其他IP):

ports: 
     - "5433:5433" 

你可以删除它们。

但是,如果你需要的Postgres听5433,无论如何,你会需要一些定制:

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

哇,我是个白痴。谢谢修复它。 我故意使它成为5433,因为postgres的localhost实例使用的是端口5432,但由于某种原因,我认为docker容器postgres会自动使用5433。 –