[PostgreSQL] 메이저 버전 업그레이드 방법(12버전->13버전)

■ PostgreSQL 업그레이드 방법

PostgreSQL에서 메이저 버전을 업그레이드 하는 방법은 크게 2가지가 있습니다.

 

• dump 프로그램 이용

기존 PostgresQL에서 모든 데이터에 대해서 dump 받은 다음 기존 엔진을 백업하고, 새로운 엔진을 설치 새로운 엔진에 기존에 백업받은 dump파일을 다시 밀어넣는 방법입니다.

엔진 설치 시간, 데이터 크기만킁 시간이 소요됩니다. 데이터를 빼내와서 다시 밀어넣는만큼 autovacuum 효과가 있습니다.

또한 데이터가 처음부터 입력되기 때문에 단편화 또한 해소되게 됩니다.

 

• inplace 업그레이드

pg_upgrade 이용하는 방법입니다. 기존 엔진에 신규 엔진을 밀어 올리는 방식입니다.

간단한 방법이기도 하면서 굉장히 어려운 방법이기도 합니다. 에러 발생시 정말 많은 케이스를 경험하게 됩니다.

클러스터 디렉토리 외부에 테이블 스페이스를 생성했을 경우 재조정이 필요할수도 있으며, 확장모듈을 수동으로 설치했을 경우 재설치가 필요할수도 있습니다.

기타 여러 에러가 발생하는만큼 내공은 쌓이겠지만 경험이 없다면 아주 많이 힘들수도 있습니다.

반드시 개발기, 스테이지 DB서버에서 테스트를 충분히 하시고 Production DB 업그레이드 하시는것을 아주 강력히 추천드립니다.

자신이 없다면 시간이 걸리더라도 dump방식이 유리합니다.

 

업그레이드 순서

1. dump restore 방법

▶︎ Databse Dump

기존 DB에서 모든 데이터 덤프

shell> pg_dumpall > full_dump_file_name.sql

 

기존 엔진 정지.

shell> systemctl stop postgresql

 

기존 엔진 이름 변경

shell> mv pgsql pgsql.old

 

▶︎ Databse Restore

신규 엔진 설치

신규 postgresql을 설치합니다.

 

신규 엔진 시작

shell> systemctl start postgres

 

덤프파일 import

shell> psql -f [덤프파일명]

 

2. pg_upgrade 이용하는 방법

▶︎ 기존 엔진 중지

shell> systemctl stop postgresql

or

shell> pg_ctl stop -D /usr/local/pgsql/data 

 

▶︎ 기존 PosgreSQL 이름 변경. 

shell> cd /usr/local

shell> mv pgsql pgsql.old

 

▶︎ 신규 Posgresql설치.

상위 버전의 신규 postgresql 설치합니다.

 

▶︎ 신규 엔진 클러스터 초기화

신규 PostgreSQL 대해서 클러스터를 초기화 합니다.

shell> initdb -D /usr/local/pgsql/data

 

▶︎ 암호가 적용되어 있는 postgresql과 적용되어 있지 않는 postgresql의 사전 처리 방법

기존 postgresql에 로그인 암호가 적용되어 있을 경우.

- 중요사항

신규 서버에도 암호가 적용되어 있어야 합니다.

 

- 암호 파일 작성

슈퍼 유저 계정 디렉토리에 .pgpass 파일을 만든 후 그안에 형식에 맞는 계정 정보를 입력합니다.

먼저 아래와 같이 파일을 생성합니다.

$ touch ~/.pgpass

$ chmod 0600 ~/.pgpass

 

- 입력 형식

server:port:database:username:password 의 순서대로 내용을 작성합니다. 예를 들면
localhost:5432:sbtest:user1:password1
10.1.100.1:5432:testdb:user2:password2
10.1.100.113:*:*:devuser:devpass
참고 URL : https://www.postgresql.org/docs/12/libpq-pgpass.html

 

암호 인증 방식을 통하지 않고 직접 업그레이드 하는 방법(추천)

- pg_hba.conf 암호 방식 처리

md5 trust 방식으로 업그레이드시까지 일시 변경.

추후 다시 trust방식으로 변경.(되는지 검증.)

 

▶︎ pg_upgrade 수행.

• pu_upgrade 옵션

먼저 pg_upgrade 옵션은 다음과 같습니다,

pg_upgrade -b oldbindir -B newbindir -d oldconfigdir -D newconfigdir [option...]

-b : 기존 바이너리 디렉토리

-B : 신규 바이너리 디렉토리

-d : 기존 환경파일 디렉토리

-D : 신규 환경파일 디렉토리

options

-U, --user : username

 

수행 방법

postgres 유저로 로그인

shell> su - postgres

신규 posgresql디렉토리로 이동

shell> cd /usr/local/pgsql

명령어 수행

shell> pg_upgrade -b /usr/local/pgsql.old/bin -B /usr/local/pgsql/bin \
-d /usr/local/pgsql.old/data -D /usr/local/pgsql/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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       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
------------------
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 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 analyze new cluster                      ok
Creating script to delete old cluster                       ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

 

 

업그레이드 작업.

전체 vacuuming 작업

업그레이드 로그중 맨아래 analyze_new_cluster.sh 실행합니다.

전체 vacuum 수행하는것으로 반드시 해주시는것을 추천드립니다.

DB 사이즈만큼 시간이 걸리는 작업이지만 그래도 필요한 작업이기에 추천드립니다.

shell> ./analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.

If you would like default statistics as quickly as possible, cancel
this script and run:
    "/usr/local/pgsql/bin/vacuumdb" --all --analyze-only

vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "sbtest": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "sbtest": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "sbtest": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

Done

 

기존 클러스터 삭제

delete_old_cluster.sh 수행하면 기존 클러스터가 삭제됩니다.

나중에 실패했을때를 대비 가장 나중에 수행하거나 수동으로 삭제하셔도 됩니다.

Designed by JB FACTORY