PostgreSQL 数据库备份
PostgreSQL 數(shù)據(jù)庫備份 pg_dump
一、備份還原
注意:命令在pg_dump目錄下進行
1、備份test數(shù)據(jù)庫
pg_dump -h 127.0.0.1 -p 5432 -U username -c -f db_back.sql test2、還原數(shù)據(jù)到test2數(shù)據(jù)庫
psql -U postgres -f /db_back.sql test2?
二、命令詳解
- 基本命令
注:數(shù)據(jù)庫名放最后,不指定默認是系統(tǒng)變量PGDATABASE指定的數(shù)據(jù)庫。
?
- ?詳解
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并行數(shù)
? -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在等待表鎖超時后操作失敗
? -?, --help ? ? ? ? ? ? ? ? ? show this help, then exit顯示此幫助信息, 然后退出
Options controlling the output content:(控制輸出的選項)
? -a, --data-only ? ? ? ? ? ? ?dump only the data, not the schema只導出數(shù)據(jù),不包括模式
? -b, --blobs ? ? ? ? ? ? ? ? ?include large objects in dump在轉(zhuǎn)儲中包括大對象
? -c, --clean ? ? ? ? ? ? ? ? ?clean (drop) database objects before recreating在重新創(chuàng)建之前,先清除(刪除)數(shù)據(jù)庫對象
? -C, --create ? ? ? ? ? ? ? ? include commands to create database in dump在轉(zhuǎn)儲中包括命令,以便創(chuàng)建數(shù)據(jù)庫(包括建庫語句,無需在導入之前先建數(shù)據(jù)庫)
? -E, --encoding=ENCODING ? ? ?dump the data in encoding ENCODING轉(zhuǎn)儲以ENCODING形式編碼的數(shù)據(jù)
? -n, --schema=SCHEMA ? ? ? ? ?dump the named schema(s) only只轉(zhuǎn)儲指定名稱的模式
? -N, --exclude-schema=SCHEMA ?do NOT dump the named schema(s)不轉(zhuǎn)儲已命名的模式
? -o, --oids ? ? ? ? ? ? ? ? ? include OIDs in dump在轉(zhuǎn)儲中包括 OID
? -O, --no-owner ? ? ? ? ? ? ? skip restoration of object ownership in在明文格式中, 忽略恢復對象所屬者??plain-text format
? -s, --schema-only ? ? ? ? ? ?dump only the schema, no data只轉(zhuǎn)儲模式, 不包括數(shù)據(jù)(不導出數(shù)據(jù))
? -S, --superuser=NAME ? ? ? ? superuser user name to use in plain-text format在轉(zhuǎn)儲中, 指定的超級用戶名
? -t, --table=TABLE ? ? ? ? ? ?dump the named table(s) only只轉(zhuǎn)儲指定名稱的表
? -T, --exclude-table=TABLE ? ?do NOT dump the named table(s)只轉(zhuǎn)儲指定名稱的表
? -x, --no-privileges ? ? ? ? ?do not dump privileges (grant/revoke)不要轉(zhuǎn)儲權(quán)限 (grant/revoke)
? --binary-upgrade ? ? ? ? ? ? for use by upgrade utilities only只能由升級工具使用
? --column-inserts ? ? ? ? ? ? dump data as INSERT commands with column names以帶有列名的INSERT命令形式轉(zhuǎn)儲數(shù)據(jù)
? --disable-dollar-quoting ? ? disable dollar quoting, use SQL standard quoting取消美元 (符號) 引號, 使用 SQL 標準引號
? --disable-triggers ? ? ? ? ? disable triggers during data-only restore在只恢復數(shù)據(jù)的過程中禁用觸發(fā)器
? --exclude-table-data=TABLE ? do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式轉(zhuǎn)儲數(shù)據(jù)
? --inserts ? ? ? ? ? ? ? ? ? ?dump data as INSERT commands, rather than COPY
? --no-security-labels ? ? ? ? do not dump security label assignments
? --no-synchronized-snapshots ?do not use synchronized snapshots in parallel jobs
? --no-tablespaces ? ? ? ? ? ? do not dump tablespace assignments不轉(zhuǎn)儲表空間分配信息
? --no-unlogged-table-data ? ? do not dump unlogged table data
? --quote-all-identifiers ? ? ?quote all identifiers, even if not key words
? --section=SECTION ? ? ? ? ? ?dump named section (pre-data, data, or post-data)
? --serializable-deferrable ? ?wait until the dump can run without anomalies
? --use-set-session-authorization
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?use SET SESSION AUTHORIZATION commands instead of
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ALTER OWNER commands to set ownership
Connection options:(控制連接的選項)
? -d, --dbname=DBNAME ? ? ?database to dump 數(shù)據(jù)庫名
? -h, --host=HOSTNAME ? ? ?database server host or socket directory數(shù)據(jù)庫服務器的主機名或套接字目錄
? -p, --port=PORT ? ? ? ? ?database server port number數(shù)據(jù)庫服務器的端口號
? -U, --username=NAME ? ? ?connect as specified database user以指定的數(shù)據(jù)庫用戶聯(lián)接
? -w, --no-password ? ? ? ?never prompt for password永遠不提示輸入口令
? -W, --password ? ? ? ? ? force password prompt (should happen automatically)強制口令提示 (自動)
? --role=ROLENAME ? ? ? ? ?do SET ROLE before dump
?
一: 純文件格式的腳本:?
示例:
1. 只導出postgres數(shù)據(jù)庫的數(shù)據(jù),不包括模式 -s
? ??
2. 導出postgres數(shù)據(jù)庫(包括數(shù)據(jù))
? ?
pg_dump -U postgres -f /postgres.sql postgres(數(shù)據(jù)庫名)?
3. 導出postgres數(shù)據(jù)庫中表test01的數(shù)據(jù)
? ??
4. 導出postgres數(shù)據(jù)庫中表test01的數(shù)據(jù),以insert語句的形式
? ?
pg_dump -U postgres -f /postgres.sql -t test01 --column-inserts postgres(數(shù)據(jù)庫名)?
5. 恢復數(shù)據(jù)到bk01數(shù)據(jù)庫
? ?
?
二、 使用歸檔文件格式:
pg_restore
使用pg_restore純文本恢復純文本格式的腳本,無法恢復
[root@localhost postgres-9.3.5]# pg_restore -U postgres -d bk01 ?/mnt/hgfs/window\&ubuntu\ shared\ folder/vendemo.sql?
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
pg_restore和歸檔文件格式一起使用重建數(shù)據(jù)庫。
1. 先備份:?
? ?pg_dump -U postgres -F t -f /vendemo.tar vendemo ?備份下來有800多k
?. 恢復:
? ?pg_restore -U postgres -d bk01 /vendemo.tar?
2. 先備份:?
? ?pg_dump -U postgres -F c -f /vendemo.tar vendemo ?備份下來有300多k
?. 恢復:
? ?pg_restore -U postgres -d bk01 /vendemo.tar?
三、 壓縮備份與恢復:
處理大數(shù)據(jù)庫:
1. 使用壓縮的轉(zhuǎn)儲. 使用你熟悉的壓縮程序,比如說 gzip。
?. 先備份:
? ?pg_dump -U postgres vendemo | gzip > /vendemo.gz 備份下來只有30多k
?. 恢復:
? ?gunzip -c /vendemo.gz | psql -U postgres bk02
?或者
? ?cat /vendemo.gz | gunzip | psql -U postgres bk02
2. 使用 split。. split 命令允許你 你用下面的方法把輸出分解成操作系統(tǒng)可以接受的大小。 比如,讓每個塊大小為 1 兆字節(jié):?
?. 先備份:
? ?pg_dump -U postgres -d vendemo | split -b 100k - /vend/vend
? ?導出來的樣子是 ? vendaa 100k
? ?vendab 100k
? ?vendac 100k
? ?vendad 16k
?. 恢復:
? cat /vend/vend* | psql -U postgres bk02
轉(zhuǎn)載于:https://www.cnblogs.com/BillyYoung/p/11057854.html
總結(jié)
以上是生活随笔為你收集整理的PostgreSQL 数据库备份的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 渐变色 + 屏幕缩小自动产生滚动条
- 下一篇: 【AtCoder】diverta 201