2014-05-08 07:18:12 -04:00
# Migrating GitLab from MySQL to Postgres
2015-08-05 01:32:09 -04:00
*Make sure you view this [guide from the `master` branch ](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/update/mysql_to_postgresql.md#migrating-gitlab-from-mysql-to-postgres ) for the most up to date instructions.*
2014-05-08 07:18:12 -04:00
2015-05-12 07:36:20 -04:00
If you are replacing MySQL with Postgres while keeping GitLab on the same server all you need to do is to export from MySQL, convert the resulting SQL file, and import it into Postgres. If you are also moving GitLab to another server, or if you are switching to omnibus-gitlab, you may want to use a GitLab backup file. The second part of this documents explains the procedure to do this.
2014-03-17 09:55:56 -04:00
2014-05-08 06:10:04 -04:00
## Export from MySQL and import into Postgres
Use this if you are keeping GitLab on the same server.
2014-03-17 09:55:56 -04:00
```
2014-05-08 07:18:22 -04:00
sudo service gitlab stop
# Update /home/git/gitlab/config/database.yml
2015-02-09 02:51:37 -05:00
git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
2014-03-17 09:55:56 -04:00
cd mysql-postgresql-converter
2015-05-12 07:36:20 -04:00
mysqldump --compatible=postgresql --default-character-set=utf8 -r gitlabhq_production.mysql -u root gitlabhq_production -p
python db_converter.py gitlabhq_production.mysql gitlabhq_production.psql
2015-07-13 12:14:26 -04:00
ed -s gitlabhq_production.psql < move_drop_indexes.ed
2014-05-19 12:28:45 -04:00
# Import the database dump as the application database user
2015-05-12 07:36:20 -04:00
sudo -u git psql -f gitlabhq_production.psql -d gitlabhq_production
2014-05-19 11:51:58 -04:00
2014-09-09 17:55:45 -04:00
# Install gems for PostgreSQL (note: the line below states '--without ... mysql')
sudo -u git -H bundle install --without development test mysql --deployment
2014-05-08 07:18:22 -04:00
sudo service gitlab start
2014-03-17 09:55:56 -04:00
```
2014-05-08 06:10:04 -04:00
## Converting a GitLab backup file from MySQL to Postgres
2014-08-21 06:28:45 -04:00
**Note:** Please make sure to have Python 2.7.x (or higher) installed.
2014-05-08 06:10:04 -04:00
2015-02-09 02:27:51 -05:00
GitLab backup files (`< timestamp > _gitlab_backup.tar`) contain a SQL dump. Using the lanyrd database converter we can replace a MySQL database dump inside the tar file with a Postgres database dump. This can be useful if you are moving to another server.
2014-05-08 06:10:04 -04:00
```
# Stop GitLab
sudo service gitlab stop
# Create the backup
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production
# Note the filename of the backup that was created. We will call it
# TIMESTAMP_gitlab_backup.tar below.
# Move the backup file we will convert to its own directory
sudo -u git -H mkdir -p tmp/backups/postgresql
sudo -u git -H mv tmp/backups/TIMESTAMP_gitlab_backup.tar tmp/backups/postgresql/
# Create a separate database dump with PostgreSQL compatibility
cd tmp/backups/postgresql
2014-11-20 11:15:58 -05:00
sudo -u git -H mysqldump --compatible=postgresql --default-character-set=utf8 -r gitlabhq_production.mysql -u root gitlabhq_production -p
2014-05-08 06:10:04 -04:00
# Clone the database converter
2015-02-09 02:51:37 -05:00
sudo -u git -H git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
2014-05-08 06:10:04 -04:00
# Convert gitlabhq_production.mysql
sudo -u git -H mkdir db
sudo -u git -H python mysql-postgresql-converter/db_converter.py gitlabhq_production.mysql db/database.sql
2015-07-14 05:50:01 -04:00
sudo -u git -H ed -s db/database.sql < mysql-postgresql-converter / move_drop_indexes . ed
2014-05-08 06:10:04 -04:00
2015-06-23 09:45:24 -04:00
# Compress database backup
2015-10-08 12:06:51 -04:00
# Warning: If you have Gitlab 7.12.0 or older skip this step and import the database.sql directly into the backup with:
# sudo -u git -H tar rf TIMESTAMP_gitlab_backup.tar db/database.sql
# The compressed databasedump is not supported at 7.12.0 and older.
2015-06-23 09:45:24 -04:00
sudo -u git -H gzip db/database.sql
2014-05-08 06:10:04 -04:00
# Replace the MySQL dump in TIMESTAMP_gitlab_backup.tar.
# Warning: if you forget to replace TIMESTAMP below, tar will create a new file
# 'TIMESTAMP_gitlab_backup.tar' without giving an error.
2015-06-23 09:45:24 -04:00
sudo -u git -H tar rf TIMESTAMP_gitlab_backup.tar db/database.sql.gz
2014-05-08 06:10:04 -04:00
2014-05-19 11:51:58 -04:00
# Done! TIMESTAMP_gitlab_backup.tar can now be restored into a Postgres GitLab
2015-08-05 01:32:09 -04:00
# installation.
2015-10-08 12:06:51 -04:00
# See https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/raketasks/backup_restore.md for more information about backups.
2014-05-08 06:10:04 -04:00
```