跳过正文

docker中使用pg_upgrade升级

·389 字·2 分钟

docker部署使用postgres比较方便,但是在进行大版本升级的时候会比较繁琐,可以 通过docker-postgres-upgrade项目所提供的镜像 可以方便的使用pg_upgrade在docker中升级

脚本示例
#

ansible来进行自动执行脚本,以postgres 16升级到postgres 17为例

---
- name: Update postgresql
  become: true
  become_method: sudo
  debugger: never
  hosts: all
  vars:
    pg_version: 17.5
    pg_major_version: "{{ pg_version | string | split('.') | first }}"
    pg_previous_version: '{{ (pg_major_version | int) - 1 }}'
  tasks:
    - name: Stop container
      community.docker.docker_compose_v2:
        project_src: /srv/postgres
        state: stopped

    - name: Upgrade
      community.docker.docker_container:
        name: postgres-upgrade
        image: 'tianon/postgres-upgrade:{{ pg_previous_version }}-to-{{ pg_major_version }}'
        state: started
        env:
          POSTGRES_PASSWORD: '{{ POSTGRES_PASSWORD }}'
        volumes:
          - '/srv/postgres/{{ pg_previous_version }}/data:/var/lib/postgresql/{{ pg_previous_version }}/data'
          - '/srv/postgres/{{ pg_major_version }}/data:/var/lib/postgresql/{{ pg_major_version }}/data'

日志示例
#

Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database user is the install user                    ok
Checking database connection settings                         ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Creating dump of global objects                               ok
Creating dump of database schemas                             ok
Checking for presence of required libraries                   ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for new cluster tablespace directories               ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Setting locale and encoding for new cluster                   ok
Analyzing all rows in the new cluster                         ok
Freezing all rows in the new cluster                          ok
Deleting files from new pg_xact                               ok
Copying old pg_xact to new server                             ok
Setting oldest XID for new cluster                            ok
Setting next transaction ID and epoch for new cluster         ok
Deleting files from new pg_multixact/offsets                  ok
Copying old pg_multixact/offsets to new server                ok
Deleting files from new pg_multixact/members                  ok
Copying old pg_multixact/members to new server                ok
Setting next multixact ID and offset for new cluster          ok
Resetting WAL archives                                        ok
Setting frozenxid and minmxid counters in new cluster         ok
Restoring global objects in the new cluster                   ok
Restoring database schemas in the new cluster                 ok
Copying user relation files                                   ok
Setting next OID for new cluster                              ok
Sync data directory to disk                                   ok
Creating script to delete old cluster                         ok
Checking for extension updates                                notice

Your installation contains extensions that should be updated
with the ALTER EXTENSION command.  The file
    update_extensions.sql
when executed by psql by the database superuser will update
these extensions.

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/lib/postgresql/17/bin/vacuumdb --all --analyze-in-stages
Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh