skip to Main Content

I am trying to connect to Postgres using docker in github actions. Here is my github actions file:

    name: Carbon Link backend server CI

on:
  push:
    branches: [ 'develop', 'PC-3-setup-github-actions-on-pr-request' ]
  pull_request:
    branches: [ 'develop', 'PC-3-setup-github-actions-on-pr-request' ]
jobs:
  # label of the container job 
  postgres-test:
    runs-on: ubuntu-latest
    container: node:latest

    services:
      postgres:
        image: postgres:latest
        # `POSTGRES_HOST` is `postgres`
        env:
          POSTGRES_DB: carbonlink
          POSTGRES_PASSWORD: password
          # optional (defaults to `5432`)
          POSTGRES_PORT: 5432
          # optional (defaults to `postgres`)
          POSTGRES_USER: postgres
        ports:
          - 5432:5432
        # set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env: 
      NODE_ENV: test
      DATABASE_URL: postgresql://postgres:password@localhost:5432/carbonlink
    steps:
      # Downloads a copy of the code in your repository before running CI tests
      - name: Check out repository code
        uses: actions/checkout@v2
      - name: Disable husky preparation
        run: npm set-script prepare ''
      - name: Install dependencies
        run: npm install
      - name: Migrate
        run: npm run db:migrate
      - name: Run tests
        run: npm test

Everything works up to this command

Run npm run db:migrate

In which I get this response:

[email protected] db:migrate
./node_modules/.bin/sequelize db:migrate

Sequelize CLI [Node: 17.9.0, CLI: 6.4.1, ORM: 6.3.5]

Loaded configuration file "config/config.js".
Using environment "test".

ERROR: connect ECONNREFUSED 127.0.0.1:5432

This is my config.js file

require('dotenv').config();

module.exports = {
  "development": {
    "username": process.env.POSTGRES_USER,
    "password": process.env.POSTGRES_PASSWORD,
    "database": process.env.POSTGRES_DB,
    "host": process.env.POSTGRES_HOST,
    "dialect": process.env.POSTGRES_DIALECT
  },
  "test": {
    "url": "postgresql://postgres:password@localhost:5432/carbonlink",
    "dialect": 'postgres'
  },
  "production": {
    "username": process.env.POSTGRES_USER,
    "password": process.env.POSTGRES_PASSWORD,
    "database": process.env.POSTGRES_DB,
    "host": process.env.POSTGRES_HOST,
    "dialect": process.env.POSTGRES_DIALECT
  }
}

Here is the docker-compose file for reference:

version: '3'
services:
  postgres:
    container_name: pgDocker
    image: postgres:latest
    environment:
      - POSTGRES_USERNAME
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_TEST_DB
    ports:
      - 5432:5432
    volumes:
      - ./data/:/var/lib/postgresql/data
      - ./create_test_db.sh:/docker-entrypoint-initdb.d/create_test_db.sh
  pgadmin:
    container_name: pgadminDocker
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL
      - PGADMIN_DEFAULT_PASSWORD
    ports:
      - 5555:80
    depends_on:
      - postgres
    volumes:
      - ./setup/servers.json:/pgadmin4/servers.json

The way I would connect locally is to execute docker-compose up that will spin up the container with postgres.

I dont understand why I am not connecting to postgres still. Is there another step I should do? Thanks!

2

Answers


  1. Can you also try the connection string from localhost to 127.0.0.1? I succeeded using your yml configuration on github actions. The only difference is the localhost vs 127.0.0.1.

    Login or Signup to reply.
  2. Had same problem,

    Found the solution, I forgot to add ports into the service description

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