邏輯備份
和psql同樣,pg_dump、pg_restore有基本的和數據庫鏈接的參數sql
-h 目標地址(對應環境變量$PGHOST)shell
-p 鏈接端口(對應環境變量$PGPORT)數據庫
-U 鏈接使用的用戶(對應環境變量$PGUSER)bash
-d 鏈接使用的庫,可是pg_dump 省略了-d,能夠直接跟庫名(對應環境變量$PGDATABASE)post
已設環境環境變量spa
$PGHOST=127.0.0.1rest
$USER=chenblog
$PGDATABASE=yunclass
備份,yun庫,備份爲custom自定義格式的歸檔文件,壓縮等級9,在shell下執行變量
pg_dump yun -Fc -Z 9 >yun1.dump
在psql中建立一個以template0爲模版的庫,名爲yun2,0號模版比較乾淨,另:老是把template 和complete 搞混,前者是模版的意思,後者是完整。。
create database yun2 template template0
還原,在shell下運行pg_restore,把yun庫恢復到yun2庫中,這就是還原到不一樣名庫中的方法
pg_restore -Upostgres -d yun2 yun1.dump
先新建庫的還原,-C自動新建與備份時同名的庫,並還原到其中,-d 只是暫時先創建鏈接用
pg_restore -Upostgres -C -d postgres yun1.dump
就是還原後用\l 列出所有數據庫時看不到存取權限了,有待繼續實驗