1
I got the project, I try the game and it gives out:

ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
    error: ������������ "admin" �� ������ �������� ����������� (�� ������)
        at Parser.parseErrorMessage (E:\Programming\Nodejs\LandPro\sportmaster\sportmaster_bitbucket\node_modules\pg-protocol\src\parser.ts:369:69)

The project has a container defined in docker-compose.yml, but the application itself is local - not in docker

  1. I run docker-compose up -d the container is created, I look at the necessary database is created

  2. I launch the nest start application and the error is - it cannot connect to the database

    docker-compose.yml:

version: '3.8'

services:
  db:
    container_name: sportmaster
    image: postgres:14.4
    restart: always
    environment:
      - POSTGRES_USER=${TYPEORM_USERNAME}
      - POSTGRES_PASSWORD=${TYPEORM_PASSWORD}
      - POSTGRES_DB=${TYPEORM_DATABASE}
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - ${TYPEORM_PORT}:${TYPEORM_PORT}

.env:

TYPEORM_USERNAME=admin
TYPEORM_PASSWORD=admin
TYPEORM_DATABASE=sportmasterDB
TYPEORM_PORT=5432

typeOrm config:

const ormconfig: TypeOrmModuleOptions = {
  type: 'postgres',
  username: process.env.TYPEORM_USERNAME,
  password: process.env.TYPEORM_PASSWORD,
  database: process.env.TYPEORM_DATABASE,
host: 'localhost',
  port: 5432,
  logging: false,
  entities: [
    Order,
  ],
  synchronize: true,
  autoLoadEntities: true,
};

export default ormconfig;

docker-compose ps:

Name Command State Ports

sportmaster docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp

3 Answers 3

6

I've faced the same problem recently. In my case it was caused by postgres service running on Windows and listening to the same port. Apparently, I installed it some time ago and completely forgot. I'm almost sure you have the same problem. Just try to stop the postgres service.

In order to stop Windows service press Win+R, then type services.msc, hit enter, find posgress service, stop it. You can also change it's startup type to manual to prevent it from starting at boot.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! You saved me, how did you figure out this answer?
Hi @Simon, I'm so glad I've saved you some time. I remember it took me several days trying to find some usefull information. Eventually I started to dig into localization issues since error message contains wrongly decoded characters. Then out of nowhere this idea pops up in my mind - maybe I have another running instance of Postgres DB on my machine 🙂
0

You need to provide "host" in your database settings.

Try this:

const ormconfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: db,
  username: process.env.TYPEORM_USERNAME,
  password: process.env.TYPEORM_PASSWORD,
  database: process.env.TYPEORM_DATABASE,
  port: 5432,
  logging: false,
  entities: [
    Order,
  ],
  synchronize: true,
  autoLoadEntities: true,
};

export default ormconfig;

The host is a docker-compose database service name.

You can add this variable in your ".env" file.

.env:

TYPEORM_HOST=db
TYPEORM_USERNAME=admin
TYPEORM_PASSWORD=admin
TYPEORM_DATABASE=sportmasterDB
TYPEORM_PORT=5432

typeOrm config:

const ormconfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: process.env.TYPEORM_HOST,
  username: process.env.TYPEORM_USERNAME,
  password: process.env.TYPEORM_PASSWORD,
  database: process.env.TYPEORM_DATABASE,
  port: 5432,
  logging: false,
  entities: [
    Order,
  ],
  synchronize: true,
  autoLoadEntities: true,
};

export default ormconfig;

6 Comments

I tried: host: '0.0.0.0', host: 'localhost', host: 'db', no result
If your NestJS project are not in docker-compose file, you can't use db service name in host. I think "127.0.0.1" is a correct value for host in your case. Try to check if you get correctly environment variable in your typeOrm config. If environment variables are undefined, try to implement a module provide by nest to manage environment variables docs.nestjs.com/techniques/configuration
I tried to write all the settings for the database in typeorm directly in lines without using variables
I tried to connect to the database from the VSCode plugin, the result is the same
Can you try this commands to check if you can connect to the database ? In terminal : docker exec -it CONTAINER_ID bash psql -U admin \c sportmasterDB
|
0

i have same problem with you and here is my solution:

  1. You must define

    DATABASE_URL: postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres-db:5432/${DB_DATABASE}
    

    inside docker compose for backend service connect to the postgres db. here is my docker-compose file:

version: '4.0'
services:
  db:
    image: postgres
    container_name: postgres
    environment:
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_DATABASE}
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
  backend:
    build: .
    container_name: backend
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgresql://${DB_USERNAME}:${DB_PASSWORD}@postgres-db:5432/${DB_DATABASE}
    depends_on:
      - db
    volumes:
      - .:/app
      - /app/node_modules
volumes:
  db_data:

then change the host(DB_HOST) in .env file equal to "db" (because you named postgres is "db" in docker-compose file)

PORT=3000

DB_HOST=db
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=123456
DB_DATABASE=auth

the typeORM config

 TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port: +configService.get('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_DATABASE'),
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: true,
        logging: true
    }),
    inject: [ConfigService],
    }),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.