Keith Pitty
Keith Pitty

Reputation: 1508

Travis CI not connecting to PostgreSQL 11.2

I'm having trouble with a build of a Rails application for a PostgreSQL 11.2 database.

Here's the .travis.yml file:

rvm:
  - 2.6.1
dist: xenial
services:
  - postgresql
addons:
  postgresql: "11.2"
  apt:
    packages:
      - postgresql-11
before_script:
  - psql --version
  - psql -c 'create database kpdotcom_test;' -U postgres
  - cp config/database.yml.travis config/database.yml
  - bundle exec rake db:schema:load

However, the build fails:

enter image description here

Any suggestions would be gratefully appreciated.

Upvotes: 6

Views: 1255

Answers (2)

guillaume
guillaume

Reputation: 719

Jonathan Wheeler's answer helped me a lot. However I needed to install postgres 11 on focal distro whereas the initial post uses xenial.

Turned out I had to tweak a little bit travis' config file. I thought that might help anyone trying to install postgres 11 on focal. Basically you need to:

  • import the repository signing key (wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -)
  • add repository contents to Ubuntu 20.04 system (echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list)
  • install PostgreSQL 11 (sudo apt -y update && sudo apt -y install postgresql-11 postgresql-client-11)
  • change pg config files:
    • change port (port = 5433/port = 5432)
    • ease postgres user access (peer/trust)
  • change access rights sudo chmod 750 /var/lib/postgresql/11/main

Here is the script:

before_install:
  - sudo apt -y remove postgresql\*
  - wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  - echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list
  - sudo apt -y update
  - sudo apt -y install postgresql-11 postgresql-client-11
  - sudo sed -i 's/port = 5433/port = 5432/' /etc/postgresql/11/main/postgresql.conf
  - sudo sed -i 's/local   all             postgres                                peer/local   all             postgres                                trust/' /etc/postgresql/11/main/pg_hba.conf
  - sudo chmod 750 /var/lib/postgresql/11/main
  - sudo service postgresql restart 11

I could keep before_script as it was given by Jonathan Wheeler.

If unsure, you can check the postgres port adding a temp line in the script: echo $(sudo cat /etc/postgresql/11/main/pg_hba.conf) and search for port = 543 in the logs.

Same for postgres auth method: echo $(sudo cat /etc/postgresql/11/main/postgresql.conf)

Upvotes: 0

Jonathan Wheeler
Jonathan Wheeler

Reputation: 2699

I had this problem too. Here's the config file that fixed it for me:

This script:

  1. Shuts down all 9.* postgreSQL databases
  2. installs 11.2 (at the time of this writing)
  3. copies the authentication information from the old 9.6 configuration
  4. creates a role called "travis"
language: ruby
rvm: 2.6.2
before_install:
  - sudo apt-get update
  - sudo apt-get --yes remove postgresql\*
  - sudo apt-get install -y postgresql-11 postgresql-client-11
  - sudo cp /etc/postgresql/{9.6,11}/main/pg_hba.conf
  - sudo service postgresql restart 11  
before_script:
  - psql --version
  - psql -c 'CREATE DATABASE {{your database name here}};' -U postgres
  - psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
  - cp config/database.yml.travis config/database.yml
script: bundle exec rake spec
services:
  - postgresql
addons:
  postgresql: "11.2"

Upvotes: 5

Related Questions