2012年10月26日 星期五

PostgreSQL 9.2.1 Windows Edition - 以指令進行資料庫備份、回存 (PostgreSQL Database Backup & Restore)

本文將介紹如何以指令進行 PostgreSQL Windows Edition 資料庫備份、回存作業。


1) 進入 pdAdmin III

2) 建立測試資料庫:test_db

3) 於測試資料庫 (test_db) 中,建置測試資料表。
create table family (first_name text, last_name text);

4) 建立測試資料
insert into family (first_name, last_name) values('Tom',     'Smith');
insert into family (first_name, last_name) values('Jackson', 'Smith');
insert into family (first_name, last_name) values('Mary',    'Smith');

5) 建立回存資料庫:test_restore

6) 將 PostgreSQL 的安裝路徑,加入系統變數 PATH 中。
我的電腦 --> 內容 --> 進階 --> 環境變數 --> 系統變數,將字串 "C:\Program Files\PostgreSQL\9.2\bin;" 加入 Path 變數中。

7) 備份檔產生於 C:\
於 DOS 模式中,執行指令:
pg_dump -U postgres TEST_DB > c:\TEST_DB.dump

8) 將上述備份檔回存至 test_restore 資料庫
於 DOS 模式中,執行指令:
type C:\TEST_DB.dump | psql --username="postgres" test_restore

9) 檢查執行結果
進入 pdAdmin III,並於資料庫 test_restore 中,執行指令:
select * from family