DATABASE/PostgreSQL

PostgreSQL 메이저 버전 업그레이드(9.6->14)

Fehoon- 2022. 11. 14. 15:24

  • PostgreSQL 9.x 버전의 지원종료로 다른 메이저버전으로 업그레이드 필요.

- 연관 프로그램 종료

  • ex> 레드마인 등 프로그램 종료

- DB 백업

# pg_dump -Fp -d redmine -f /tmp/db.sql
# pg_dump -Fc -d redmine -f /tmp/db.dump
# pg_dump -Ft -d redmine -f /tmp/db.tar
# pg_dump -Fd -d redmine -f /tmp/dumpdir

PostgreSQL (centos에서 14버전 다운로드)

- CentOS 7 / RHEL 7 Linux 시스템에 PostgreSQL 저장소 추가.

sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

- 사용가능한 저장소 확인

PostgreSQL 14 확인 가능 시 정상적으로 된 것을 확인.

$ sudo yum repolist -y
Failed to set locale, defaulting to C
Loaded plugins: product-id, search-disabled-repos, subscription-manager
repo id                                                                              repo name                                                                                                  status
pgdg-common/7Server/x86_64                                                           PostgreSQL common RPMs for RHEL/CentOS 7Server - x86_64                                                      353
pgdg10/7Server/x86_64                                                                PostgreSQL 10 for RHEL/CentOS 7Server - x86_64                                                              1004
pgdg11/7Server/x86_64                                                                PostgreSQL 11 for RHEL/CentOS 7Server - x86_64                                                              1112
pgdg12/7Server/x86_64                                                                PostgreSQL 12 for RHEL/CentOS 7Server - x86_64                                                               697
pgdg13/7Server/x86_64                                                                PostgreSQL 13 for RHEL/CentOS 7Server - x86_64                                                               445
pgdg14/7Server/x86_64                                                                PostgreSQL 14 for RHEL/CentOS 7Server - x86_64                                                               183
pgdg96/7Server/x86_64                                                                PostgreSQL 9.6 for RHEL/CentOS 7Server - x86_64                                                              960
rhel-7-server-rpms/7Server/x86_64                                                    Red Hat Enterprise Linux 7 Server (RPMs)                                                                   32535
repolist: 37289

- PostgreSQL 14 설치

  • yum update 후 reboot
sudo yum -y update
sudo systemctl reboot
  • postgresql 14 설치 시작
sudo yum install -y postgresql14-server postgresql14
  • 설치 성공 후 패키지 버전 확인
$ rpm -qi postgresql14-server postgresql14
Name        : postgresql14-server
Version     : 14.1
Release     : 1PGDG.rhel7
Architecture: x86_64
Install Date: Thu Dec  9 05:32:06 2021
Group       : Unspecified
Size        : 23220670
License     : PostgreSQL
Signature   : DSA/SHA1, Thu Nov 11 04:21:43 2021, Key ID 1f16d2e1442df0f8
Source RPM  : postgresql14-14.1-1PGDG.rhel7.src.rpm
Build Date  : Wed Nov 10 10:47:13 2021
Build Host  : koji-centos7-x86-64-pgbuild
......

Name        : postgresql14
Version     : 14.1
Release     : 1PGDG.rhel7
Architecture: x86_64
Install Date: Thu Dec  9 05:32:04 2021
Group       : Unspecified
Size        : 8155895
License     : PostgreSQL
Signature   : DSA/SHA1, Thu Nov 11 04:21:43 2021, Key ID 1f16d2e1442df0f8
Source RPM  : postgresql14-14.1-1PGDG.rhel7.src.rpm
Build Date  : Wed Nov 10 10:47:13 2021
Build Host  : koji-centos7-x86-64-pgbuild

9.6 -> 14 이전 작업

- 이전 9.6 버전 정지

sudo systemctl stop postgresql-9.6
sudo systemctl status postgresql-9.6

- 14버전을 initDB 해줌

su - postgres

cd /usr/pgsql-14/bin

