MusicBrainz Database Setup

This repository contains a collection of scripts that can help you setup a local copy of the MusicBrainz database and keep it up to date. There is no script that does everything for you though. The main motivation for writing these scripts was to customize the database, so the way to install the database might differ from user to user.


  1. Make sure you have Python and psycopg2 installed. On Debian and Ubuntu, that means installing these packages:

    sudo apt install python python-psycopg2
  2. Create mbslave.conf by copying and editing mbslave.conf.default. You will need to get the API token on the MetaBrainz website.

  3. Setup the database. If you are starting completely from scratch, you can use the following commands to setup a clean database:

    sudo su - postgres
    createuser musicbrainz
    createdb -l C -E UTF-8 -T template0 -O musicbrainz musicbrainz
    createlang plpgsql musicbrainz
    psql musicbrainz -c 'CREATE EXTENSION cube;'
    psql musicbrainz -c 'CREATE EXTENSION earthdistance;'
  4. Prepare empty schemas for the MusicBrainz database and create the table structure:

    echo 'CREATE SCHEMA musicbrainz;' | ./ -S
    echo 'CREATE SCHEMA statistics;' | ./ -S
    echo 'CREATE SCHEMA cover_art_archive;' | ./ -S
    echo 'CREATE SCHEMA wikidocs;' | ./ -S
    echo 'CREATE SCHEMA documentation;' | ./ -S
    ./ <sql/CreateTables.sql | ./
    ./ <sql/statistics/CreateTables.sql | ./
    ./ <sql/caa/CreateTables.sql | ./
    ./ <sql/wikidocs/CreateTables.sql | ./
    ./ <sql/documentation/CreateTables.sql | ./
  5. Download the MusicBrainz database dump files from

  6. Import the data dumps, for example:

    ./ mbdump.tar.bz2 mbdump-derived.tar.bz2
  7. Setup primary keys, indexes and views:

    ./ <sql/CreatePrimaryKeys.sql | ./
    ./ <sql/statistics/CreatePrimaryKeys.sql | ./
    ./ <sql/caa/CreatePrimaryKeys.sql | ./
    ./ <sql/wikidocs/CreatePrimaryKeys.sql | ./
    ./ <sql/documentation/CreatePrimaryKeys.sql | ./
    ./ <sql/CreateIndexes.sql | grep -v musicbrainz_collate | ./
    ./ <sql/CreateSlaveIndexes.sql | ./
    ./ <sql/statistics/CreateIndexes.sql | ./
    ./ <sql/caa/CreateIndexes.sql | ./
    ./ <sql/CreateViews.sql | ./
  8. Vacuum the newly created database (optional)

    echo 'VACUUM ANALYZE;' | ./

Tips and Tricks

Single Database Schema

MusicBrainz used a number of schemas by default. If you are embedding the MusicBrainz database into an existing database for your application, it's convenient to merge them all into a single schema. That can be done by changing your config like this:


After this, you only need to create the musicbrainz schema and import all the tables there.

No PostgreSQL Extensions

You can avoid installing the cube and earthdistance extensions if you map the CUBE column to TEXT and remove some indexes.

You might do a similar thing with JSONB columns, if you don't want to upgrade to PostgreSQL 9.5 yet.

Replace the commands above with something like his:

./ <sql/CreateTables.sql | \
    perl -pe 's{\b(CUBE|JSONB)\b}{TEXT}' | \

./ <sql/CreateIndexes.sql | \
    grep -v musicbrainz_collate | \
    grep -v ll_to_earth | \
    grep -v medium_index | \
    perl -pe 's{\bUSING BRIN\b}{}' | \
    perl -pe 'BEGIN { undef $/; } s{^CREATE INDEX edit_data_idx_link_type .*?;}{}smg' | \

Full Import Upgrade

You can use the schema mapping feature to do zero-downtime upgrade of the database with full data import. You can temporarily map all schemas to e.g. musicbrainz_NEW, import your new database there and then rename it.

echo 'BEGIN; ALTER SCHEMA musicbrainz RENAME TO musicbrainz_OLD; ALTER SCHEMA musicbrainz_NEW RENAME TO musicbrainz; COMMIT;' | ./ -S


After the initial database setup, you might want to update the database with the latest data. The script will fetch updates from MusicBrainz and apply it to your local database:


In order to update your database regularly, add a cron job like this that runs every hour:

15 * * * * $HOME/mbslave/ >>/var/log/mbslave.log


When the MusicBrainz database schema changes, the replication will stop working. This is usually announced on the MusicBrainz blog. When it happens, you need to upgrade the database.

Release 2017-05-25 (24)

Run the upgrade scripts:

./ <sql/updates/schema-change/24.slave.sql | ./
echo 'UPDATE replication_control SET current_schema_sequence = 24;' | ./