對于postgresql資料庫的備份按照官方手冊的方法之一就是采用“sql dump”的方式(另一種方式是直接備份檔案系統中的檔案,可參考官方手冊)。
基本用法如下:
pg_dump dbname > outfile
首先,正如指令行所展示的,pg_dump是将指令結果輸出到标準輸出中。
其次,pg_dump并不影響資料庫工作過程中的其他操作(主要是關心pg_dump會不會産生讀寫鎖(read lock、write lock)),但也有例外,那就是哪些需要使用互斥鎖(exclusive lock)的操作,如alter table。
由于對于運維而言,通常需要使用腳本來執行資料庫備份,而不是每天手動執行指令行并輸入密碼備份,是以特地查了一下文檔,根據文檔“31.15. the password file”節的說明,可以在使用者目錄下建一個配置檔案,提前将密碼寫入這個配置檔案中,配置檔案的格式如下:
hostname:port:database:username:password
需要将此檔案放在執行pg_dump指令的使用者目錄下,儲存成.pgpass 檔案,并且權限為600,否則postgresql就會報
warning: password file "/root/.pgpass" has group or world access; permissions should be u=rw (0600) or less
注意:如果postgresql資料庫的資料庫表結構(database schema)依賴oids(例如外鍵),則pg_dump需要加-o選項。
postgresql資料庫的導出速度還是比較快的,導出3萬多行不到1s。
postgresql導出資料庫指令行執行個體:
1
<code>pg_dump -u confluence -d confluence -h 127.0.0.1 -o ></code><code>/tmp/tmp_confluence_postgresql</code><code>.sql</code>
其中,
-u表示執行使用者
-d表示資料庫
-h表示主機
-o表示支援olds
注:如果不想使用.pgpass 檔案,則可以在指令行中添加-w選項,意思是輸入密碼再執行。
附錄:postgresql資料庫一些适合運維的基本操作
登入postgresql資料庫:
<code>psql -u dbuser -d exampledb -h 127.0.0.1 -p 5432</code>
如果也不想輸入密碼,則可以執行:
<code>psql </code><code>"host=127.0.0.1 hostaddr=127.0.0.1 port=5432 user=yourloginname password=yoursecret"</code>
列出資料庫:
\l
退出資料庫console:
\q
tag:postgresql資料庫備份,postgresql指令行不輸入密碼,postgresql資料庫基本操作,postgresql資料庫導出,postgresql運維教程
--end--