Postgres error - could not connect to server: No such file or directory

Hello - hoping someone can point me in the right direction.  Trying to get an action working for Rails with Postgres support.  Got the authorization bit sorted out (finally!) but now am at this error:

could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Couldn't create 'api_test' database. Please check your configuration.
rake aborted!
ActiveRecord::NoDatabaseError: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

This looks vaguely familiar to me.  I had an issue like this with the Docker image on my local machine and I ended up have to specify a volume in my docker_compose.yml file.  I think normally it has to do with an issue with the database file on postgres not being setup right.  Feel like I am following the instructions … so wonder what I am missing?

Here is my ruby.yml configuration setup:

name: Ruby

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

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

    steps:
    - uses: actions/checkout@v2

    - uses: actions/cache@v1
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-gems-

    - name: Set up Ruby 2.7.1
      uses: ruby/setup-ruby@v1.30.1
      with:
        ruby-version: 2.7.1

    - name: Install PostgreSQL Client
      run: |
        sudo apt-get -yqq install libpq-dev

    - name: Run bundle install
      env:
        RAILS_ENV: test
      run: |
        bundle config path vendor/bundle
        bundle install --jobs 4 --retry 3

    - name: Build and test with rspec
      env:
        RAILS_ENV: test
        POSTGRES_HOST: localhost
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
        RUBYOPT: "-W:no-deprecated -W:no-experimental"
      run: |
        bundle exec rake db:setup
        bundle exec rspec

Hi @thornomad ,

In the last step, you can access the service container using localhost and the mapped port directly.

POSTGRES_PORT: 5432

Please refer to the doc here for more details.

If it doesn’t work, please kindly share your repo/sample repo for further investigation.

Thanks.

1 Like

Hello @weide-zhou - thanks for taking time to help me.  I have another repo I had used for a different test (basic rails app that uses postgres with no real configuration).  I added the github action to that app in its own branch to demonstrate the failure I am having.  You can see the action setting at:

https://github.com/thornomad/issue-38279-rails-6/blob/add-github-action/.github/workflows/ruby.yml

I am taking a guess at the config/database.yml setup.  I added the password, username, and port evn variables there.

https://github.com/thornomad/issue-38279-rails-6/blob/add-github-action/config/database.yml#L58-L63

Finally, here is the output from the action.  Am having the same issue as with the other application.

https://github.com/thornomad/issue-38279-rails-6/runs/564539190?check_suite_focus=true

Thanks again.  I feel like all my searching online and trying different examples floating around and I am getting stuck.  Must be something fundamental that I am missing!

Hi @thornomad ,

Thanks for your reply! I forked your repo and did some changes, now the error is gone:

  1. In config/database.yml, change to use below code, use ‘adapter’ & ‘host’ parameter:

    test:
    <<: *default
    database: test_app_6_test
    adapter: postgresql
    host: localhost
    encoding: unicode
    username: <%= ENV[“POSTGRES_USER”] %>
    password: <%= ENV[“POSTGRES_PASSWORD”] %>

  2. The workflow file:

    name: Ruby

    on: [push]

    jobs:
    build:
    runs-on: ubuntu-latest
    services:
    postgres:
    image: postgres:11
    ports:
    - 5432:5432
    env:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

     steps:
       - uses: actions/checkout@v1
    
       - uses: actions/setup-ruby@v1
         with:
           ruby-version: 2.6.x
    
       - name: Install dependent libraries
         run: sudo apt-get install libpq-dev
    
       - name: Bundle install
         run: |
           gem install bundler
           bundle install --jobs 4 --retry 3
       - name: setup databases
         env:
           POSTGRES_PASSWORD: postgres
           POSTGRES_USER: postgres
           RAILS_ENV: test
         run: bundle exec rake db:setup
    
  3. In the Gemfile, i changed ruby version from ‘2.6.3’ to ‘2.6.5’, since verion 2.6.3 cannot be found by action/setup-ruby.

My workflow run here: https://github.com/weide-zhou/issue-38279-rails-6/runs/566774300?check_suite_focus=true

You can refer to the link for more details. Hope it helps!

Hi - thanks for working with me on this.  It looks like the issue in the end was not defining host: localhost in the database.yml file.

For anyone else who stubmles across this; here is what ended up working for me on a new Rails 6 setup using postgres.  This let’s both the local machine testing environment and github actions do its thing! Thanks again!

env:
  POSTGRES_PASSWORD: postgres
  POSTGRES_USER: postgres
  POSTGRES_HOST: localhost
  RAILS_ENV: test

name: Ruby

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:latest
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v2

      - name: Set up Ruby version (using project .ruby-version)
        uses: eregon/use-ruby-action@master

      - name: Install dependent libraries
        run: sudo apt-get install libpq-dev

      - name: Bundle install
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3

      - name: Setup the postgres database
        run: bundle exec rake db:setup

      - name: Run RSpec tests
        run: bundle exec rspec

 And then the relevant section from database.yml:

test:
  <<: *default
  database: my_app_test
  host: <%= ENV["POSTGRES_HOST"] %>
  username: <%= ENV["POSTGRES_USER"] %>
  password: <%= ENV["POSTGRES_PASSWORD"] %>

Thanks again!

2 Likes