명령어/DB

[PostgreSQL] pg_restore 아카이브 복원

jykim23 2026. 7. 30. 22:15
반응형

설치·접속: PostgreSQL 설치와 접속

부제: pg_dump 커스텀/디렉토리 아카이브를 골라서 복원한다

pg_restore --list shop.dump          # 아카이브 목차 확인
pg_restore -d shop_copy shop.dump    # shop_copy DB로 복원
pg_restore -d shop_copy -t orders shop.dump  # orders만 복원
pg_restore: connecting to database for restore
pg_restore: creating TABLE "public.orders"
pg_restore: creating SEQUENCE "public.orders_id_seq"
pg_restore: creating TABLE "public.products"
pg_restore: creating TABLE "public.users"
pg_restore: processing data for table "public.orders"
pg_restore: processing data for table "public.products"
pg_restore: processing data for table "public.users"
pg_restore: creating CONSTRAINT "public.users users_pkey"
pg_restore: creating INDEX "public.idx_orders_user"
pg_restore: creating FK CONSTRAINT "public.orders orders_user_id_fkey"

pg_restorepg_dump -Fc(커스텀)나 -Fd(디렉토리)로 뜬 아카이브를 되돌리는 도구다. 평문 SQL 백업에는 못 쓰고(그건 psql로 넣는다) 바이너리 아카이브 전용이다. 진짜 강점은 선택 복원인데, --list로 목차(TOC)를 먼저 뽑아 필요한 테이블·인덱스만 골라 복원할 수 있다. 위 예시는 shop_copy로 전체를 복원한 뒤 orders가 200건 그대로 들어왔는지 확인한 흐름이다. 복원 대상 DB는 미리 createdb로 만들어 두거나 -C 옵션으로 함께 생성한다.

이렇게도 쓴다

아카이브 안에 뭐가 들었는지 목차부터 본다. (복원 전 확인)

pg_restore --list shop.dump

 

DB를 새로 만들면서 통째로 복원한다. (-C 조합)

pg_restore -C -d postgres shop.dump

 

특정 테이블 하나만 골라 복원한다.

pg_restore -d shop_copy -t orders shop.dump

 

CPU 여러 개로 병렬 복원해 시간을 줄인다. (-j)

pg_restore -j 4 -d shop_copy shop.dump

 

데이터는 빼고 스키마 구조만 복원한다.

pg_restore --schema-only -d shop_copy shop.dump

 

목차를 편집해 원하는 항목만 순서대로 복원한다. (-L 조합)

pg_restore --list shop.dump > toc.txt && pg_restore -L toc.txt -d shop_copy shop.dump

 

복원 중 에러가 나면 통째로 롤백한다. (트랜잭션 단일화)

pg_restore --single-transaction -d shop_copy shop.dump
반응형