Can not connect to postgres

I am trying to execute integration tests against a secondary Postgres service container and a RabbitMQ container. Here is my config file

version: 2.1

jobs:
  integration-test:
    docker:
      - image: circleci/node:latest
      - image: postgres
#      - image: circleci/postgres:9.6.5-alpine-ram
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres123
      - image: rabbitmq
        environment:
          RABBITMQ_DEFAULT_USER: jira
          RABBITMQ_DEFAULT_PASS: jira123
          RABBITMQ_DEFAULT_VHOST: "/"
    steps:
      - checkout
      - *restore_dependencies
      - *restore_build
      - run:
          name: Wait for service containers
          command: |
            for i in `seq 1 10`;
            do
              nc -z localhost 5432 && nc -z localhost 5672 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for secondary containers && exit 1
      - run:
          name: Initializing db
          command: |
            yarn db-migrate:test
      - run:
          name: Running integration tests
          command: |
            yarn test:integration

*restore_dependencies and *restore_build, restores the cached node_modules and build folders from previous jobs. I know there is no issue here because the integration tests that do not depend on Postgres pass successfully.

“Initializing db” step uses an npm library called “db-migrate” and respective npm command is

db-migrate up -e test -v

“Running integration tests” refers to an npm command that uses “jest” to run some integration tests

jest --testPathPattern=intg\\.ts$ --passWithNoTests

The tests fail because of failing to connect to Postgres

Error: Connection terminated due to connection timeout
  • “Wait for service containers” step passes. I see the tests also successfully connects to RabbitMQ service container from test logs.

  • “Initializing db” also passes. I see the the successful logs with the following connection parameters

    [INFO] Using test settings: {
      driver: 'pg',
      user: 'postgres',
      password: '******',
      host: 'localhost',
      database: 'postgres',
      port: 5432,
      schema: 'jira' }
    
  • The tests use correct Postgres connection configuration. I see it in the logs

    2020-06-08T08:08:36.065Z - Configuration[debug]: Postgres client params are {"host":"localhost","port":5432,"database":"postgres","user":"postgres","password":"******"}
    

Also this whole setup works successfully and all integration tests pass, when I work locally with my local Docker server using the following docker-compose file

version: '3.1'

services:
  db:
    image: postgres
    container_name: postgres
    #restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres123
    ports:
      - 5432:5432

  queue:
    image: rabbitmq:3-management
    container_name: rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: jira
      RABBITMQ_DEFAULT_PASS: jira123
      RABBITMQ_DEFAULT_VHOST: "/"
    ports:
      - 15672:15672
      - 5672:5672

I tried this both with official Postgres Docker image and the CircleCI Postgres image. Both are failing.

What am I doing wrong?

integration tests run successfully when I pull circleci node image to version 12

circleci/node:12

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.