[PostgreSQL] Ubuntu Linux PostgreSQL 사용법 - #4pg_dump, pg_dumpall, pg_restore 명령어 및 옵션

    이전글

    2023.07.27 - [본업/Database] - [PostgreSQL] Ubuntu Linux PostgreSQL 사용법 - #3-2 DBeaver 연결


    PostgreSQL의 Backup & Restore에는 세 가지 명령어가 주로 사용된다.

    • pg_dumpall : 데이터베이스 클러스터 전제를 백업
    • pg_dump : 특정 데이터베이스를 백업
    • pg_restore : pg_dump 및 pg_dumpall의 백업 파일을 사용해 데이터베이스 또는 클러스터 전제를 복원

    각 명령어의 사용은 사실상 다양한 옵션이 붙어 실행되는데, 그 옵션의 수가 매우 많고 다양하여 처음 접했을 때 혼란스러웠다.

    이를 처음 공부할때 최대한 빨리 받아들이기 위해 pgAdmin의 GUI를 활용해 백업, 복원할 때를 가정하여 GUI 화면 상의 옵션과 실제 명령어의 옵션을 각각 연관지어 설명한다.

    참고로 한 번 보고, 이해하고 나면 DBeaver같은 응용 프로그램으로도 충분히 백업과 복구가 가능했다.

     

    pg_dumpall

    • Filename: 출력 파일 이름(stdout : -)
      • -f, --file
    • Format: 백업 파일 포맷
      • -F, --format
      • custom, directory, tar
      • ex) -Fc or -F c
      • ex) -Fd or --format=d
      • ex)-Ft or --format=t
    • Role name: 복원 전에 SET ROLE 수행
      • --role

    • Only data: 스키마가 아닌, 데이터만 덤프
      • -a, --data-only
    • Only schema: 데이터가 아닌, 스키마만 덤프
      • -s, --schema-only
    • Owner: (Plaintext 포맷에서)원시 데이터베이스와 일치하도록 object 소유권을 설정하는 명령 건너뛰기
      • -O, --no-owner
    • Privilege: 접근 권한(privileges)(grant/revoke) 덤프 안 함
      • -x, --no-privileges
    • Tablespace: Tablespace 할당 덤프 안 함
      • --no-tablespaces
    • Unlogged table data: 기록되지 않은(unlogged) 테이블 및 시퀀스 컨텐츠 덤프 안함
      • --no-unlogged-table-data
    • Comments: 주석 덤프 안함
      • --no-comments

    • Use Column Inserts: 컬럼 이름과 함께 INSERT 명령을 사용해 데이터 덤프
      • --column-inserts
    • User Insert Commands: COPY 대신 INSERT 명령을 사용해 데이터 덤프
      • --inserts
    • Include DROP DATABASE statement: 다시 만들기(recreating) 전에 데이터베이스 object 삭제(DROP)
      • -c, --clean
    • Trigger: data-only 복원 시, 트리거 사용 안함
      • --disable-triggers
    • $ quoting: SQL 표준 따옴표를 사용하고 $ 인용구 사용 안함
      • --disable-dollar-quoting
    • With OID(s): OID 포함하여 덤프
      • -o, --oids
      • 버전 및 옵션 지원 여부 확인 필요
    • Verbose messages: Verbose 모드 사용(세부 작업 내용 출력)
      • -v, --verbose
    • Force double quote on identifiers: 키워드가 아니더라도 모든 식별자에 강제로 인용구(") 적용
      • --quote-all-identifiers
    • User SET SESSION AUTHORIZATION: ALTER OWNER 대신 SET SESSION AUTHORIZATION 명령을 사용해 소유권 설정
      • --use-set-session-authorization

    pg_dump

    • Filename: 출력 파일 혹은 디렉토리 이름
      • -f, --file
      • ex) -f /home/$USER/dbdump.sql
      • ex) --file /home/$USER/mydump.tar
    • Format: 출력 파일 포맷
      • custom, directory, tar, plaintext(default)
      • -F, --format
      • ex) -Fc or -F c
      • ex) -Fd or --format=d
      • ex) -Ft or --format=t
    • Compression ratio: 압축 포맷의 압축 수준
      • -Z, --compress
      • ex) -Z 4
      • ex) -compress=4
    • Encoding: 명시한 인코딩으로 데이터를 덤프
      • -E, --encoding
      • ex) -E UTF8
      • ex) --encoding=ISO_8859_5
    • Number of jobs: 명시한 개수 만큼 job을 병렬로 사용해 덤프
      • -j, --jobs
      • ex) -j 5
    • Role name: 덤프 전에 SET ROLE 수행
      • --role

    • Sections: 명시된 섹션만 덤프.
      • pre-data, data, post-data
      • 디폴트는 모든 섹션을 덤프
      • ex) --section=pre-data
      • ex) --section=data
      • ex) --section=post-data
    • Only Data: 스키마가 아닌 데이터만 덤프
      • -a, --data-only
    • Only schema: 데이터가 아닌 스키마만 덤프
      • -s, --schema-only
    • Blobs: Large object들을 포함하여 덤프
      • -b, --blobs
    • No blobs: Large object들을 포함하지 않고 덤프
      • -B, --no-blobs
    • Owner: (Plaintext 포맷에서)원시 데이터베이스와 일치하도록 object 소유권을 설정하는 명령 건너뛰기
      • -O, --no-owner
    • Privilege: 접근 권한(privileges)(grant/revoke) 덤프 안 함
      • -x, --no-privileges
    • Tablespace: Tablespace 할당 덤프 안 함
      • --no-tablespaces
    • Unlogged table data: 기록되지 않은(unlogged) 테이블 및 시퀀스 컨텐츠 덤프 안함
      • --no-unlogged-table-data
    • Comments: 주석 덤프 안함
      • --no-comments

    • Use Column Inserts: 컬럼 이름과 함께 INSERT 명령을 사용해 데이터 덤프
      • --column-inserts
    • User Insert Commands: COPY 대신 INSERT 명령을 사용해 데이터 덤프
      • --inserts
    • Include CREATE DATABASE statement: CREATE DATABASE 명령을 포함해 덤프
      • -C, --create
    • Include DROP DATABASE statement: 다시 만들기(recreating) 전에 데이터베이스 object 삭제(DROP)
      • -c, --clean
    • Load Via Partition Root: 파티션 계층 구조의 root를 로드해 테이블 파티션을 덤프
      • --load-via-partition-root
    • Trigger: data-only 복원 시, 트리거 사용 안함
      • --disable-triggers
    • $ quoting: SQL 표준 따옴표를 사용하고 $ 인용구 사용 안함
      • --disable-dollar-quoting
    • With OID(s): OID 포함하여 덤프
      • -o, --oids
      • 버전 및 옵션 지원 여부 확인 필요
    • Verbose messages: Verbose 모드 사용(세부 작업 내용 출력)
      • -v, --verbose
    • Force double quote on identifiers: 키워드가 아니더라도 모든 식별자에 강제로 인용구(") 적용
      • --quote-all-identifiers
    • User SET SESSION AUTHORIZATION: ALTER OWNER 대신 SET SESSION AUTHORIZATION 명령을 사용해 소유권 설정
      • --use-set-session-authorization

    pg_restore

    • Format: 백업 파일 포맷
      • custom, directory, tar, plaintext(default)
      • -F, --format
      • ex) -Fc or -F c
      • ex) -Fd or --format=d
      • ex) -Ft or --format=t
    • Filename: 출력 파일 이름(stdout : -)
      • -f, --file
    • Number of jobs: 명시한 개수 만큼 job을 병렬로 사용해 덤프
      • -j, --jobs
      • ex) -j 5
    • Role name: 복원 전에 SET ROLE 수행
      • --role

    • Sections: 명시된 섹션만 복원.
      • pre-data, data, post-data
      • 디폴트는 모든 섹션을 복원
      • ex) --section=pre-data
      • ex) --section=data
      • ex) --section=post-data
    • Only Data: 스키마가 아닌 데이터만 복원
      • -a, --data-only
    • Only schema: 데이터가 아닌 스키마만 복원
      • -s, --schema-only
    • Owner: (Plaintext 포맷에서)원시 데이터베이스와 일치하도록 object 소유권을 설정하는 명령 건너뛰기
      • -O, --no-owner
    • Privilege: 접근 권한(privileges)(grant/revoke) 덤프 안 함
      • -x, --no-privileges
    • Tablespace: Tablespace 할당 덤프 안 함
      • --no-tablespaces
    • Comments: 주석 덤프 안함
      • --no-comments

    • Include CREATE DATABASE statement: 복원 전에 대상 데이터베이스를 생성
      • -C, --create
    • Include DROP DATABASE statement: 다시 만들기(recreating) 전에 데이터베이스 object 삭제(DROP)
      • -c, --clean
    • Single translaction: 단일 트랜잭션으로 복원 실행
      • -1, --single-transaction
    • Trigger: data-only 복원 시, 트리거 사용 안함
      • --disable-triggers
    • No data for Failed Tables: 생성할 수 없는 테이블의 데이터는 복원 안함
      • --no-data-for-failed-tables
    • Verbose messages: Verbose 모드 사용(세부 작업 내용 출력)
      • -v, --verbose
    • User SET SESSION AUTHORIZATION: ALTER OWNER 대신 SET SESSION AUTHORIZATION 명령을 사용해 소유권 설정
      • --use-set-session-authorization
    • Exit on error: 에러 발생 시 종료.
      • 디폴트는 계속 진행(복원 작업 종료 후 에러 카운트 출력)
      • -e, --exit-on-error

    댓글

    Designed by JB FACTORY