skip to Main Content
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:ProgrammingNodejsLandProsportmastersportmaster_bitbucketnode_modulespg-protocolsrcparser.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

2

Answers


  1. 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;
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search