About
home
Tmax OpenSQL
home

PostgreSQL Version Upgrade

작성자
CJH
PostgresSQL Version 은 Major version과 Minor version으로 구성됩니다.
예를 들어, 11.14 version 은 Major version이 11 , Minor version 14 로 구성됩니다.
PostgreSQL 은 동일한 Major version을 가진 Minor version과 호환됩니다.
예를 들어, 10.1 version 은 10.0 및 10.6 버전과 호환됩니다.
호환되는 버전 간에 업데이트 하려면 서버가 다운된 상태에서 실행 파일을 교체하고 서버를 다시 시작하면 됩니다.
데이터 디렉토리는 변경되지 않은 상태로 유지됩니다.
메이저 버전의 경우 내부 데이터 저장 형식이 변경될 수 있으므로 업그레이드가 복잡합니다.

Major Version Upgrade ( 동일한 서버에서 수행 )

PostgreSQL 9.6 → PostgreSQL 14.2 Version Upgrade 절차
Binary Directory Path / Data Cluster Path 는 개개인의 설정이나 환경에 따라 다를 수 있습니다.
따라서, PostgreSQL RPM Default 설치 경로를 예를 들어 설명하겠습니다.
본인의 설정 경로가 다르다면 바꿔서 적용해주시면 되겠습니다.
1.
기존 PostgreSQL Version 및 PSQL Version 확인
Upgrade 전의 PostgreSQL version 이 9.6 입니다.
$ psql -c "select version();" version ----------------------------------------------------------------------------------------------------------- PostgreSQL 9.6.24 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row) $ psql -V psql (PostgreSQL) 9.6.24
Shell
복사
2.
기존 PostgreSQL 테스트 용 Database 생성 및 데이터 생성
3.
기존 PostgreSQL Stop
현재 PostgreSQL 을 정상적으로 종료시킵니다.
$ /usr/pgsql-9.6/bin/pg_ctl stop waiting for server to shut down.... done server stopped
Shell
복사
4.
새로운 PostgreSQL 버전 설치 (14.2 Version) ( root 계정 )
# yum -y install postgresql14-server postgresql14-contrib # cd /usr # ls | grep pgsql pgsql-14 pgsql-9.6
Shell
복사
5.
새로운 PostgreSQL 데이터 클러스터 생성
$ /usr/pgsql-14/bin/pg_ctl -D /var/lib/pgsql/14/data
Shell
복사
6.
pg_upgrade 유틸리티 Major Version Upgrade 수행
$ export PATH=/usr/pgsql-14/bin:$PATH $ export PGDATA=/var/lib/pgsql/14/data $ cd /var/lib/pgsql $ 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 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 Checking for user-defined encoding conversions ok Checking for user-defined postfix operators ok Checking for tables WITH OIDS ok Checking for invalid "sql_identifier" user columns ok Checking for invalid "unknown" user columns 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_clog 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 hash indexes ok Checking for extension updates ok Upgrade Complete ---------------- Optimizer statistics are not transferred by pg_upgrade. Once you start the new server, consider running: /usr/pgsql-14/bin/vacuumdb --all --analyze-in-stages Running this script will delete the old cluster's data files: ./delete_old_cluster.sh
Shell
복사
7.
새로운 PostgreSQL 로 서버 기동
$ pg_ctl start waiting for server to start....2022-04-21 23:18:31.175 KST [8834] LOG: redirecting log output to logging collector process 2022-04-21 23:18:31.175 KST [8834] HINT: Future log output will appear in directory "log". done server started
Shell
복사
8.
Upgrade PostgreSQL 서버에서 데이터 확인
-- 다른 오브젝트들도 조회 해보시면 좋을 듯 합니다. $ psql -U postgres -d dvdrental -c "\dt" List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | actor | table | postgres public | address | table | postgres public | category | table | postgres public | city | table | postgres public | country | table | postgres public | customer | table | postgres public | film | table | postgres public | film_actor | table | postgres public | film_category | table | postgres public | inventory | table | postgres public | language | table | postgres public | payment | table | postgres public | rental | table | postgres public | staff | table | postgres public | store | table | postgres (15 rows)
Shell
복사

Minor Version Upgrade ( 동일한 서버에서 수행 )

PostgreSQL 11.14 → PostgreSQL 11.15 Version Upgrade 절차
Binary Directory Path / Data Cluster Path 는 개개인의 설정이나 환경에 따라 다를 수 있습니다.
따라서, PostgreSQL RPM Default 설치 경로를 예를 들어 설명하겠습니다.
본인의 설정 경로가 다르다면 바꿔서 적용해주시면 되겠습니다.
1.
기존 PostgreSQL Version 및 PSQL Version 확인
$ psql -c "select version();" version ---------------------------------------------------------------------------------------------------------- PostgreSQL 11.14 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row) $ psql -V psql (PostgreSQL) 11.14
Shell
복사
2.
기존 PostgreSQL Stop
$ /usr/pgsql-11/bin/pg_ctl stop waiting for server to shut down.... done server stopped
Shell
복사
3.
기존 PostgreSQL 바이너리 파일 백업 ( root 계정 )
$ su - root # mv /usr/pgsql-11 /usr/pgsql-11_bak
Shell
복사
4.
새로운 PostgreSQL 설치 (11.15 Version) ( root 계정 )3 yum -y install postgresql11-server postgresql11-contrib $ cd /usr $ ls | grep pgsql pgsql-11
5.
새로운 PostgreSQL 로 서버 기동 ( postgres 계정 )
$ /usr/pgsql-11/bin/pg_ctl start waiting for server to start....2022-04-21 17:26:33.517 KST [13388] LOG: listening on IPv6 address "::1", port 5432 2022-04-21 17:26:33.517 KST [13388] LOG: listening on IPv4 address "127.0.0.1", port 5432 2022-04-21 17:26:33.520 KST [13388] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2022-04-21 17:26:33.526 KST [13388] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2022-04-21 17:26:33.535 KST [13388] LOG: redirecting log output to logging collector process 2022-04-21 17:26:33.535 KST [13388] HINT: Future log output will appear in directory "log". done server started
Shell
복사

