At Oktavilla we are in the process of moving a RubyOnRails application from MySQL to PostgreSQL. For this appliction we use the excellent CircleCI to automatically run the test suit when new commits are pushed to GitHub. This application is in swedish so we want to setup the database with collation set to sv_SE.UTF-8 to make sorting work correctly.

This didn’t work out of the box on CircleCI. After some testing and reading we ended up with this.

A custom database.yml for CircleCI. Create a file in config/ with:

  strict: false
  database: circle_ruby_test
  adapter: postgresql
  username: ubuntu
  host: localhost
  encoding: utf8
  collation: sv_SE.UTF-8
  template: template0

Note that collation and template is set. Read more on PostgreSQL database templates in the docs.

Our circle.yml looks like this:

    SPEC_OPTS: '--format doc'
    PGDATA: /var/lib/postgresql/9.4/main
    - sudo locale-gen sv_SE.UTF-8 
    - sudo -E -u postgres /usr/lib/postgresql/9.4/bin/pg_ctl restart; sleep 1 
    - mv config/ config/database.yml
    - bundle exec rake db:create db:schema:load --trace

First we have to create the system locale using locale-gen. This requires a restart of PostgreSQL and some sleep time to let it finish the startup sequence. Then we override the default database setup and use our own and create the database.