본문 바로가기
BACKEND/PostgreSQL

Ubuntu PostgreSQL 백업(backup) 및 복구(restore)

by 드로니뚜벅이 2023. 3. 12.

더 이상 PostgreSQL이 사용하지 않거나 새로운 버전으로 설치하고자 할 경우 먼저 기존에 작업했던 데이터를 백업을 해야 합니다.

이런 경우에 사용할 수 있는 명령어는 pg_dumpall이나 pg_dump 입니다.

 

1. 데이터베이스 단위로 백업

사용법: pg_dump  [connection-option...]  [option...]  [db-name]

 

2. 백업할 파일명을 지정하여 백업할 경우

사용법: pg_dum -f backup.dump [db-name]

 

3. 특정 테이블만 백업할 경우

사용법: pg_dump -f backup.dump -t [table-name]  [db-name]

 

4. 특정 테이블만 제외하고 백업할 경우

사용법: pg_dump -f backup-dump -T  [table-name]  [db-name]

 

5. 특정 포맷으로 백업할 경우

사용법1(텍스트 포맷): pg_dump -f backup.dump -Fp -T [table-name]  [db-name]

사용법2(TAR 포맷): pg_dump -f backup.dump -Ft -T [table-name]  [db-name]

사용법3(사용자 지정 포맷): pg_dump -f backup.dump -Fc -T [table-name]  [db-name]

 

그리고, 백업된 데이터를 복구할 때 사용하는 명령어는 pg_restore 입니다.

 

Examples (postgresql.org)

To dump a database called mydb into an SQL-script file:

$ pg_dump mydb > db.sql

To reload such a script into a (freshly created) database named newdb:

$ psql -d newdb -f db.sql

 

To dump a database into a custom-format archive file:

$ pg_dump -Fc mydb > db.dump

 

To dump a database into a directory-format archive:

$ pg_dump -Fd mydb -f dumpdir

 

To dump a database into a directory-format archive in parallel with 5 worker jobs:

$ pg_dump -Fd mydb -j 5 -f dumpdir

 

To reload an archive file into a (freshly created) database named newdb:

$ pg_restore -d newdb db.dump

 

To reload an archive file into the same database it was dumped from, discarding the current contents of that database:

$ pg_restore -d postgres --clean --create db.dump

 

To dump a single table named mytab:

$ pg_dump -t mytab mydb > db.sql

 

To dump all tables whose names start with emp in the detroit schema, except for the table named employee_log:

$ pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > db.sql

 

To dump all schemas whose names start with east or west and end in gsm, excluding any schemas whose names contain the word test:

$ pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sql

 

The same, using regular expression notation to consolidate the switches:

$ pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sql

 

To dump all database objects except for tables whose names begin with ts_:

$ pg_dump -T 'ts_*' mydb > db.sql

 

To specify an upper-case or mixed-case name in -t and related switches, you need to double-quote the name; else it will be folded to lower case (see Patterns below). But double quotes are special to the shell, so in turn they must be quoted. Thus, to dump a single table with a mixed-case name, you need something like

$ pg_dump -t "\"MixedCaseName\"" mydb > mytab.sql

'BACKEND > PostgreSQL' 카테고리의 다른 글

쿼리 로그(Query logs) 파일에 저장하기  (1) 2023.05.28
Linux(Ubuntu) pgAdmin4 설치  (0) 2023.03.11
pgRouting 설치  (0) 2023.01.31
PostGIS 설치  (0) 2023.01.31
PostgreSQL 설치  (0) 2022.07.14