mysql_to_postgresql.md 4.8 KB
Newer Older
1 2
# Migrating GitLab from MySQL to Postgres

3
If you are replacing MySQL with Postgres while keeping GitLab on the same server all you need to do is to export from MySQL, import into Postgres and rebuild the indexes as described below. 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 已提交
4

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

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

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

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

14
git clone https://github.com/gitlabhq/mysql-postgresql-converter.git
D
dosire 已提交
15
cd mysql-postgresql-converter
16
mysqldump --compatible=postgresql --default-character-set=utf8 -r databasename.mysql -u root gitlabhq_production -p
D
dosire 已提交
17
python db_converter.py databasename.mysql databasename.psql
18 19 20

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

22 23
# Rebuild indexes (see below)

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 31
## Rebuild indexes

32
The lanyrd database converter script does not preserve all indexes, so we have to recreate them ourselves after migrating from MySQL. It is not necessary to shut down GitLab for this process.
33

34
### For non-omnibus installations
35

36
On non-omnibus installations (distributed using Git) we retrieve the index declarations from version control using `git stash`.
37

38 39 40 41 42 43
```
# Clone the database converter on your Postgres-backed GitLab server
cd /tmp
git clone https://github.com/gitlabhq/mysql-postgresql-converter.git

cd /home/git/gitlab
44 45

# Stash changes to db/schema.rb to make sure we can find the right index statements
46 47
sudo -u git -H git stash

48 49 50 51 52 53 54 55 56
# Generate add_index.rb
ruby /tmp/mysql-postgresql-converter/add_index_statements.rb db/schema.rb > /tmp/mysql-postgresql-converter/add_index.rb

# Create the indexes
sudo -u git -H bundle exec rails runner -e production 'eval $stdin.read' < /tmp/mysql-postgresql-converter/add_index.rb
```

### For omnibus-gitlab installations

57
On omnibus-gitlab we need to get the index declarations from a file called `schema.rb.bundled`. For versions older than 6.9, we need to download the file.
58 59 60 61 62 63 64

```
# Clone the database converter on your Postgres-backed GitLab server
cd /tmp
/opt/gitlab/embedded/bin/git clone https://github.com/gitlabhq/mysql-postgresql-converter.git
cd /tmp/mysql-postgresql-converter

65 66 67
# Download schema.rb.bundled if necessary
test -e /opt/gitlab/embedded/service/gitlab-rails/db/schema.rb.bundled || sudo /opt/gitlab/embedded/bin/curl -o /opt/gitlab/embedded/service/gitlab-rails/db/schema.rb.bundled https://gitlab.com/gitlab-org/gitlab-ce/raw/v6.9.1/db/schema.rb

68 69 70
# Generate add_index.rb
/opt/gitlab/embedded/bin/ruby add_index_statements.rb /opt/gitlab/embedded/service/gitlab-rails/db/schema.rb.bundled > add_index.rb

71
# Create the indexes
72
/opt/gitlab/bin/gitlab-rails runner 'eval $stdin.read' < add_index.rb
73 74
```

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

78
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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

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

# Clone the database converter
100
sudo -u git -H git clone https://github.com/gitlabhq/mysql-postgresql-converter.git
101 102 103 104 105 106 107 108 109 110 111 112

# 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

# 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.

sudo -u git -H tar rf TIMESTAMP_gitlab_backup.tar db/database.sql

113 114
# Done! TIMESTAMP_gitlab_backup.tar can now be restored into a Postgres GitLab
# installation. Remember to recreate the indexes after the import.
115
```