1. 程式人生 > >postgresql資料庫資料匯入匯出

postgresql資料庫資料匯入匯出

from—— 前輩指導

匯出資料庫

1.匯出單個表

從ip為xxx的資料庫monitor中匯出threshold的表結構和資料到本地檔案threshold.sql:
pg_dump -t threshold -h 135.32.94.142 monitor -U monitor -p 5432 -f threshold.sql
引數說明:-t 指定匯出的表名;-h 資料庫地址;-U 資料庫使用者;-p 訪問埠;-f 匯出到指定檔案;

2.匯出所有表和所有資料

例:從ip為xxx的資料庫monitor匯出所有表結構和資料到檔案monitor.sql:
pg_dump -h 135.32.94.142 monitor -U monitor -p 5432 -f monitor.sql

3.僅匯出所有表結構

例:從ip為xxx的資料庫monitor匯出所有的表結構到檔案monitor.sql:
pg_dump -s -h 135.32.94.142 monitor -U monitor -p 5432 -f monitor.sql
引數說明:-s 表示只匯出表結構,不導資料
注:一般資料庫資料量比較大,如果遠端匯出所有的表結構和資料的話會非常慢,所有隻匯出表結構是個明智的選擇。隨後可以再匯出單個重要表結構和資料進來。

匯入資料庫

1.匯入到遠端資料庫

例:從本地檔案threshold.sql匯入表結構和資料到ip為xxx的資料庫monitor:
psql -h 135.32.9.99 -d monitor -U monitor -f threshold.sql
引數說明: -h 資料庫地址;-d 資料庫;-U 指定使用者;-f 要匯入的檔案 注:檔案就是從資料庫匯出的檔案。

2.匯入到本地資料庫
psql -h 0.0.0.0 -d monitor -U monitor -p 5432 -f monitor.sql
引數說明: -p 指定資料庫服務埠,視情況而變,預設是5432埠的可以不加