Version Upgrade ( 다른 두 개의 서버에서 수행 )

두 개의 다른 서버에서는 위에서 사용한 pg_upgrade 유틸리티 사용이 불가능합니다.
따라서 호환되는 버전에 한하여 pg_dump / pg_dumpall 사용이 필요합니다.
Upgrade 수행 시나리오는 아래와 같습니다.
Server #1 / Server #2 로 각각의 서버가 존재합니다.
Server #1 에는 PostgreSQL v13.9 이 설치되어 있습니다.
Server #2 에는 PostgreSQL v14.6 이 설치되어 있습니다.
Server #1 의 PostgreSQL 에 Sample Database 생성 후 Server #2 로 이관하겠습니다.
PostgreSQL 13.9 ( Server #1 ) → PostgreSQL 14.6 ( Server #2 ) Version Upgrade 절차
1.
Server #1 - PostgreSQL Version 및 PSQL Version 확인
$ psql -c "select version();" version ---------------------------------------------------------------------------------------------------- ----- PostgreSQL 13.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64 -bit (1 row) $ psql -V psql (PostgreSQL) 13.9
Shell
복사
2.
Server #1 - PostgreSQL Sample Database 생성 및 데이터 생성
아래 문서를 통해 Server #1 의 PostgreSQL 에 Sample Database 를 생성합니다.
3.
Server #2 - PostgreSQL Version 및 PSQL Version 확인
$ psql -c "select version();" version ---------------------------------------------------------------------------------------------------- ----- PostgreSQL 14.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64 -bit (1 row) $ psql -V psql (PostgreSQL) 14.6
Shell
복사
4.
Server #2 - pg_hba.conf 파일에 내용 추가
-- 테스트를 위해 모든 IP 접근을 허용하도록 하겠습니다. host all all 0.0.0.0/0 trust -- pg_hba.conf 파일 적용을 위해 리로드 커맨드를 수행합니다. $ pg_ctl reload server signaled
Shell
복사
5.
Server #1 - pg_dumpall 수행
-- $ pg_dumpall -c | psql -h [Server #2 IP] -p 5432 $ pg_dumpall -c | psql -h 192.168.81.142 -p 5432 SET SET SET ERROR: database "dvdrental" does not exist ERROR: current user cannot be dropped ERROR: role "postgres" already exists ALTER ROLE SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET UPDATE 1 DROP DATABASE CREATE DATABASE ALTER DATABASE You are now connected to database "template1" as user "postgres". SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET COMMENT ALTER DATABASE You are now connected to database "template1" as user "postgres". SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET REVOKE GRANT SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET CREATE DATABASE ALTER DATABASE You are now connected to database "dvdrental" as user "postgres". SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET CREATE TYPE ALTER TYPE CREATE DOMAIN ALTER DOMAIN CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE FUNCTION ALTER FUNCTION CREATE SEQUENCE ALTER TABLE SET SET CREATE TABLE ALTER TABLE CREATE FUNCTION ALTER FUNCTION CREATE AGGREGATE ALTER AGGREGATE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE TABLE ALTER TABLE CREATE VIEW ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE VIEW ALTER TABLE CREATE VIEW ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE VIEW ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE VIEW ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE CREATE TABLE ALTER TABLE CREATE VIEW ALTER TABLE CREATE VIEW ALTER TABLE COPY 200 COPY 603 COPY 16 COPY 600 COPY 109 COPY 599 COPY 1000 COPY 5462 COPY 1000 COPY 4581 COPY 6 COPY 14596 COPY 16044 COPY 2 COPY 2 setval -------- 200 (1 row) setval -------- 605 (1 row) setval -------- 16 (1 row) setval -------- 600 (1 row) setval -------- 109 (1 row) setval -------- 599 (1 row) setval -------- 1000 (1 row) setval -------- 4581 (1 row) setval -------- 6 (1 row) setval -------- 32098 (1 row) setval -------- 16049 (1 row) setval -------- 2 (1 row) setval -------- 2 (1 row) ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER CREATE TRIGGER ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET DROP DATABASE CREATE DATABASE ALTER DATABASE You are now connected to database "postgres" as user "postgres". SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET COMMENT
Shell
복사
지금까지 PostgreSQL Version Upgrade에 관해 알아보았습니다
‘PostgreSQL Sample Database’ 도 바로 이어서 확인해보세요!

자유롭게 댓글을 남겨주세요