mysql_to_postgresql.md 3.3 KB
Newer Older
1
# Migrating GitLab from MySQL to Postgres
2
*Make sure you view this [guide from the `master` branch](../../../master/doc/update/mysql_to_postgresql.md) for the most up to date instructions.*
3

4
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.
D
dosire 已提交
5

6 7 8 9
## Export from MySQL and import into Postgres

Use this if you are keeping GitLab on the same server.

D
dosire 已提交
10
```
11 12 13 14
sudo service gitlab stop

# Update /home/git/gitlab/config/database.yml

15
git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
D
dosire 已提交
16
cd mysql-postgresql-converter
17 18
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
19
ed -s gitlabhq_production.psql < move_drop_indexes.ed
20 21

# Import the database dump as the application database user
22
sudo -u git psql -f gitlabhq_production.psql -d gitlabhq_production
23

24 25 26
# Install gems for PostgreSQL (note: the line below states '--without ... mysql')
sudo -u git -H bundle install --without development test mysql --deployment

27
sudo service gitlab start
D
dosire 已提交
28
```
29 30

## Converting a GitLab backup file from MySQL to Postgres
31
**Note:** Please make sure to have Python 2.7.x (or higher) installed.
32

33
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

```
# 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
52
sudo -u git -H mysqldump --compatible=postgresql --default-character-set=utf8 -r gitlabhq_production.mysql -u root gitlabhq_production -p
53 54

# Clone the database converter
55
sudo -u git -H git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
56 57 58 59

# 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
60
sudo -u git -H ed -s db/database.sql < move_drop_indexes.ed
61

K
Kamil Trzcinski 已提交
62 63 64
# Compress database backup
sudo -u git -H gzip db/database.sql

65 66 67 68 69
# 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.

K
Kamil Trzcinski 已提交
70
sudo -u git -H tar rf TIMESTAMP_gitlab_backup.tar db/database.sql.gz
71

72 73
# Done! TIMESTAMP_gitlab_backup.tar can now be restored into a Postgres GitLab
# installation. Remember to recreate the indexes after the import.
74
```