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

0. 準備工做linux

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

1. 先備份sql

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 小寫指代目標.
相關文章
相關標籤/搜索