Migrating PostgreSQL database with pg_dump

Dump and compress DB on the source server:

pg_dump --username=DB_USER --format=custom --compress=9  --file=DB_NAME.dump  DB_NAME

(To generate dump file name dynamically, use --file=$(date +%Y%m%d)_DB_NAME.dump. Could be helpful for backup scenarios.)

Download the dump to the target DB server:

scp -i ~/.ssh/PRIVATE_KEY USER@HOST:DB_NAME.dump ~/

Restore the dump on the new server:

pg_restore --username=DB_USER --create --dbname=DB_NAME DB_NAME.dump