PostgreSQL pg_dump&psql 資料的備份與恢複

來源:互聯網
上載者:User

標籤:section   binary   變數   style   com   sage   lte   出版   系統變數   

Usage:
  pg_dump [OPTION]... [DBNAME] 資料庫名放最後,不指定預設是系統變數PGDATABASE指定的資料庫。

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在等待表鎖逾時後操作失敗
  -?, --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在轉儲中包括大對象
  -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轉儲以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在轉儲中包括 OID
  -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)不要轉儲許可權 (grant/revoke)
  --binary-upgrade             for use by upgrade utilities only只能由升級工具使用
  --column-inserts             dump data as INSERT commands with column names以帶有列名的INSERT命令形式轉儲資料
  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting取消美元 (符號) 引號, 使用 SQL 標準引號
  --disable-triggers           disable triggers during data-only restore在只恢複資料的過程中禁用觸發器
  --exclude-table-data=TABLE   do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式轉儲資料
  --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不轉儲資料表空間分配資訊
  --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 資料庫名
  -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

If no database name is supplied, then the PGDATABASE environment如果沒有提供資料庫名字, 那麼使用 PGDATABASE 環境變數的數值. 
variable value is used.

Report bugs to <[email protected]>.

一: 純檔案格式的指令碼: 
樣本:
1. 只匯出postgres資料庫的資料,不包括模式 -s
   pg_dump -U postgres -f /postgres.sql -s postgres(資料庫名)
2. 匯出postgres資料庫(包括資料)
   pg_dump -U postgres -f /postgres.sql  postgres(資料庫名)
3. 匯出postgres資料庫中表test01的資料
   create database "test01" with owner="postgres" encoding=‘utf-8‘;(單引號,雙引號不能錯)
   pg_dump -U postgres -f /postgres.sql -t test01 postgres(資料庫名)
4. 匯出postgres資料庫中表test01的資料,以insert語句的形式
   pg_dump -U postgres -f /postgres.sql -t test01 --column-inserts postgres(資料庫名)
5. 恢複資料到bk01資料庫
  psql -U postgres -f /postgres.sql bk01

二、 使用歸檔檔案格式:
pg_restore
使用pg_restore純文字恢複純文字格式的指令碼,無法恢複
[[email protected] 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和歸檔檔案格式一起使用重建資料庫。

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 

三、 壓縮備份與恢複:
處理大資料庫:
1. 使用壓縮的轉儲. 使用你熟悉的壓縮程式,比如說 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 命令允許你 你用下面的方法把輸出分解成作業系統可以接受的大小。 比如,讓每個塊大小為 1 MB: 
 . 先備份:
   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

PostgreSQL pg_dump&psql 資料的備份與恢複

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.