pg_dump執行個體詳解(備份postgresql和greenplum資料庫)

來源:互聯網
上載者:User

標籤:包括   列印   硬體   超級使用者   attach   ssi   action   問題   jobs   

一、pg_dump的用法:
資料庫的匯入匯出是最常用的功能之一,每種資料庫都提供有這方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldump,而PostgreSQL提供的對應工具為pg_dump和pg_restore。
pg_dump是用於備份PostgreSQL資料庫的工具。它可以在資料庫正在使用的時候進行完整一致的備份,並不阻塞其它使用者對資料庫的訪問。
轉儲格式可以是一個指令碼或者歸檔檔案。轉儲指令碼的格式是純文字,包含許多SQL命令,這些SQL命令可以用於重建該資料庫並將之恢複到儲存指令碼時的狀態。可以使用 psql從這樣的指令碼中恢複。它們甚至可以用於在其它機器甚至是其它硬體體系的機器上重建資料庫,通過對指令碼進行一些修改,甚至可以在其它SQL資料庫產品上重建資料庫。
歸檔檔案格式必須和pg_restore一起使用重建資料庫。它們允許pg_restore對恢複什麼東西進行選擇,甚至是在恢複之前對需要恢複的條目進行重新排序。歸檔檔案也是可以跨平台移植的。
D:\Program Files\PowerCmd>pg_dump --help
pg_dump 把一個資料庫轉儲為純文字檔案或者是其它格式.
用法:  pg_dump [選項]... [資料庫名字]
一般選項:
  -f, --file=FILENAME         output file or directory name
  -F, --format=c|d|t|p        output file format (custom, directory, tar, plain text)
  -v, --verbose            詳細模式
  -Z, --compress=0-9       被壓縮格式的壓縮層級
--lock-wait-timeout=TIMEOUT 在等待表鎖逾時後操作失敗
  --help                       顯示此協助資訊, 然後退出
  --versoin                    輸出版本資訊, 然後退出
控制輸出內容選項:
  -a, --data-only          只轉儲資料,不包括模式
  -b, --blobs              在轉儲中包括大對象
  -c, --clean              在重新建立之前,先清除(刪除)資料庫物件
  -C, --create             在轉儲中包括命令,以便建立資料庫
  -E, --encoding=ENCODING     轉儲以ENCODING形式編碼的資料
  -n, --schema=SCHEMA      只轉儲指定名稱的模式
 -N, --exclude-schema=SCHEMA     不轉儲已命名的模式
  -o, --oids               在轉儲中包括 OID
  -O, --no-owner           在明文格式中, 忽略恢複對象所屬者
  -s, --schema-only        只轉儲模式, 不包括資料
  -S, --superuser=NAME     在轉儲中, 指定的超級使用者名稱
  -t, --table=TABLE        只轉儲指定名稱的表
  -T, --exclude-table=TABLE       只轉儲指定名稱的表
  -x, --no-privileges      不要轉儲許可權 (grant/revoke)
  --binary-upgrade         只能由升級工具使用
  --column-inserts          以帶有列名的INSERT命令形式轉儲資料
  --disable-dollar-quoting     取消美元 (符號) 引號, 使用 SQL 標準引號
  --disable-triggers         在只恢複資料的過程中禁用觸發器
  --inserts                 以INSERT命令,而不是COPY命令的形式轉儲資料
  --no-security-labels        do not dump security label assignments
  --no-tablespaces           不轉儲資料表空間分配資訊
  --no-unlogged-table-data    do not dump unlogged table data
  --quote-all-identifiers     quote all identifiers, even if not key words
  --serializable-deferrable   wait until the dump can run without anomalies
 --use-set-session-authorization
   使用 SESSION AUTHORIZATION 命令代替ALTER OWNER 命令來設定所有權
