I've documented the procedure to migrate H2 to Postgres database under gerrit 2.7. Hope it'll be helpful.
Migrate H2 to Postgres Database
1. Create the postgres database and re-init the gerrit to create the
database schema by:
cd ~; java -jar gerrit-2.7.war init -d /home/gerrit2/review_site
NOTE: gerrit may change the project rights in All-Projects.git,
please back up All-Projects.git and restore it every time you do
the re-init operation.
After that, you can switch the database by preparing two sets of
gerrit.config and secure.config files so that you can connect to
database offline or start gerrit server by your choice.
You can connect to the database offline by:
cd ~/review_site; java -jar bin/gerrit.war gsql
2. List the table schema of both H2 and Postgres and compare them to find
the differences.
We need to follow the Postgres database schema to export the tables
and dump the contents for the next two steps.
3. Switch to H2 and Export all tables into CSV files from H2.
CALL CSVWRITE('account_diff_preferences', 'SELECT context,
expand_all_comments,ignore_whitespace,intraline_difference,line_length,
manual_review,retain_header,show_line_endings, show_tabs,show_whitespace_errors,
skip_deleted,skip_uncommented,syntax_highlighting,tab_size,id
FROM account_diff_preferences');
CALL CSVWRITE('account_external_ids', 'SELECT account_id,
email_address,password,external_id
FROM account_external_ids');
CALL CSVWRITE('account_group_includes_by_uuid', 'SELECT group_id,include_uuid
FROM account_group_includes_by_uuid');
CALL CSVWRITE('account_group_includes_by_uuid_audit', 'SELECT added_by, removed_by,
removed_on,added_on,group_id,include_uuid
FROM account_group_includes_by_uuid_audit');
CALL CSVWRITE('account_group_members', 'SELECT account_id,group_id
FROM account_group_members');
CALL CSVWRITE('account_group_members_audit', 'SELECT added_by,removed_by,
removed_on,account_id,added_on,group_id
FROM account_group_members_audit');
CALL CSVWRITE('account_group_names', 'SELECT group_id,name
FROM account_group_names');
CALL CSVWRITE('account_groups', 'SELECT description,group_type,
group_uuid,name,owner_group_uuid,visible_to_all,group_id
FROM account_groups');
CALL CSVWRITE('account_patch_reviews', 'SELECT account_id,file_name,
change_id,patch_set_id
FROM account_patch_reviews');
CALL CSVWRITE('account_project_watches', 'SELECT notify_abandoned_changes,
notify_all_comments,notify_new_changes,notify_new_patch_sets,notify_submitted_changes,
account_id,filter,project_name
FROM account_project_watches');
CALL CSVWRITE('account_ssh_keys', 'SELECT ssh_public_key,valid,
account_id,seq
FROM account_ssh_keys');
CALL CSVWRITE('accounts', 'SELECT contact_filed_on,
full_name,comment_visibility_strategy,copy_self_on_email,date_format,download_command,
download_url,maximum_page_size,relative_date_in_change_table,reverse_patch_set_order,
show_site_header,show_username_in_review_category,time_format,use_flash_clipboard,
inactive,preferred_email,registered_on,account_id
FROM accounts');
CALL CSVWRITE('change_messages', 'SELECT author_id,message,
patchset_change_id,patchset_patch_set_id,written_on,change_id,uuid
FROM change_messages');
CALL CSVWRITE('changes', 'SELECT change_key,created_on,
current_patch_set_id,dest_branch_name,dest_project_name,last_sha1_merge_tested,
last_updated_on,mergeable,open,owner_account_id,sort_key,status,subject,topic,
row_version,change_id
FROM changes');
CALL CSVWRITE('patch_comments', 'SELECT author_id,line_nbr,
message,parent_uuid,side,status,written_on,file_name,change_id,patch_set_id,uuid
FROM patch_comments');
CALL CSVWRITE('patch_set_ancestors', 'SELECT ancestor_revision,
change_id,patch_set_id,position
FROM patch_set_ancestors');
CALL CSVWRITE('patch_set_approvals', 'SELECT change_open,
change_sort_key,granted,value,account_id,category_id,change_id,patch_set_id
FROM patch_set_approvals');
CALL CSVWRITE('patch_sets', 'SELECT created_on,draft,
revision,uploader_account_id,change_id,patch_set_id
FROM patch_sets');
CALL CSVWRITE('schema_version', 'SELECT version_nbr,singleton
FROM schema_version');
CALL CSVWRITE('starred_changes', 'SELECT account_id,change_id
FROM starred_changes');
CALL CSVWRITE('submodule_subscriptions', 'SELECT submodule_branch_name,
submodule_project_name,submodule_path,super_project_branch_name,
super_project_project_name
FROM submodule_subscriptions');
CALL CSVWRITE('system_config', 'SELECT admin_group_id,
admin_group_uuid,anonymous_group_id,batch_users_group_id,batch_users_group_uuid,
owner_group_id,register_email_private_key,registered_group_id,site_path,
wild_project_name,singleton
FROM system_config');
CALL CSVWRITE('tracking_ids', 'SELECT change_id,
tracking_key,tracking_system
FROM tracking_ids');
4. Start gerrit and Dump the contents of each table for check later into
directory db_h2. Please make each command one line in real script.
Note that the ssh port of gerrit is changed to 29419 temporarily to
make sure no one will operate on it at the same time.
ignore_whitespace,intraline_difference,line_length,manual_review,retain_header,
show_line_endings,show_tabs,show_whitespace_errors,skip_deleted,skip_uncommented,
syntax_highlighting,tab_size,id
FROM account_diff_preferences\" >account_diff_preferences
password,external_id
FROM account_external_ids\" >account_external_ids
FROM account_group_includes_by_uuid\" >account_group_includes_by_uuid
removed_on,added_on,group_id,include_uuid
FROM account_group_includes_by_uuid_audit\" >account_group_includes_by_uuid_audit
FROM account_group_members\" >account_group_members
removed_on,account_id,added_on,group_id
FROM account_group_members_audit\" >account_group_members_audit
FROM account_group_names\" >account_group_names
group_uuid,name,owner_group_uuid,visible_to_all,group_id
FROM account_groups\" >account_groups
change_id,patch_set_id
FROM account_patch_reviews\" >account_patch_reviews
notify_all_comments,notify_new_changes,notify_new_patch_sets,
notify_submitted_changes,account_id,filter,project_name
FROM account_project_watches\" >account_project_watches
account_id,seq
FROM account_ssh_keys\" >account_ssh_keys
comment_visibility_strategy,copy_self_on_email,date_format,download_command,
download_url,maximum_page_size,relative_date_in_change_table,
reverse_patch_set_order,show_site_header,show_username_in_review_category,
time_format,use_flash_clipboard,inactive,preferred_email,registered_on,account_id
FROM accounts\" >accounts
patchset_change_id,patchset_patch_set_id,written_on,change_id,uuid
FROM change_messages\" >change_messages
current_patch_set_id,dest_branch_name,dest_project_name,last_sha1_merge_tested,
last_updated_on,mergeable,open,owner_account_id,sort_key,status,subject,topic,
row_version,change_id
FROM changes\" >changes
message,parent_uuid,side,status,written_on,file_name,change_id,patch_set_id,uuid
FROM patch_comments\" >patch_comments
change_id,patch_set_id,position
FROM patch_set_ancestors\" >patch_set_ancestors
change_sort_key,granted,value,account_id,category_id,change_id,patch_set_id
FROM patch_set_approvals\" >patch_set_approvals
revision,uploader_account_id,change_id,patch_set_id
FROM patch_sets\" >patch_sets
FROM schema_version\" >schema_version
FROM starred_changes\" >starred_changes
submodule_project_name,submodule_path,super_project_branch_name,
super_project_project_name
FROM submodule_subscriptions\" >submodule_subscriptions
admin_group_uuid,anonymous_group_id,batch_users_group_id,batch_users_group_uuid,
owner_group_id,register_email_private_key,registered_group_id,site_path,
wild_project_name,singleton
FROM system_config\" >system_config
tracking_system
FROM tracking_ids\" >tracking_ids
5. Stop gerrit and connect to H2 database to get sequence number.
SELECT currval('change_message_id');
SELECT currval('change_id');
SELECT currval('account_id');
SELECT currval('account_group_id');
You can double check with:
SELECT nextval('change_message_id');
SELECT nextval('change_id');
SELECT nextval('account_id');
SELECT nextval('account_group_id');
It should be 'nextval = currval + 1' for each sequence.
6. Stop gerrit, Switch to Postgres and Delete all the contents in Postgres.
DELETE FROM account_diff_preferences;
DELETE FROM account_external_ids;
DELETE FROM account_group_includes_by_uuid;
DELETE FROM account_group_includes_by_uuid_audit;
DELETE FROM account_group_members;
DELETE FROM account_group_members_audit;
DELETE FROM account_group_names;
DELETE FROM account_groups;
DELETE FROM account_patch_reviews;
DELETE FROM account_project_watches;
DELETE FROM account_ssh_keys;
DELETE FROM accounts;
DELETE FROM change_messages;
DELETE FROM changes;
DELETE FROM patch_comments;
DELETE FROM patch_set_ancestors;
DELETE FROM patch_set_approvals;
DELETE FROM patch_sets;
DELETE FROM schema_version;
DELETE FROM starred_changes;
DELETE FROM submodule_subscriptions;
DELETE FROM system_config;
DELETE FROM tracking_ids;
7. Change the user gerrit2 to superuser temporarily.
ALTER USER gerrit2 WITH SUPERUSER;
8. Stay in the Postgres and continue to Import the CSV files to
Postgres tables.
COPY account_diff_preferences FROM '/home/gerrit2/review_site/account_diff_preferences' DELIMITER ',' CSV HEADER;
COPY account_external_ids FROM '/home/gerrit2/review_site/account_external_ids' DELIMITER ',' CSV HEADER;
COPY account_group_includes_by_uuid FROM '/home/gerrit2/review_site/account_group_includes_by_uuid' DELIMITER ',' CSV HEADER;
COPY account_group_includes_by_uuid_audit FROM '/home/gerrit2/review_site/account_group_includes_by_uuid_audit' DELIMITER ',' CSV HEADER;
COPY account_group_members FROM '/home/gerrit2/review_site/account_group_members' DELIMITER ',' CSV HEADER;
COPY account_group_members_audit FROM '/home/gerrit2/review_site/account_group_members_audit' DELIMITER ',' CSV HEADER;
COPY account_group_names FROM '/home/gerrit2/review_site/account_group_names' DELIMITER ',' CSV HEADER;
COPY account_groups FROM '/home/gerrit2/review_site/account_groups' DELIMITER ',' CSV HEADER;
COPY account_patch_reviews FROM '/home/gerrit2/review_site/account_patch_reviews' DELIMITER ',' CSV HEADER;
COPY account_project_watches FROM '/home/gerrit2/review_site/account_project_watches' DELIMITER ',' CSV HEADER;
COPY account_ssh_keys FROM '/home/gerrit2/review_site/account_ssh_keys' DELIMITER ',' CSV HEADER;
COPY accounts FROM '/home/gerrit2/review_site/accounts' DELIMITER ',' CSV HEADER;
COPY change_messages FROM '/home/gerrit2/review_site/change_messages' DELIMITER ',' CSV HEADER;
COPY changes FROM '/home/gerrit2/review_site/changes' DELIMITER ',' CSV HEADER;
COPY patch_comments FROM '/home/gerrit2/review_site/patch_comments' DELIMITER ',' CSV HEADER;
COPY patch_set_ancestors FROM '/home/gerrit2/review_site/patch_set_ancestors' DELIMITER ',' CSV HEADER;
COPY patch_set_approvals FROM '/home/gerrit2/review_site/patch_set_approvals' DELIMITER ',' CSV HEADER;
COPY patch_sets FROM '/home/gerrit2/review_site/patch_sets' DELIMITER ',' CSV HEADER;
COPY schema_version FROM '/home/gerrit2/review_site/schema_version' DELIMITER ',' CSV HEADER;
COPY starred_changes FROM '/home/gerrit2/review_site/starred_changes' DELIMITER ',' CSV HEADER;
COPY submodule_subscriptions FROM '/home/gerrit2/review_site/submodule_subscriptions' DELIMITER ',' CSV HEADER;
COPY system_config FROM '/home/gerrit2/review_site/system_config' DELIMITER ',' CSV HEADER;
COPY tracking_ids FROM '/home/gerrit2/review_site/tracking_ids' DELIMITER ',' CSV HEADER;
9. Set the sequence number for postgres database.
SELECT setval('change_message_id', change_message_id_num);
SELECT setval('change_id', change_id_num);
SELECT setval('account_id', account_id_num);
SELECT setval('account_group_id', account_group_id_num);
10.Change the user gerrit2 to non superuser.
ALTER USER gerrit2 WITH NOSUPERUSER;
11.Start gerrit and execute commands in step 4 in directory db_postgres.
Compare the table contents in db_h2 and db_postgres to make sure the
migration is complete.