Help us learn about your current experience with the documentation. Take the survey.

将 GitLab 数据库迁移到另一个 PostgreSQL 实例

  • 版本:免费版、高级版、旗舰版
  • 产品:GitLab 私有化部署

有时,您需要将数据库从一个 PostgreSQL 实例迁移到另一个。例如,如果您正在使用 AWS Aurora 并准备启用数据库负载均衡,就需要将数据库迁移到 RDS for PostgreSQL。

要将数据库从一个实例迁移到另一个:

  1. 收集源和目标 PostgreSQL 的端点信息:

    SRC_PGHOST=<source postgresql host>
    SRC_PGUSER=<source postgresql user>
    
    DST_PGHOST=<destination postgresql host>
    DST_PGUSER=<destination postgresql user>
  2. 停止 GitLab:

    sudo gitlab-ctl stop
  3. 从源转储数据库:

    /opt/gitlab/embedded/bin/pg_dump -h $SRC_PGHOST -U $SRC_PGUSER -c -C -f gitlabhq_production.sql gitlabhq_production
    /opt/gitlab/embedded/bin/pg_dump -h $SRC_PGHOST -U $SRC_PGUSER -c -C -f praefect_production.sql praefect_production

    在极少数情况下,您可能会在执行 pg_dump 和恢复操作后注意到数据库性能问题。这可能是因为 pg_dump 不包含优化器用于制定查询规划决策的统计信息。如果恢复后性能下降,可以通过找到有问题的查询,然后对该查询使用的表运行 ANALYZE 来解决此问题。

  4. 将数据库恢复到目标(这将覆盖所有同名的现有数据库):

    /opt/gitlab/embedded/bin/psql -h $DST_PGHOST -U $DST_PGUSER -f praefect_production.sql postgres
    /opt/gitlab/embedded/bin/psql -h $DST_PGHOST -U $DST_PGUSER -f gitlabhq_production.sql postgres
  5. 可选。如果您从不使用 PgBouncer 的数据库迁移到使用 PgBouncer 的数据库,则必须手动将 pg_shadow_lookup 函数添加到应用数据库(通常是 gitlabhq_production)。

  6. 在您的 /etc/gitlab/gitlab.rb 文件中,为 GitLab 应用服务器配置目标 PostgreSQL 实例的适当连接详细信息:

    gitlab_rails['db_host'] = '<destination postgresql host>'

    有关 GitLab 多节点设置的更多信息,请参考参考架构

  7. 重新配置以使更改生效:

    sudo gitlab-ctl reconfigure
  8. 重启 GitLab:

    sudo gitlab-ctl start