Product at Otovo 🏗


Today I needed to fix our Travis CI setup to use PostgreSQL to ensure we run the same version of PostgreSQL in production as well as in our test suite.

Travis provides all PostgreSQL versions that are available in the PostgreSQL official APT repository, so we had the packages we needed.

But, the documentation is a bit lacking, as the packages and setup differs slightly from the default 9.6 database Travis CI provides you with.

So here is the step-by-step guide to running PostgreSQL 10 with PostGIS 2.4 on Travis.

The very first thing is to set sudo: true if not already set in .travis.yml:

sudo: true

Then you need to set the correct addons:

addons:
  postgresql: '10'
  apt:
    packages:
      - postgresql-10-postgis-2.4
      - postgresql-10-postgis-2.4-scripts
      - postgresql-client-10

Make sure to include the postgresql-10-postgis-2.4-scripts package as it requires the PostgreSQL extension control files needed to enable the extension.

Since Travis has a bunch of PostgreSQL versions pre-installed, the new PostgreSQL will run on the secondary port 5433, so you’ll need to both pass this to your application and the psql tool which we’ll need later.

The easiest way to achieve this, is to set it as a global environment variable. If you like us have multiple environment variables configured and use them as the test matrix, you’ll need to split the env declaration in .travis.yml into a global section and a matrix section. If you had the following test matrix:

env:
  - TOX_ENV=py36
  - TOX_ENV=flake8

The new setup becomes:

env:
  global:
    - PGPORT=5433
  matrix:
    - TOX_ENV=py36
    - TOX_ENV=flake8

The default PostgreSQL 9.6 version comes pre-setup with the PostgreSQL super user postgres with no password. The PostgreSQL 10 packages does not, so you’ll need to create the user you want to use from your tests.

We do that in the before_install phase. You could also use the install phase if you like that better, as long as it is before any of your code starts to run. See the Travis CI build lifecycle documentation for more details on the different phases.

Add the following to your .travis.yml files:

before_install:
  - sudo -u postgres psql -c "CREATE USER testuser WITH PASSWORD 'password'"
  - sudo -u postgres psql -c "ALTER ROLE testuser SUPERUSER"

Now, remember to update your application config to match. We use Django, so our settings.py file was changed to include the following:

if 'TRAVIS' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.contrib.gis.db.backends.postgis',
            'NAME': 'travis',
            'USER': 'testuser',
            'PASSWORD': 'password',
            'HOST': 'localhost',
            'PORT': os.getenv('PGPORT'),
        },
}

All in all, not a terribly hard process, but it took me quite a while since there does not seem to be any up to date documentation on how all the pieces fit together.

Hopefully this blog post will save you the time I spent!