Search code examples
rubypostgresqltravis-ci

psql: could not connect to server error on "travis-ci"


My build is broken on travis-ci:

$ psql -c 'create database travis_ci_test;' -U postgres
psql: 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"?

The command "psql -c 'create database travis_ci_test;' -U postgres" failed and exited with 2 during.

I'm usin this .travis.yml:

Yaml Version updated on 05/31/2020:

dist: trusty

env:
    global:
      - PGPORT=5433

services:
    - postgresql

addons:
    postgresql: '10'
    apt:
        packages:
        - postgresql-10
        - postgresql-client-10

before_script:
    - export RUBYOPT='-W0' # to remove ruby 2.7 warnings
    - cp config/database.yml.travis config/database.yml

language: ruby

rvm:
    - 2.7.0

script:
    - bundle exec rails db:reset db:setup db:migrate
    - bundle exec rspec
    - bundle exec rubocop --config .rubocop.yml

before_install:
    - gem update --system
    - gem install bundler

Solution

  • EDITED AFTER SOLUTION:

    I was able to find out what was wrong. See below the definitive solution:

    First: Travis wasn't compactive with postgres-10 (in the past)

    Second: I needed remove the line that create a new database.

    services:
        - postgresql
    
    addons:
        postgresql: '9.6'
    
    before_script:
        - cp config/database.yml.travis config/database.yml
    
    language: ruby
    
    rvm:
        - 2.5.0
    
    script:
        - bundle exec rails db:reset db:setup db:migrate RAILS_ENV=test
        - bundle exec rspec
        - bundle exec rubocop --config .rubocop.yml