postgresql使用pg_dump和pg_restore 實現跨服務器的數據庫遷移或備份


因為業務需求,需要將服務器上的postgre多個數據庫的數據整個庫得遷移到另一個postgre數據庫上。

一般表較少時,會使用postgre 的copy to 和 copy from 命令就能完成表的遷移,但這種方式需要target_database 上提前先創建好對應的表,並且每一個表都需要一次copy to 和copy from操作,當表比較多的時候,非常繁瑣。
因此我查詢了網上的方法,發現了pg_dump這個方法,但是網上的描述比較雜亂,因此我查詢了 postgresql的官方手冊 https://www.postgresql.org/docs/9.2/app-pgdump.html
使用pg_dump和pg_restore可以非常快速進行整個database的數據遷移或者備份。
以下是pg_dump的部分選項,pg_restore相似:
 1 -F format
 2 --format=format
 3 Selects the format of the output. format can be one of the following:
 4 p
 5 plain
 6 Output a plain-text SQL script file (the default).
 7 c
 8 custom
 9 Output a custom-format archive suitable for input into pg_restore. Together with the directory output format, this is the most flexible output format in that it allows manual selection and reordering of archived items during restore. This format is also compressed by default.
10 d
11 directory
12 Output a directory-format archive suitable for input into pg_restore. This will create a directory with one file for each table and blob being dumped, plus a so-called Table of Contents file describing the dumped objects in a machine-readable format that pg_restore can read. A directory format archive can be manipulated with standard Unix tools; for example, files in an uncompressed archive can be compressed with the gzip tool. This format is compressed by default.
13 t
14 tar
15 Output a tar-format archive suitable for input into pg_restore. The tar format is compatible with the directory format: extracting a tar-format archive produces a valid directory-format archive. However, the tar format does not support compression. Also, when using tar format the relative order of table data items cannot be changed during restore.
16  
17 -C
18 --create
19 Begin the output with a command to create the database itself and reconnect to the created database. (With a script of this form, it doesn't matter which database in the destination installation you connect to before running the script.) If --clean is also specified, the script drops and recreates the target database before reconnecting to it.
20 This option is only meaningful for the plain-text format. For the archive formats, you can specify the option when you call pg_restore.
21 -E encoding
22 --encoding=encoding
23 Create the dump in the specified character set encoding. By default, the dump is created in the database encoding. (Another way to get the same result is to set the PGCLIENTENCODING environment variable to the desired dump encoding.)
24  
25 -O
26 --no-owner
27 Do not output commands to set ownership of objects to match the original database. By default, pg_dump issues ALTER OWNER or SET SESSION AUTHORIZATION statements to set ownership of created database objects. These statements will fail when the script is run unless it is started by a superuser (or the same user that owns all of the objects in the script). To make a script that can be restored by any user, but will give that user ownership of all the objects, specify -O.
28 This option is only meaningful for the plain-text format. For the archive formats, you can specify the option when you call pg_restore.
29  

 

 
舉例:
pg_dump  -Fc dm  -O  > dm.dump
需要先su到有操作postgresql權限的用戶,否則則需加上指定-h IP -U username
將對名為dm的database 以自定義的的方式並且忽略掉原數據庫的owner進行dump。
pg_dump默認dump文件到當前user的的home目錄下
pg_dump內部使用的copy命令,速度還比較快,幾個G的數據20多分鍾就能dump完
 
然后將dm.dump文件用FileZilla Client拷貝到target服務器上
因為在目標服務器上沒有有操作postgresql權限的用戶,所以需加上指定-h IP -U username
pg_restore -O -h IP -U username -d dm dm.dump
這樣就能將數據庫遷移到目標服務器上了,這里目標服務器已經有了dm數據庫,若沒有需要加上-C選項創建數據庫。
-O的作用就是能將restore到目標服務上數據庫的表的owner更改成目標服務上數據庫的owner。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM