Prafful Panwar
Prafful Panwar

Reputation: 437

ECONNREFUSED 127.0.0.1:5432

I am a beginner in node, nest, and docker but somehow I got assigned a job to dockerized all the existing node js applications.

I followed one of the youtube tutorial and successfully deployed the basic hello world via docker but in the next youtube tutorial when I am trying to add Postgres to the docker I am facing some issues in connecting to Postgres.

I am using docker desktop on mac.

Here is my docker-compose.yml file code snippet

version: "3.9"  # optional since v1.27.0
services:
  api:
    build:
      dockerfile: Dockerfile
      context: .
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://user:password@postgres:5432/db
      NODE_ENV: developement
      PORT: 3000
    ports:
      - "8080:3000"
  postgres:
    image: postgres:14.0
    ports:
      - "35000:5432"
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: db

Here is the entire error log

enter image description here

Github Repository of this project

Thank you for helping in advance :)

Upvotes: 1

Views: 12592

Answers (4)

Ok, After some investigation I found out the problem is with host. You should use container_name as a host value

docker-compose.yml is:

version: "3.8"

services:
  db:
    image: postgres:16.1-alpine
    container_name: my-app-db
    restart: unless-stopped
    ports:
      - "${POSTGRES_PORT}:${POSTGRES_PORT}"
    env_file:
      - .env
    volumes:
      - db:/var/lib/postgresql/data

volumes:
  db:

config db is:

const dbConfig = {
  port: Number(process.env.POSTGRES_PORT),
  user: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB
};

export const ormConfig: PostgresConnectionOptions = {
  type: "postgres",
  host: "my-app-db",
  port: dbConfig.port,
  database: dbConfig.database,
  username: dbConfig.user,
  password: dbConfig.password
};

@Module({
  imports: [
    TypeOrmModule.forRoot(ormConfig),
    ...
  ],
  providers: [AppService]
})

Upvotes: 0

In the normal case, without Docker, i. e. you are using node and postgresql on you development or production machine, you just want to start postgres service and enable it if you want.

In order to start your postgres service, type the following command:

sudo systemctl start postgresql

To enable :

sudo systemctl enable postgresql

Note:

  1. "Enable" means enbable the postgres server at boot time.
  2. In my environment, i am using Red Hat. So if commands doesn't work, find the corresponding command on your linux distribution or on your specific OS.

I hope this can help someone else!

Upvotes: -1

Robert-Jan Kuyper
Robert-Jan Kuyper

Reputation: 3346

Make sure your connection string is correct in your docker-compose.yml. Just pass the host, port, user and pass seperated and let TypeOrm handle the connection.

// app.module.ts
TypeOrmModule.forRoot({
    type: 'postgres',
    host: process.env.POSTGRES_HOST,
    port: process.env.POSTGRES_PORT,
    username: process.env.POSTGRES_USER,
    password: process.env.POSTGRES_PASSWORD,
    database: process.env.POSTGRES_DB,
 })

And your docker-compose.yml:

# docker-compose.yml
version: '3.9'
services:
  api:
    build:
      dockerfile: Dockerfile
      context: .
    depends_on:
      - postgres
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_PASSWORD=promo-pass
      - POSTGRES_USER=promo-user
      - POSTGRES_DB=promo-api-db
      - POSTGRES_PORT=5432

  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: promo-user
      POSTGRES_PASSWORD: promo-pass
      POSTGRES_DB: promo-api-db

Upvotes: 0

Pooya
Pooya

Reputation: 3183

Your problem for a typo in DATABASE_URL. In code for connect database use DATABSE_URL word but in docker-compose used DATABASE_URL.

You should change url: process.env.DATABSE_URL to url: process.env.DATABASE_URL

Upvotes: 1

Related Questions