Migrating a PostgreSQL database on Debian involves transferring data from a source server to a target server while ensuring data integrity and minimal downtime. Below is a structured guide covering common methods, precautions, and troubleshooting tips.
Install PostgreSQL on Target Server
Ensure PostgreSQL is installed on the target Debian server. Use the following commands to install the latest version from the official repository:
sudo apt update
sudo apt install postgresql postgresql-contrib
Check Version Compatibility
Verify that the PostgreSQL versions on the source and target servers are compatible. Major version upgrades (e.g., 13 → 15) may require additional steps like running pg_upgrade. Minor version differences (e.g., 14.5 → 14.7) are usually safe.
Backup Source Database
Always back up the source database before starting. Use pg_dump for logical backups (recommended for most cases):
sudo -u postgres pg_dump -Fc -b -v -f /path/to/source_backup.dump mydatabase
-Fc: Custom format (supports compression and parallel restores).-b: Include large objects (e.g., binary files).-v: Verbose mode (shows progress).For a full cluster backup (all databases), use pg_dumpall:
sudo -u postgres pg_dumpall -f /path/to/full_backup.sql
pg_dump and pg_restoreThis method is ideal for migrating individual databases or when changing database configurations (e.g., encoding, tablespaces).
Run the pg_dump command on the source server (replace placeholders with actual values):
sudo -u postgres pg_dump -Fc -b -v -f ~/mydatabase_backup.dump mydatabase
Use scp to copy the backup file to the target server:
scp ~/mydatabase_backup.dump user@target_server_ip:/home/user/
sudo -u postgres createdb mydatabase
pg_restore (run as the postgres user):sudo -u postgres pg_restore -d mydatabase -v ~/mydatabase_backup.dump
-d: Target database name.-v: Verbose mode (shows progress).Connect to the target database and run sample queries:
sudo -u postgres psql -d mydatabase -c "SELECT COUNT(*) FROM my_table;"
This method is faster for large databases but requires downtime. It copies the entire PostgreSQL data directory from the source to the target server.
Stop the service to ensure data consistency:
sudo systemctl stop postgresql
The default data directory is /var/lib/postgresql/<version>/main. Copy it to the target server:
sudo rsync -avz /var/lib/postgresql/ user@target_server_ip:/var/lib/postgresql/
Ensure the postgres user owns the data directory:
sudo chown -R postgres:postgres /var/lib/postgresql/
Start the service and verify the migration:
sudo systemctl start postgresql
sudo -u postgres psql -d mydatabase -c "\l"
pgloader for Heterogeneous Migrationspgloader supports migrating data from MySQL, Oracle, CSV, and other sources to PostgreSQL. It automates schema conversion and data loading.
pgloaderOn Debian, install via apt:
sudo apt install pgloader
Define the source and target databases in a .load file (e.g., migrate.load):
LOAD DATABASE
FROM mysql://user:password@source_host/source_db
INTO postgresql://user:password@target_host/target_db
WITH include drop, create tables, create indexes, reset sequences
SET maintenance_work_mem to '128MB',
work_mem to '16MB';
pgloaderExecute the migration:
pgloader migrate.load
postgres user has read/write access to the data directory and backup files.pg_upgrade for major version upgrades (not covered in this guide).scp with -C (compression) or split large backups into smaller chunks.postgis) on the target server after migration.By following this guide, you can successfully migrate PostgreSQL databases on Debian while minimizing downtime and ensuring data integrity. Always test migrations in a staging environment before applying them to production.