PostgreSQL 数据库备份
PostgreSQL 數(shù)據(jù)庫(kù)備份 pg_dump
一、備份還原
注意:命令在pg_dump目錄下進(jìn)行
1、備份test數(shù)據(jù)庫(kù)
pg_dump -h 127.0.0.1 -p 5432 -U username -c -f db_back.sql test2、還原數(shù)據(jù)到test2數(shù)據(jù)庫(kù)
psql -U postgres -f /db_back.sql test2?
二、命令詳解
- 基本命令
注:數(shù)據(jù)庫(kù)名放最后,不指定默認(rèn)是系統(tǒng)變量PGDATABASE指定的數(shù)據(jù)庫(kù)。
?
- ?詳解
General options:(一般選項(xiàng))
? -f, --file=FILENAME ? ? ? ? ?output file or directory name導(dǎo)出后保存的文件名
? -F, --format=c|d|t|p? ? ? ? ? ? output file format (custom, directory, tar,導(dǎo)出文件的格式??plain text (default))
? -j, --jobs=NUM? ? ? ? ? ? ? ? ? use this many parallel jobs to dump并行數(shù)
? -v, --verbose ? ? ? ? ? ? ? ? ? verbose mode 詳細(xì)模式
? -V, --version ? ? ? ? ? ? ? ? ? output version information, then exit輸出版本信息, 然后退出
? -Z, --compress=0-9? ? ? ? ? compression level for compressed formats被壓縮格式的壓縮級(jí)別
? --lock-wait-timeout=TIMEOUT ?fail after waiting TIMEOUT for a table lock在等待表鎖超時(shí)后操作失敗
? -?, --help ? ? ? ? ? ? ? ? ? show this help, then exit顯示此幫助信息, 然后退出
Options controlling the output content:(控制輸出的選項(xiàng))
? -a, --data-only ? ? ? ? ? ? ?dump only the data, not the schema只導(dǎo)出數(shù)據(jù),不包括模式
? -b, --blobs ? ? ? ? ? ? ? ? ?include large objects in dump在轉(zhuǎn)儲(chǔ)中包括大對(duì)象
? -c, --clean ? ? ? ? ? ? ? ? ?clean (drop) database objects before recreating在重新創(chuàng)建之前,先清除(刪除)數(shù)據(jù)庫(kù)對(duì)象
? -C, --create ? ? ? ? ? ? ? ? include commands to create database in dump在轉(zhuǎn)儲(chǔ)中包括命令,以便創(chuàng)建數(shù)據(jù)庫(kù)(包括建庫(kù)語(yǔ)句,無(wú)需在導(dǎo)入之前先建數(shù)據(jù)庫(kù))
? -E, --encoding=ENCODING ? ? ?dump the data in encoding ENCODING轉(zhuǎn)儲(chǔ)以ENCODING形式編碼的數(shù)據(jù)
? -n, --schema=SCHEMA ? ? ? ? ?dump the named schema(s) only只轉(zhuǎn)儲(chǔ)指定名稱的模式
? -N, --exclude-schema=SCHEMA ?do NOT dump the named schema(s)不轉(zhuǎn)儲(chǔ)已命名的模式
? -o, --oids ? ? ? ? ? ? ? ? ? include OIDs in dump在轉(zhuǎn)儲(chǔ)中包括 OID
? -O, --no-owner ? ? ? ? ? ? ? skip restoration of object ownership in在明文格式中, 忽略恢復(fù)對(duì)象所屬者??plain-text format
? -s, --schema-only ? ? ? ? ? ?dump only the schema, no data只轉(zhuǎn)儲(chǔ)模式, 不包括數(shù)據(jù)(不導(dǎo)出數(shù)據(jù))
? -S, --superuser=NAME ? ? ? ? superuser user name to use in plain-text format在轉(zhuǎn)儲(chǔ)中, 指定的超級(jí)用戶名
? -t, --table=TABLE ? ? ? ? ? ?dump the named table(s) only只轉(zhuǎn)儲(chǔ)指定名稱的表
? -T, --exclude-table=TABLE ? ?do NOT dump the named table(s)只轉(zhuǎn)儲(chǔ)指定名稱的表
? -x, --no-privileges ? ? ? ? ?do not dump privileges (grant/revoke)不要轉(zhuǎn)儲(chǔ)權(quán)限 (grant/revoke)
? --binary-upgrade ? ? ? ? ? ? for use by upgrade utilities only只能由升級(jí)工具使用
? --column-inserts ? ? ? ? ? ? dump data as INSERT commands with column names以帶有列名的INSERT命令形式轉(zhuǎn)儲(chǔ)數(shù)據(jù)
? --disable-dollar-quoting ? ? disable dollar quoting, use SQL standard quoting取消美元 (符號(hào)) 引號(hào), 使用 SQL 標(biāo)準(zhǔn)引號(hào)
? --disable-triggers ? ? ? ? ? disable triggers during data-only restore在只恢復(fù)數(shù)據(jù)的過(guò)程中禁用觸發(fā)器
? --exclude-table-data=TABLE ? do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式轉(zhuǎn)儲(chǔ)數(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)儲(chǔ)表空間分配信息
? --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:(控制連接的選項(xiàng))
? -d, --dbname=DBNAME ? ? ?database to dump 數(shù)據(jù)庫(kù)名
? -h, --host=HOSTNAME ? ? ?database server host or socket directory數(shù)據(jù)庫(kù)服務(wù)器的主機(jī)名或套接字目錄
? -p, --port=PORT ? ? ? ? ?database server port number數(shù)據(jù)庫(kù)服務(wù)器的端口號(hào)
? -U, --username=NAME ? ? ?connect as specified database user以指定的數(shù)據(jù)庫(kù)用戶聯(lián)接
? -w, --no-password ? ? ? ?never prompt for password永遠(yuǎn)不提示輸入口令
? -W, --password ? ? ? ? ? force password prompt (should happen automatically)強(qiáng)制口令提示 (自動(dòng))
? --role=ROLENAME ? ? ? ? ?do SET ROLE before dump
?
一: 純文件格式的腳本:?
示例:
1. 只導(dǎo)出postgres數(shù)據(jù)庫(kù)的數(shù)據(jù),不包括模式 -s
? ??
2. 導(dǎo)出postgres數(shù)據(jù)庫(kù)(包括數(shù)據(jù))
? ?
pg_dump -U postgres -f /postgres.sql postgres(數(shù)據(jù)庫(kù)名)?
3. 導(dǎo)出postgres數(shù)據(jù)庫(kù)中表test01的數(shù)據(jù)
? ??
4. 導(dǎo)出postgres數(shù)據(jù)庫(kù)中表test01的數(shù)據(jù),以insert語(yǔ)句的形式
? ?
pg_dump -U postgres -f /postgres.sql -t test01 --column-inserts postgres(數(shù)據(jù)庫(kù)名)?
5. 恢復(fù)數(shù)據(jù)到bk01數(shù)據(jù)庫(kù)
? ?
?
二、 使用歸檔文件格式:
pg_restore
使用pg_restore純文本恢復(fù)純文本格式的腳本,無(wú)法恢復(fù)
[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ù)庫(kù)。
1. 先備份:?
? ?pg_dump -U postgres -F t -f /vendemo.tar vendemo ?備份下來(lái)有800多k
?. 恢復(fù):
? ?pg_restore -U postgres -d bk01 /vendemo.tar?
2. 先備份:?
? ?pg_dump -U postgres -F c -f /vendemo.tar vendemo ?備份下來(lái)有300多k
?. 恢復(fù):
? ?pg_restore -U postgres -d bk01 /vendemo.tar?
三、 壓縮備份與恢復(fù):
處理大數(shù)據(jù)庫(kù):
1. 使用壓縮的轉(zhuǎn)儲(chǔ). 使用你熟悉的壓縮程序,比如說(shuō) gzip。
?. 先備份:
? ?pg_dump -U postgres vendemo | gzip > /vendemo.gz 備份下來(lái)只有30多k
?. 恢復(fù):
? ?gunzip -c /vendemo.gz | psql -U postgres bk02
?或者
? ?cat /vendemo.gz | gunzip | psql -U postgres bk02
2. 使用 split。. split 命令允許你 你用下面的方法把輸出分解成操作系統(tǒng)可以接受的大小。 比如,讓每個(gè)塊大小為 1 兆字節(jié):?
?. 先備份:
? ?pg_dump -U postgres -d vendemo | split -b 100k - /vend/vend
? ?導(dǎo)出來(lái)的樣子是 ? vendaa 100k
? ?vendab 100k
? ?vendac 100k
? ?vendad 16k
?. 恢復(fù):
? cat /vend/vend* | psql -U postgres bk02
轉(zhuǎn)載于:https://www.cnblogs.com/BillyYoung/p/11057854.html
總結(jié)
以上是生活随笔為你收集整理的PostgreSQL 数据库备份的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 渐变色 + 屏幕缩小自动产生滚动条
- 下一篇: mysql 导入txt数据到数据表【原创