Can't connect to postgres service

Here is services in the github workflow:

services:
postgres:
image: postgres:9
redis:
image: redis

Container with postgres was started:

Status: Downloaded newer image for postgres:9
docker.io/library/postgres:9
/usr/bin/docker create --name 6389e0a15cc645dfbc2b440cb43917d2_postgres9_9b3b8e --label 488dfb --network github_network_8bc25efd4e3c4ef581794739550806f5 --network-alias postgres -e GITHUB_ACTIONS=true postgres:9
be1f8b93768561bee42b4841054415dee6de30a902a5f5f1919ebdce938c5eeb
/usr/bin/docker start be1f8b93768561bee42b4841054415dee6de30a902a5f5f1919ebdce938c5eeb
be1f8b93768561bee42b4841054415dee6de30a902a5f5f1919ebdce938c5eeb
/usr/bin/docker ps --all --filter id=be1f8b93768561bee42b4841054415dee6de30a902a5f5f1919ebdce938c5eeb --filter status=running --no-trunc --format "{{.ID}} {{.Status}}"
be1f8b93768561bee42b4841054415dee6de30a902a5f5f1919ebdce938c5eeb Up Less than a second

But next step which runs a command to interact with DB failed:

could not translate host name "postgres" to address: Temporary failure in name resolution
Couldn't create database. Please check your configuration.

Same things was when I mapped postgres port (5432) to localhost and used it as a postgres hostname.

Environment: ubuntu-18.04

Any advice?

Have you tried following the official instructions?

I believe the health check part of the command is required, otherwise the next step might start before the database is up and accepting connections.

services:
  postgres:
    image: postgres:10.8
    env:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    ports:
    - 5432:5432
    # needed because the postgres container does not provide a healthcheck
    options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
1 Like

@dzivalli ,

How are things going?

As @dannyben had mentioned, I have tried and tested the example shared in the official instructions, it can work fine.

Did you also tried this? Please try it, and if have any progress, feel free to tell us.

I have the same problem.

This is my workflow.yml:

jobs:
  testing:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x, 16.x]

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: wgl
          POSTGRES_PASSWORD: wgl
          POSTGRES_DB: wf
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v2

      - name: use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install

      - name: Prepare test db
        run: |
          psql -h postgres -U wgl -c "SELECT current_role;" wf
          npx db-migrate -e github up

I get:

> Initialize containers 27s
>> Checking docker version
>> Clean up resources from previous jobs
>> Create local container network
>> Starting postgres service container
>> Waiting for all services to be ready
...
  starting
  postgres service is starting, waiting 9 seconds before checking again.
  /usr/bin/docker inspect --format="{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}" 418b8b3b83f55f1f5c6c976d23d2392c70bddd0b592ad89ec75b1b527313a397
  healthy
  postgres service is healthy.
> Run actions/checkout@v2
> Run actions/setup-node@v2
> Run npm install
> Run psql -h postgres -U wgl -c "SELECT current_role;" wf;
psql: error: could not translate host name "postgres" to address: Temporary failure in name resolution
Error: Process completed with exit code 2.

the answer is here: Can't connect to Postgres service - #5 by mcosta74

since my test step is not running in a docker container it should use localhost to connect to postgres instead of the container name postgres