[完整版]Postgresql 數據庫 備份以及恢復的過程


0. 准備工作

linux機器上面 必須安裝上pg數據庫
然后 需要將 pg的主程序目錄 放到環境變量里面去  便於執行命令.

1. 先備份

1. 備份目標數據庫:
pg_dump -h 10.24.193.25 -U postgres -p 5432  -F c -f  /TestPG/TestDS0816.dmp TestDS0816
# -h 小寫 指向目標服務器
# -U 大寫 使用的用戶
# -p 小寫 pg 數據庫使用的端口.
# -F 指代導出格式. c 只的就是 自定義的pg_dump格式.
# -f 指代目錄
# 目標數據庫在最后面. 

man的內容為:
       -F format
       --format=format
           Selects the format of the output.  format can be one of the following:

           p
           plain
               Output a plain-text SQL script file (the default).

           c
           custom
               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.

           d
           directory
               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 and also supports
               parallel dumps.

           t
           tar
               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.

2. 創建用戶以及創建數據庫

登錄數據庫

 psql -U postgres

創建用戶以及創建數據庫 注意 這里面有很大的一個坑 創建的時候 不區分大小寫 ,但是因為恢復的區分大小寫,所以必須添加 "" 雙引號括住 數據庫和用戶的名字才可以.
(感謝平台部 劉威 協助..這一塊坑了我半個多小時.)

創建用戶
create role "TestDS0816" superuser login;
創建數據庫
create database "TestDS0816" ;
設置密碼
alter role "TestDS0816" with password 'Test6530';
退出數據庫的命令為:
\q

 

3.執行恢復

pg_restore -U postgres -d TestDS0816 /TestPG/TestDS0816.dmp
# -U 大寫 用戶
# -d 小寫指代目標.

 


免責聲明!

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



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