./initdb -D /var/lib/pgsql/14/data/

- 14버전이 정상적으로 실행되는 지 확인 후 종료

sudo systemctl start postgresql-14
sudo systemctl status postgresql-14
sudo systemctl stop postgresql-14

- 9.6버전 authentication config 값 수정

cd / var/lib/pgsql/9.6/data/
vim pg_hba.conf
  • method md5 부분을 trust로 다음과 같이 변경 (파일을 백업해두기를 추천)
  # TYPE  DATABASE        USER            ADDRESS                 METHOD

  # "local" is for Unix domain socket connections only
  #local   all             all                                     md5
  # IPv4 local connections:
  #host    all             all             127.0.0.1/32            md5
  # IPv6 local connections:
  #host    all             all             ::1/128                 md5
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  #host    all             all             192.168.0.0/23          md5
  #local   replication     postgres                                peer
  #host    replication     postgres        127.0.0.1/32            ident
  #host    replication     postgres        ::1/128                 ident

  # "local" is for Unix domain socket connections only
  local   all             all                                     trust
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
  host    all             all             0.0.0.0/0               trust
  # IPv6 all connections:
  host    all             all             ::/0                    trust

- 9.6 에서 14버전으로 스위칭 처리

su - postgres
cd /var/lib/pgsql/
  • pg_upgrade 옵션
pg_upgrade

  -b, --old-bindir=BINDIR       옛 클러스터 실행 파일의 디렉터리
  -B, --new-bindir=BINDIR       새 클러스터 실행 파일의 디렉터리 (기본값:
                                pg_upgrade가 있는 디렉터리)
  -c, --check                   실 작업 없이, 그냥 검사만
  -d, --old-datadir=DATADIR     옛 클러스터 데이터 디렉터리
  -D, --new-datadir=DATADIR     새 클러스터 데이터 디렉터리
  -j, --jobs=NUM                동시에 작업할 프로세스 또는 쓰레드 수
  -k, --link                    새 클러스터 구축을 복사 대신 링크 사용
  -o, --old-options=옵션        옛 서버에서 사용할 서버 옵션들
  • 전환이 정상적으로 가능한지 -c 옵션을 줘서 체크
  • 정상 처리되었을 경우 -c 옵션을 제외하고 진행한다.
/usr/pgsql-14/bin/pg_upgrade -b /usr/pgsql-9.6/bin -B /usr/pgsql-14/bin -d /var/lib/pgsql/9.6/data -D /var/lib/pgsql/14/data -c
/usr/pgsql-14/bin/pg_upgrade -b /usr/pgsql-9.6/bin -B /usr/pgsql-14/bin -d /var/lib/pgsql/9.6/data -D /var/lib/pgsql/14/data

- 9.6버전 authentication config 값 재수정(롤백)

  • 9.6버전 authentication config 값 수정 << 부분을 다시 역순으로 롤백시키거나 백업한 파일로 복원한다.

- config 파일 이전

  • /var/lib/pgsql/9.6/data/ 의 경로에서 /var/lib/pgsql/14/data/로
  • 9.6버전의 pg_hba.conf 파일과 postgresql.conf 파일을 복사(cp) 복사 전에 기존 파일(14버전)은 백업하였음.
  • 복사 후 ls -al 로 UID, GID 확인 후 postgres로 변경 시켜줘야 에러 발생하지 않음.(명령어 chown, chgrp)

- postgresql-14 시작

- sudo systemctl start postgresql-14

- 종료한 연관 프로그램 시작


자동실행 설정

- 부팅시 자동 실행

# systemctl enable service명.service

# systemctl enable postgresql-14-service 

- 부팅시 자동 실행 등록/해제

# systemctl disable service명.service

# systemctl disable postgresql-9.6-service 
[root@WNSERVER2 uadmin]# systemctl disable postgresql-9.6.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service.
[root@WNSERVER2 uadmin]# systemctl enable postgresql-14.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-14.service to /usr/lib/systemd/system/postgresql-14.service.
반응형