2017-05-25 207 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。 –