skip to Main Content

I’m new with Github Actions and Docker. Now I’m trying to set up my first CI and getting this error:

`failed to connect to `host=/var/run/postgresql user= database=`: dial error (dial unix /var/run/postgresql/.s.PGSQL.5432: connect: no such file or directory)`

But locally all works correctly.

My ci.yaml file:

name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: password
          POSTGRES_DB: sso_dev
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v2

      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: '1.21'

      - name: Install golang-migrate
        run: |
          curl -L https://github.com/golang-migrate/migrate/releases/download/v4.12.2/migrate.linux-amd64.tar.gz | tar xvz
          sudo mv migrate.linux-amd64 /usr/bin/migrate
          which migrate

      - name: Install dependencies
        run: go mod download

      - name: Create /config directory
        run: mkdir -p ./config

      - name: Create .env file
        run: echo "${{ secrets.ENV_CONFIG }}" > ./config/.env

      - name: Display .env file
        run: cat ./config/.env

      - name: Wait for PostgreSQL to be ready
        run: sleep 10

      - name: Check PostgreSQL connection
        run: pg_isready -h localhost -p 5432

      - name: Run tests
        run: make test-all-app

      - name: Notify results
        run: echo "Tests completed!"

In the ENV_CONFIG I have the following env variables:

GRPC_SERVER_PORT=44044
GRPC_SERVER_TIMEOUT=30s

DB_HOST=localhost
DB_PORT=5432
DB_NAME=sso_dev
DB_USER=root
DB_PASSWORD=password
DB_SSL_MODE=disable
DB_CONN_URL="postgres://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?sslmode=$DB_SSL_MODE"
DB_CONN_POOL_SIZE=10
DB_READ_TIMEOUT=5s
DB_WRITE_TIMEOUT=5s
DB_IDLE_TIMEOUT=60s
DB_DIAL_TIMEOUT=10s

I don’t understand where is the root cause. I asked about it ChatGPT and got the answer that application is attempting to connect to PostgreSQL via a Unix socket, but it can’t find the socket file. But if I’m not mistaken I use tcp connection…

Maybe the reason is the way how I create connection to database in my code (I use Golang)?

package postgres

import (
    "context"
    "fmt"
    "github.com/jackc/pgx/v5/pgxpool"
    _ "github.com/jackc/pgx/v5/stdlib" // Import for side effects
    "github.com/rshelekhov/sso/internal/config"
    "github.com/rshelekhov/sso/internal/storage/postgres/sqlc"
    "net"
)

// NewStorage creates a new Postgres storage
func NewStorage(cfg *config.ServerSettings) (*pgxpool.Pool, error) {
    const method = "storage.postgres.NewStorage"

    poolCfg, err := pgxpool.ParseConfig(cfg.Postgres.ConnURL)
    if err != nil {
        return nil, fmt.Errorf("%s: failed to parse config: %w", method, err)
    }

    poolCfg.MaxConnLifetime = cfg.Postgres.IdleTimeout
    poolCfg.MaxConns = int32(cfg.Postgres.ConnPoolSize)

    dialer := &net.Dialer{KeepAlive: cfg.Postgres.DialTimeout}
    dialer.Timeout = cfg.Postgres.DialTimeout
    poolCfg.ConnConfig.DialFunc = dialer.DialContext

    pool, err := pgxpool.NewWithConfig(context.Background(), poolCfg)
    if err != nil {
        return nil, fmt.Errorf("%s: failed to create pgx connection pool: %w", method, err)
    }

    return pool, nil
}

UPD.

So, I found a way to make this workflow work. I changed ENV_CONFIG secrets a bit, I removed this:

DB_HOST=localhost
DB_PORT=5432
DB_NAME=sso_dev
DB_USER=root
DB_PASSWORD=password
DB_SSL_MODE=disable

And updated this url from:

DB_CONN_URL="postgres://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?sslmode=$DB_SSL_MODE"

To:

DB_CONN_URL="postgres://root:password@localhost:5432/sso_dev?sslmode=disable"

And it works. For now I’m trying to figure out why compound env variables don’t work

2

Answers


  1. Did you ensure your sleep time is more than 10 seconds?
    if not better to add it to the yaml file. But also there might be issues with your host configurations, for example, if you’re relying on LOCALHOST for connecting to database, ensure that the service is exposed correctly and app has an access to it from the CI environment..

    I added service logs and pg_isready to your yaml file, it will look like this

    name: CI
    
    on:
      push:
        branches:
          - main
      pull_request:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        services:
          postgres:
            image: postgres:latest
            env:
              POSTGRES_USER: root
              POSTGRES_PASSWORD: password
              POSTGRES_DB: sso_dev
            ports:
              - 5432:5432
            options: >-
              --health-cmd pg_isready
              --health-interval 10s
              --health-timeout 5s
              --health-retries 5
    
        steps:
          - uses: actions/checkout@v2
    
          - name: Setup Go
            uses: actions/setup-go@v2
            with:
              go-version: '1.21'
    
          - name: Install golang-migrate
            run: |
              curl -L https://github.com/golang-migrate/migrate/releases/download/v4.12.2/migrate.linux-amd64.tar.gz | tar xvz
              sudo mv migrate.linux-amd64 /usr/bin/migrate
              which migrate
    
          - name: Install dependencies
            run: go mod download
    
          - name: Create /config directory
            run: mkdir -p ./config
    
          - name: Create .env file
            run: echo "${{ secrets.ENV_CONFIG }}" > ./config/.env
    
          - name: Display .env file
            run: cat ./config/.env
    
          - name: Wait for PostgreSQL to be ready
            run: |
              for i in {1..20}; do
                pg_isready -h localhost -p 5432 && break
                sleep 3
              done
    
          - name: Check PostgreSQL logs
            run: docker logs ${{ job.services.postgres.id }}
    
          - name: Run tests
            run: make test-all-app
    
          - name: Notify results
            run: echo "Tests completed!"
    
    
    Login or Signup to reply.
  2. Please try this:

     - name: Check PostgreSQL connection
       run: pg_isready -h postgres -p 5432 -U root
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search