Marcelo Toledo
Marcelo Toledo

Reputation: 138

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

Upvotes: 6

Views: 3170

Answers (3)

dijonkitchen
dijonkitchen

Reputation: 145

It seems over the years, Travis CI switched over to something that wasn't able to infer that PostgreSQL was a dependency from the Gemfile in Ruby or for a Rails project.

Others have noted the missing services in the Travis configuration. Here is the missing piece explicitly:

services:
  - postgresql

I was wondering why my old projects started failing recently as I updated dependencies, but even old one that worked would fail if run again in master. This was tricky!

Reference: https://docs.travis-ci.com/user/database-setup/#postgresql

Upvotes: 0

Marcelo Toledo
Marcelo Toledo

Reputation: 138

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

Upvotes: 3

Amirol Ahmad
Amirol Ahmad

Reputation: 542

you need to add services: postgresql to start the service. check here:

language: ruby
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rspec
before_script:
  - psql -c 'create database db_test;' -U postgres
rvm:
  - 2.5.0
services:
  - postgresql
  - memcached
  - redis-server
addons:
  postgresql: "9.4"
before_install:
  - gem update --system

Upvotes: 5

Related Questions