聯結選項:
  -h, --host=主機名稱        資料庫伺服器的主機名稱或通訊端目錄
  -p, --port=連接埠號碼        資料庫伺服器的連接埠號碼
  -U, --username=名字      以指定的資料庫使用者聯結
  -w, --no-password        永遠不提示輸入口令
  -W, --password           強制口令提示 (自動)
  --role=ROLENAME          do SET ROLE before dump
如果沒有提供資料庫名字, 那麼使用 PGDATABASE 環境變數的數值.

二、pg_dump的使用執行個體
1、建立兩個資料庫
CREATE DATABASE "TestDb1"
  WITH OWNER = "TestRole1"
       ENCODING = ‘UTF8‘
       TABLESPACE = "TestTbs1";
CREATE DATABASE "TestDb2"
  WITH OWNER = "TestRole1"
       ENCODING = ‘UTF8‘
       TABLESPACE = "TestTbs1";
在TestDb1中建立表csm_bill、cfg_public_int_transport插入幾條記錄,並建立索引,索引使用索引資料表空間TestTbsIndex。

2、僅遷移資料庫結構:
E:\>pg_dump -U TestRole1 -s -f TestDb1.sql TestDb1
口令:
-U TestRole1和超級使用者-U postgres結果完全相同:
E:\>pg_dump -U postgres -s -f TestDb11.sql TestDb1
E:\>psql -U TestRole2 -f TestDb1.sql TestDb2 >a.txt 2>&1
使用者 TestRole2 的口令:
匯入時,使用-U TestRole2往往有很多許可權不夠,要想成功匯入需要修改相關資料庫物件的屬主,所以最好使用超級使用者-U postgres:
E:\>psql -U postgres -f TestDb1.sql TestDb2 >a.txt 2>&1
不轉儲許可權選項:-x
E:\>pg_dump -U postgres -x -s -f TestDb12.sql TestDb1
TestDb12.sql比TestDb1.sql少了一下幾行:


為了可以多次運行TestDb1.sql,可以在檔案開始加以下兩行:
drop schema public cascade;
create schema public;
或者使用-c選項:
E:\>pg_dump -U postgres -c -x -s -f TestDb13.sql TestDb1
TestDb13.sql比TestDb1.sql多以下幾行:

此時,可以多次運行:
E:\>psql -U postgres -f TestDb13.sql TestDb2 >a.txt 2>&1
但是,如果兩個庫有不同的表或索引,應該使用第一種方法,因為第二種方法在找不到某些資料庫物件時會報錯。

3、遷移資料庫結構和資料(可以實現資料庫的備份與恢複)
資料的複製使用copy命令:
E:\>pg_dump -U postgres TestDb1>TestDb14.sql

資料的複製使用insert語句:
E:\>pg_dump -U postgres --column-inserts TestDb1>TestDb15.sql

4、把遠程linux上PostgreSQL上的cpost資料庫結構遷移至本地PostgreSQL
(1)在本地建一個完全相同的環境
create user "cpost" inherit createdb;
create tablespace "pis_data" owner cpost location ‘E:\PostgreSQL/data/pis_data‘;
create tablespace "pis_index" owner cpost location ‘E:\PostgreSQL/data/pis_index‘;
遠端資料庫cpost仍使用了預設資料表空間:
CREATE DATABASE cpost
  WITH OWNER = cpost
       --ENCODING = ‘LATIN9‘
       TABLESPACE = pg_default
       --LC_COLLATE = ‘C‘
       --LC_CTYPE = ‘C‘
       CONNECTION LIMIT = -1;
使用以上三個參數報錯,建成後的資料庫如下:
CREATE DATABASE cpost
  WITH OWNER = cpost
       ENCODING = ‘UTF8‘
       TABLESPACE = pg_default
       LC_COLLATE = ‘Chinese (Simplified)_People‘‘s Republic of China.936‘
       LC_CTYPE = ‘Chinese (Simplified)_People‘‘s Republic of China.936‘
       CONNECTION LIMIT = -1;
