邏輯備份是最基礎的遷移方式,適用于跨版本、跨平臺或需要靈活調整數據的場景。
pg_dump
導出數據庫(-Fc
格式支持壓縮和并行,提升效率):pg_dump -U postgres -Fc db_name -f db_name.dump
將db_name.dump
傳輸到目標服務器(如用scp
),然后使用pg_restore
導入:pg_restore -U postgres -d new_db_name db_name.dump
pg_dumpall
:pg_dumpall -U postgres -f all.dump
傳輸到目標服務器后,用psql
恢復:psql -U postgres -f all.dump
pg_dump
可兼容舊版本數據庫,建議用新版本工具導出。物理遷移直接復制數據庫文件,速度快,適用于同版本、同架構的遷移(如CentOS系統內升級PostgreSQL版本)。
systemctl stop postgresql
;/var/lib/pgsql/data
,可通過postgresql.conf
確認):cp -rp /var/lib/pgsql/data/* /new/data/directory/
③ 修改權限:確保新目錄屬主為postgres
,權限為700
:chown -R postgres:postgres /new/data/directory/
chmod -R 700 /new/data/directory/
④ 修改配置:編輯/etc/postgresql/[版本]/main/postgresql.conf
,將data_directory
指向新路徑;systemctl start postgresql
。pgloader支持從MySQL、SQLite、Oracle等多種數據庫遷移到PostgreSQL,也適用于PostgreSQL跨版本遷移,具備自動類型轉換、并行處理等功能。
yum install -y pgloader
;migrate.load
):LOAD DATABASE
FROM mysql://user:password@source_host/source_db
INTO postgresql://user:password@target_host/target_db
WITH include no drop, create tables, create indexes, reset sequences
③ 執行遷移:pgloader migrate.load
。CloudCanal等可視化工具支持PostgreSQL與其他數據庫(如Doris、MySQL)的雙向同步,適合需要持續數據同步的場景。
pg_hba.conf
,禁止寫入);psql -f backup.sql
時添加-1
參數),避免部分失敗導致數據不一致。postgres
)對數據目錄有讀寫權限;GRANT
語句是否生效)、表空間路徑(pg_tablespace
表)是否正確。pg_upgrade
(就地升級,停機時間短)或邏輯備份還原(兼容性好);pg_upgrade -c
檢查兼容性問題(如不支持的擴展、數據類型);pg_stat_statements
擴展的變更)。