Vadym Urupa
Vadym Urupa

Reputation: 41

Error Unable to obtain connection from database for user postgres

I'm trying to test ci-cd for database ( Postgres, flyway ) from docker-compose file. I create falyway-migrate.yml and it fails due to connections:

ERROR: Unable to obtain connection from database (jdbc:postgresql://172.31.0.2:5432/a_site_to_order_stuff?sslmode=require) for user 'postgres': The connection attempt failed.

enter image description here

my docker-compose file:

version: "3.9"

services:
  db:
    container_name: postgres
    image: postgres:15.4-alpine
    environment:
      - POSTGRES_DB=a_site_to_order_stuff_local
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5433:5432"

  flyway:
    container_name: flyway
    image: "flyway/flyway:9.22-alpine"
    command: -url=jdbc:postgresql://db:5432/a_site_to_order_stuff_local -user=postgres -password=postgres -connectRetries=60 migrate
    volumes:
      - ./sql/:/flyway/sql
    depends_on:
      - db

my falyway-migrate.yml

name: "Flyway CI"

on:
  push:
    branches:
      - main
    paths:
      - "sql/**"
      - ".github/workflows/*flyway*.yml"

jobs:
  flyway-migrate-dev:
    name: Flyway Migrate Dev
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      
      - name: Flyway migrate dev
        run: >
          docker run -v $(pwd)/sql/:/flyway/sql flyway/flyway:9.22-alpine
          -url=jdbc:postgresql://172.31.0.2:5432/a_site_to_order_stuff?sslmode=require
          -user=postgres
          -password=postgres
          -connectRetries=1
          -validateMigrationNaming=true
          migrate
        env:
          ENV: dev

  flyway-migrate-prod:
    name: Flyway Migrate Prod
    runs-on: ubuntu-latest
    needs: flyway-migrate-dev
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      
      - name: Flyway migrate prod
        run: >
          docker run -v $(pwd)/sql/:/flyway/sql flyway/flyway:9.22-alpine
          -url=jdbc:postgresql://172.31.0.2:5432/a_site_to_order_stuff?sslmode=require
          -user=postgres
          -password=postgres
          -connectRetries=1
          -validateMigrationNaming=true
          migrate
        env:
          ENV: prod

I tried to connect to my DB by: it's name url=jdbc:postgresql://db:5432/a_site_to_order_stuff_local

By it's host: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name my_container_ip

The end goal is to test postgres ci-cd and also for ms sql server 2019

Upvotes: 0

Views: 109

Answers (1)

Vadym Urupa
Vadym Urupa

Reputation: 41

I added to my workflow file:

FLYWAY_URL=jdbc:postgresql://db:5432/test -e FLYWAY_USER=postgres -e FLYWAY_PASSWORD=postgres flyway migrate

Everything work file. Thanks to Teemu Risikko to point it out.

Upvotes: 0

Related Questions