| 양쪽 이전 판 이전 판 | |
| postgre:pg_dump [2026/06/28 02:34] – ↷ 문서가 postgre:pg_dump에서 postgresql:pg_dump(으)로 이동되었습니다 writer | postgre:pg_dump [2026/06/28 02:34] (현재) – 만듦 - 바깥 편집 (알 수 없는 날짜) 127.0.0.1 |
|---|
| | {{tag>[cli postgres postgresql backup dump]}} |
| | |
| | ====== pg_dump ====== |
| | PostgreSQL 데이터베이스를 logical backup으로 내보내는 명령이다. plain SQL, custom archive, directory, tar format을 지원하며 schema-only 또는 data-only dump에도 자주 쓴다. |
| | |
| | ===== Summary ===== |
| | * 단일 database 또는 schema/table 단위 logical backup 생성에 사용한다. |
| | * 복원 경로를 생각하면 ''-F c'' custom format이나 ''-F d'' directory format이 유연하다. |
| | * plain SQL dump는 사람이 읽기 쉽고 diff가 가능하지만, 복원은 일반적으로 ''psql''로 수행한다. |
| | |
| | ===== Usage ===== |
| | <code bash> |
| | pg_dump [OPTION]... [DBNAME] |
| | pg_dump -d appdb -f appdb.sql |
| | pg_dump -Fc -d appdb -f appdb.dump |
| | pg_dump -Fd -j 4 -d appdb -f appdb.dir |
| | </code> |
| | |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>[<switches>...]</color> <color #7092be>[DBNAME]</color>'' |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>**-f**</color> <color #7092be>FILE</color>'' 출력 파일 또는 디렉터리 지정 |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>**-F**</color> <color #7092be>p|c|d|t</color>'' 출력 형식 지정 |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>**-s**</color>'' schema-only dump |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>**-a**</color>'' data-only dump |
| | * ''<color #c3c3c3>**pg_dump**</color> <color #22b14c>**-n**</color> <color #7092be>SCHEMA</color>'', ''<color #22b14c>**-t**</color> <color #7092be>TABLE</color>'' 범위 제한 |
| | |
| | ===== Options ===== |
| | * ''<color #22b14c>**-f**</color>'', ''<color #22b14c>**--file**</color>'' 출력 파일 또는 directory 이름 |
| | * ''<color #22b14c>**-F**</color>'', ''<color #22b14c>**--format**</color>'' ''p'' plain, ''c'' custom, ''d'' directory, ''t'' tar |
| | * ''<color #22b14c>**-j**</color>'', ''<color #22b14c>**--jobs**</color>'' 병렬 dump worker 수. directory format에서 자주 사용 |
| | * ''<color #22b14c>**-Z**</color>'', ''<color #22b14c>**--compress**</color>'' 압축 레벨 지정 |
| | * ''<color #22b14c>**-a**</color>'', ''<color #22b14c>**--data-only**</color>'' 데이터만 dump |
| | * ''<color #22b14c>**-s**</color>'', ''<color #22b14c>**--schema-only**</color>'' schema만 dump |
| | * ''<color #22b14c>**-c**</color>'', ''<color #22b14c>**--clean**</color>'' 복원 전에 object drop SQL 포함 |
| | * ''<color #22b14c>**-C**</color>'', ''<color #22b14c>**--create**</color>'' database 생성 SQL 포함 |
| | * ''<color #22b14c>**-n**</color>'', ''<color #22b14c>**--schema**</color>'' 특정 schema만 dump |
| | * ''<color #22b14c>**-N**</color>'', ''<color #22b14c>**--exclude-schema**</color>'' 특정 schema 제외 |
| | * ''<color #22b14c>**-t**</color>'', ''<color #22b14c>**--table**</color>'' 특정 table만 dump |
| | * ''<color #22b14c>**-T**</color>'', ''<color #22b14c>**--exclude-table**</color>'' 특정 table 제외 |
| | * ''<color #22b14c>**-O**</color>'', ''<color #22b14c>**--no-owner**</color>'' ownership 복원 정보 생략 |
| | * ''<color #22b14c>**-x**</color>'', ''<color #22b14c>**--no-privileges**</color>'' GRANT/REVOKE 정보 생략 |
| | * ''<color #22b14c>**-h**</color>'', ''<color #22b14c>**--host**</color>'', ''<color #22b14c>**-p**</color>'', ''<color #22b14c>**-U**</color>'' 접속 정보 지정 |
| | |
| | ===== Examples ===== |
| | <code bash> |
| | # 사람이 읽을 수 있는 plain SQL dump |
| | pg_dump -d appdb -f ./appdb.sql |
| | |
| | # pg_restore로 다루기 좋은 custom archive |
| | pg_dump -Fc -d appdb -f ./appdb.dump |
| | |
| | # 병렬 dump용 directory format |
| | pg_dump -Fd -j 4 -d appdb -f ./appdb.dir |
| | |
| | # schema만 백업 |
| | pg_dump -s -d appdb -f ./appdb-schema.sql |
| | |
| | # data만 백업 |
| | pg_dump -a -d appdb -f ./appdb-data.sql |
| | |
| | # 특정 schema만 백업 |
| | pg_dump -n public -d appdb -f ./public.sql |
| | |
| | # 특정 table만 백업 |
| | pg_dump -t public.users -d appdb -f ./users.sql |
| | </code> |
| | |
| | ===== Troubleshooting ===== |
| | * long-running transaction과 lock 대기가 심하면 ''--lock-wait-timeout''을 검토한다. |
| | * restore 대상을 유연하게 제어하려면 plain SQL보다 ''-Fc'' 또는 ''-Fd''가 보통 낫다. |
| | * ownership이나 privilege 차이 때문에 복원 실패가 예상되면 backup 단계에서 ''-O -x'' 조합을 검토한다. |
| | * very large database는 logical dump보다 physical backup 전략이 더 적절할 수 있다. |
| | |
| | ===== Compatibility ===== |
| | * logical backup은 major version 간 이식성에 유리하지만, extension과 collation, role 구조 차이는 별도 확인이 필요하다. |
| | * plain format은 ''psql''로 복원하고, ''custom/directory/tar'' archive는 [[postgresql:pg_restore]]로 복원하는 흐름이 일반적이다. |
| | |
| | <note warning> |
| | ''pg_dump''는 백업 생성 도구다. plain SQL dump에 ''-c'' 또는 ''-C''를 넣으면 복원 시 기존 object를 drop하거나 database를 다시 만들 수 있으므로 production 적용 전에 내용을 확인한다. |
| | </note> |
| | |
| | ===== Help ===== |
| | ++++ pg_dump --help | |
| | <code text> |
| | pg_dump dumps a database as a text file or to other formats. |
| | |
| | Usage: |
| | pg_dump [OPTION]... [DBNAME] |
| | |
| | General options: |
| | -f, --file=FILENAME output file or directory name |
| | -F, --format=c|d|t|p output file format (custom, directory, tar, |
| | plain text (default)) |
| | -j, --jobs=NUM use this many parallel jobs to dump |
| | -v, --verbose verbose mode |
| | -V, --version output version information, then exit |
| | -Z, --compress=0-9 compression level for compressed formats |
| | --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock |
| | --no-sync do not wait for changes to be written safely to disk |
| | -?, --help show this help, then exit |
| | |
| | Options controlling the output content: |
| | -a, --data-only dump only the data, not the schema |
| | -b, --blobs include large objects in dump |
| | -B, --no-blobs exclude large objects in dump |
| | -c, --clean clean (drop) database objects before recreating |
| | -C, --create include commands to create database in dump |
| | -E, --encoding=ENCODING dump the data in encoding ENCODING |
| | -n, --schema=SCHEMA dump the named schema(s) only |
| | -N, --exclude-schema=SCHEMA do NOT dump the named schema(s) |
| | -o, --oids include OIDs in dump |
| | -O, --no-owner skip restoration of object ownership in |
| | plain-text format |
| | -s, --schema-only dump only the schema, no data |
| | -S, --superuser=NAME superuser user name to use in plain-text format |
| | -t, --table=TABLE dump the named table(s) only |
| | -T, --exclude-table=TABLE do NOT dump the named table(s) |
| | -x, --no-privileges do not dump privileges (grant/revoke) |
| | --disable-triggers disable triggers during data-only restore |
| | --exclude-table-data=TABLE do NOT dump data for the named table(s) |
| | --if-exists use IF EXISTS when dropping objects |
| | --inserts dump data as INSERT commands, rather than COPY |
| | --no-publications do not dump publications |
| | --no-security-labels do not dump security label assignments |
| | --no-subscriptions do not dump subscriptions |
| | --no-tablespaces do not dump tablespace assignments |
| | --section=SECTION dump named section (pre-data, data, or post-data) |
| | |
| | Connection options: |
| | -d, --dbname=DBNAME database to dump |
| | -h, --host=HOSTNAME database server host or socket directory |
| | -p, --port=PORT database server port number |
| | -U, --username=NAME connect as specified database user |
| | -w, --no-password never prompt for password |
| | -W, --password force password prompt (should happen automatically) |
| | --role=ROLENAME do SET ROLE before dump |
| | </code> |
| | ++++ |
| | |
| | ===== See Also ===== |
| | * [[postgresql:psql]] |
| | * [[postgresql:pg_restore]] |
| | * [[postgresql:command]] |
| | |
| | ===== History ===== |
| | * codex:: 2026-06-22 Rebuilt pg_dump as a structured backup reference with format selection, examples, and restore guidance. |
| |