在Linux上遷移PostgreSQL數據庫可以通過多種方法實現,以下是一些常見的方法:
pg_dump
和pg_restore
備份源數據庫
使用pg_dump
命令備份源數據庫到一個文件中。
pg_dump -U username -d source_db_name -f backup_file.sql
將備份文件傳輸到目標服務器
使用scp
或其他文件傳輸工具將備份文件傳輸到目標服務器。
scp backup_file.sql user@target_server:/path/to/destination
在目標服務器上恢復數據庫
在目標服務器上使用pg_restore
命令恢復數據庫。
pg_restore -U username -d target_db_name /path/to/destination/backup_file.sql
pg_dumpall
和pg_restoreall
如果需要遷移整個PostgreSQL集群(包括所有數據庫),可以使用pg_dumpall
和pg_restoreall
。
備份所有數據庫
使用pg_dumpall
命令備份所有數據庫到一個文件中。
pg_dumpall -U username -f backup_all.sql
將備份文件傳輸到目標服務器
使用scp
或其他文件傳輸工具將備份文件傳輸到目標服務器。
scp backup_all.sql user@target_server:/path/to/destination
在目標服務器上恢復所有數據庫
在目標服務器上使用psql
命令恢復所有數據庫。
psql -U username -f /path/to/destination/backup_all.sql
pg_basebackup
如果需要遷移整個PostgreSQL數據目錄(包括數據文件),可以使用pg_basebackup
。
在源服務器上創建基礎備份
使用pg_basebackup
命令創建基礎備份。
pg_basebackup -U username -D /path/to/backup -F t -z -P
將備份文件傳輸到目標服務器
使用scp
或其他文件傳輸工具將備份文件傳輸到目標服務器。
scp -r /path/to/backup user@target_server:/path/to/destination
在目標服務器上恢復數據目錄 在目標服務器上停止PostgreSQL服務,將備份的數據目錄替換到目標位置,然后啟動PostgreSQL服務。
sudo systemctl stop postgresql
sudo rsync -a /path/to/destination/postgresql/ /var/lib/postgresql/
sudo systemctl start postgresql
通過以上方法,你可以在Linux上成功遷移PostgreSQL數據庫。選擇適合你需求的方法進行操作。