Setting up Databases
This guide covers setting up the most popular databases and other services in the Travis CI environment.
All services use default settings, with the exception of some added users and relaxed security settings.
Travis CI environments do not start services by default, to make more RAM available
to build scripts. Start services by adding them to the
services: section of your
If you install a service in the
addons:section, such as MariaDB, you do not need to add it to the
services:section as well.
To start several services:
services: - riak - rabbitmq - memcached
Note that this feature only works for services we provision in our CI environment. If you download Apache Jackrabbit you still have to start it in a
Start MySQL in your
services: - mysql
MySQL binds to 127.0.0.1 and requires authentication. You can connect using the username “travis” or “root” and a blank password.
Note that the “travis” user does not have full MySQL privileges that the “root” user does.
Using MySQL with ActiveRecord
config/database.yml example for Ruby projects using ActiveRecord:
test: adapter: mysql2 database: myapp_test username: travis encoding: utf8
You might have to create the
myapp_test database first. Run this as part of your build script:
# .travis.yml before_script: - mysql -e 'create database myapp_test;'
In older versions of MySQL, Ubuntu package provided the
test database by default.
This is no longer the case as of version 5.5.37 due to security concerns
(See change log).
If you need it, create it using the following
before_install: - mysql -e "create database IF NOT EXISTS test;" -uroot
The recommended way to get MySQL 5.6 is switching to our Trusty CI Environment and manually installing the required packages by adding the following lines to the
dist: trusty sudo: required addons: apt: packages: - mysql-server-5.6 - mysql-client-core-5.6 - mysql-client-5.6
Note that you’ll need to use the user
travis is not available yet.
For example, if you were running:
mysql -e 'create database your_db_name;'
You should run instead:
mysql -u root -e 'create database your_db_name;'
Start PostgreSQL in your
services: - postgresql
Using PostgreSQL in your Builds
The default user for accessing the local PostgreSQL server is
postgres with a blank password.
Create a database for your application by adding a line to your .travis.yml:
before_script: - psql -c 'create database travis_ci_test;' -U postgres
For a Rails application, you can now use the following
database.yml configuration to access the database locally:
test: adapter: postgresql database: travis_ci_test
If your local test setup uses different credentials or settings to access the local test database, we recommend putting these settings in a
database.yml.travis in your repository and copying that over as part of your build:
before_script: - cp config/database.yml.travis config/database.yml
Using a different PostgreSQL Version
The Travis CI build environments use version 9.1 by default, but other versions from the official PostgreSQL APT repository are also available. To use a version other than the default, specify only the major.minor version in your
addons: postgresql: "9.4"
The following versions are available on Linux builds:
|PostgreSQL||sudo enabled precise||sudo enabled trusty||container precise|
On OSX, the following versions are installed:
All installed versions of PostgreSQL include PostGIS 2.1 .
You need to activate the extension in your
before_script: - psql -U postgres -c "create extension postgis"
PostgreSQL and Locales
The following locales are installed on Travis CI build environements:
You can find what language packs are currently available for Ubuntu 12.04 on the packages site.
The following example shows the lines you need to add to your
.travis.yml to install the Spanish language pack. The
sudo command is not available on container based infrastructure so you currently cannot install locales on it.
Note that you need to remove the PostgreSQL version from the
addonssection of your .travis.yml:
before_install: - sudo apt-get update - sudo apt-get install language-pack-es - sudo /etc/init.d/postgresql stop - sudo /etc/init.d/postgresql start 9.3
MariaDB is a community-developed fork of MySQL. It is available as an addon on Travis CI.
To use MariaDB, specify the “major.minor” version you want to use in your
.travis.yml. Versions are listed on the MariaDB web page.
addons: mariadb: '10.0'
The version number is exported as the
TRAVIS_MARIADB_VERSION environment variable.
The easiest and simplest relational database.
SQLite3 in Ruby Projects
Add the sqlite3 ruby bindings to your bundle:
# Gemfile # for CRuby, Rubinius, including Windows and RubyInstaller gem "sqlite3", :platform => [:ruby, :mswin, :mingw] # for JRuby gem "jdbc-sqlite3", :platform => :jruby
If you use ActiveRecord, add the following to your
test: adapter: sqlite3 database: ":memory:" timeout: 500
Or if you’re not using a
config/database.yml, connect to the database manually:
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
Start MongoDB in your
services: - mongodb
MongoDB binds to 127.0.0.1 and requires no authentication or database creation up front. If you add an
admin user authentication is enabled, since
mongod is started with the
Note: Admin users are users created in the admin database.
To create users for your database, add a
before_script section to your
before_script: - mongo mydb_test --eval 'db.addUser("travis", "test");'
MongoDB does not immediately accept connections
A few users have reported that MongoDB does not accept connections when from the build script.
The issue is intermittent, and the only reliable way to avoid it is to inject an artificial wait before making the first connection:
Add the following
before_script to your
.travis.yml to wait before connecting to MongoDB:
before_script: - sleep 15 - mongo mydb_test --eval 'db.addUser("travis", "test");'
Start CouchDB in your
services: - couchdb
CouchDB binds to 127.0.0.1, uses default configuration and does not require authentication (in CouchDB terms it runs in admin party).
Before using CouchDB you need to create the database as part of your build process:
before_script: - curl -X PUT localhost:5984/myapp_test
setuid flags, so you can only run RabbitMQ on standard, OSX or Trusty infrastructure (ie, your
.travis.yml must contain
Start RabbitMQ in your
services: - rabbitmq
RabbitMQ uses the default configuration:
You can set up more vhosts and roles in the
before_script section of your
Start Riak in your
services: - riak
Riak uses the default configuration apart from the storage backend, which is LevelDB.
Riak Search is enabled.
Start Memcached service in your
services: - memcached
Memcached uses the default configuration and binds to localhost.
Start Redis in your
services: - redis-server
Redis uses the default configuration and is available on localhost.
Start Cassandra in your
services: - cassandra
Cassandra is provided by Datastax Community Edition and uses the default configuration. It is available on 127.0.0.1.
Installing older versions of Cassandra
Use the following example to install a specific older version of Cassandra in your
before_install: - sudo rm -rf /var/lib/cassandra/* - wget http://www.us.apache.org/dist/cassandra/1.2.18/apache-cassandra-1.2.18-bin.tar.gz && tar -xvzf apache-cassandra-1.2.18-bin.tar.gz && sudo sh apache-cassandra-1.2.18/bin/cassandra
If you’re using Container-based infrastructure you won’t be able to install other versions of Cassandra as the
sudocommand is not available.
Start Neo4J in your
services: - neo4j
Neo4J Server uses default configuration and binds to localhost on port 7474.
Neo4j does not start on container-based infrastructure. See https://github.com/travis-ci/travis-ci/issues/3243
Start ElasticSearch in your
services: - elasticsearch
ElasticSearch takes few seconds to start, to make sure it is available when the build script runs add a small delay to your build script:
before_script: - sleep 10
ElasticSearch uses the default configuration and is available on 127.0.0.1.
Installing specific versions of ElasticSearch
You can overwrite the installed ElasticSearch with the version you need (e.g., 1.2.4) with the following:
before_install: - curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.4.deb && sudo dpkg -i --force-confnew elasticsearch-1.2.4.deb && sudo service elasticsearch restart
sudois not available on Container-based infrastructure.
Truncated Output in the Build Log
When ElasticSearch starts, you may see a truncated error message such as:
$ sudo service elasticsearch start * Starting ElasticSearch Server ission denied on key 'vm.max_map_count'
To use RethinkDB with Travis CI, list it as an addon in the
.travis.yml configuration file, specifying the version number as a string.
addons: rethinkdb: '2.3.4'
If you specify a partial version number, the addon will install and run the latest version that matches. For example,
'2.3' will match the latest RethinkDB version in the
Two environment variables are exported:
TRAVIS_RETHINKDB_VERSIONis the version specified in the configuration (e.g.,
TRAVIS_RETHINKDB_PACKAGE_VERSIONis the full version of the package that was installed (e.g.,
When enabled, RethinkDB will start on
localhost at the default port (
Multiple Database Builds
If you need to run multiple builds using different databases, you can configure environment variables
before_install line to create a build matrix.
Using environemnt variables and a before_script step
DB environment variable to specify the name of the database configuration. Locally you would run:
DB=postgres [commands to run your tests]
On Travis CI you want to create a build matrix of three builds each having the
DB variable exported with a different value, and for that you can use the
env option in
env: - DB=sqlite - DB=mysql - DB=postgres
Then you can use those values in a
before_script) step to set up each database. For example:
before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS doctrine_tests;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS doctrine_tests_tmp;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database doctrine_tests;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database doctrine_tests_tmp;' -U postgres; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS doctrine_tests_tmp;create database IF NOT EXISTS doctrine_tests;'; fi"
Travis CI does not have any special support for these variables, it just creates three builds with different exported values. It is up to your build script and
before_scriptsteps to make use of them.
For a real example, see doctrine/doctrine2 .travis.yml.
Another approach is put all database configuration in one YAML file (
test/database.yml for example), like ActiveRecord does:
sqlite: adapter: sqlite3 database: ":memory:" timeout: 500 mysql: adapter: mysql2 database: myapp_test username: encoding: utf8 postgres: adapter: postgresql database: myapp_test username: postgres
Then, in your test suite, read that data into a configurations hash:
configs = YAML.load_file('test/database.yml') ActiveRecord::Base.configurations = configs db_name = ENV['DB'] || 'sqlite' ActiveRecord::Base.establish_connection(db_name) ActiveRecord::Base.default_timezone = :utc