M. F
M. F

Reputation: 121

Connecting PostgreSQL from TypeORM docker container

I am trying to connect a postgres db to nodejs using typeorm.

I tried doing this with both postgres and nodejs in localhost and it worked fine. However I am having problems when I put postgres and nodejs into docker containers.

(The "IPAdress" field in docker inspect for postgres container is 172.19.0.3)

Error from 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"
    }
  }
]

Thanks

Upvotes: 4

Views: 7912

Answers (1)

Robert
Robert

Reputation: 36843

The default port of PostgreSQL is 5432. Change it in your nodejs config.

The port exposing is not necessary for your nodejs, that is only to link that port to your localhost (or other IPs):

    ports:
      - "5433:5433"

You can remove them.

But, if you need to postgres listen to 5433 anyway, you will need some customization:

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

Upvotes: 1

Related Questions