(2)使用pg_dump遷移表結構
使用-h選項,使匯出的sql檔案直接存放在本地:
E:\>pg_dump -h 132.10.10.11 -p 1234 -U cpost -x -s -f cpost.sql cpost
E:\>psql -U postgres -f cpost.sql
匯入成功,但報了一個錯誤:
psql:cpost.sql:22: ERROR:  character 0xe99499 of encoding "UTF8" has no equivalent in "LATIN9"
字元集錯誤,字元集問題詳見我的另一篇blog:由PostgreSQL的地區與字元集說起

三、使用pg_dump和pg_restore實現資料庫的備份與恢複
E:\>pg_restore --help
pg_restore 從一個歸檔中恢複一個由 pg_dump 建立的 PostgreSQL 資料庫.
用法:
  pg_restore [選項]... [檔案名稱]
一般選項:
  -d, --dbname=名字        串連資料庫名字
  -f, --file=檔案名稱        輸出檔案名
  -F, --format=c|d|t       backup file format (should be automatic)
  -l, --list               列印歸檔檔案的 TOC 概述
  -v, --verbose            詳細模式
  --help                   顯示此協助資訊, 然後退出
  --version                輸出版本資訊, 然後退出恢複控制選項:
  -a, --data-only          只恢複資料, 不包括模式
  -c, --clean              在重新建立資料庫物件之前需要清除(刪除)資料庫物件
  -C, --create             建立目標資料庫
  -e, --exit-on-error      發生錯誤退出, 預設為繼續
  -I, --index=名稱         恢複指定名稱的索引
  -j, --jobs=NUM           可以執行多個任務並行進行恢複工作
  -L, --use-list=檔案名稱    從這個檔案中使用指定的內容表排序輸出
  -n, --schema=NAME      在這個模式中只恢複對象
  -O, --no-owner           忽略恢複對象所屬者
  -P, --function=名字(參數)  恢複指定名字的函數
  -s, --schema-only        只復原模式, 不包括資料
  -S, --superuser=NAME     使用指定的超級使用者來禁用觸發器
  -t, --table=NAME         恢複指定命字的表
  -T, --trigger=NAME       恢複指定命字的觸發器
  -x, --no-privileges      跳過處理許可權的恢複 (grant/revoke)
  -1, --single-transaction   作為單個事務恢複
 --disable-triggers        在只恢複資料的過程中禁用觸發器
  --no-data-for-failed-tables 沒有恢複無法建立表的資料
  --no-security-labels     do not restore security labels
--no-tablespaces          不恢複資料表空間的分配資訊
  --use-set-session-authorization 使用 SESSION AUTHORIZATION 命令代替ALTER OWNER命令來設定對象所有權
聯結選項:
  -h, --host=主機名稱        資料庫伺服器的主機名稱或通訊端目錄
  -p, --port=連接埠號碼        資料庫伺服器的連接埠號碼
  -U, --username=名字      以指定的資料庫使用者聯結
  -w, --no-password        永遠不提示輸入口令
  -W, --password           強制口令提示 (自動)
  --role=ROLENAME          在恢複前執行SET ROLE操作
如果沒有提供輸入檔案名稱, 則使用標準輸入.
1、使用dump格式備份和恢複:
E:\>pg_dump -U postgres -Fc TestDb1 >TestDb1.dump
postgres=# drop database "TestDb2";
DROP DATABASE
postgres=# create database "TestDb2"
postgres-# with owner="TestRole2"
postgres-# tablespace="TestTbs2";
CREATE DATABASE
E:\>pg_restore -U postgres -d TestDb2 TestDb1.dump >a.txt 2>&1
2、使用tar格式備份和恢複:
E:\>pg_dump -U postgres -Ft TestDb1>TestDb1.tar
postgres=# drop database "TestDb2";
DROP DATABASE
postgres=# create database "TestDb2"
postgres-# with owner="TestRole2"
postgres-# tablespace="TestTbs2";
CREATE DATABASE
E:\>pg_restore -U postgres -d TestDb2 TestDb1.tar >a.txt 2>&1

pg_dump執行個體詳解(備份postgresql和greenplum資料庫)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.