Jane Doe
Pro Plan
Cloning a remote PostgreSQL database (or selected tables) to another remote database is a common task for migrations, backups, or syncing environments. This guide walks you through the process using pg_dump and psql, with practical examples for copying specific tables and restoring them to a target database.
Use pg_dump to export the desired tables from your source (dev/staging) database. Replace [DB-PASSWORD], [IP-ORIGINAL], and table names as needed.
PGPASSWORD=[DB-PASSWORD] pg_dump -h [IP-ORIGINAL] -U postgres \ -t cerberus.chart_premiums_paid \ -t cerberus.chart_premiums_paid_aggregate \ -t cerberus.chart_iv_term \ -t cerberus.chart_iv_heatmap \ -t cerberus.historical_combined_metrics \ -t cerberus.historical_volume_pcr_metrics \ -t cerberus.historical_oi_pcr_metrics \ -t cerberus.chart_iv_smile \ --data-only --no-owner --no-privileges \ -f /tmp/db_dump.sqlBefore restoring, clear out the target tables to avoid duplicate data. This example uses psql to connect to the destination (production) database and truncate the tables.
PGPASSWORD=[DB-PASSWORD] psql -h [IP-CLONE] -U postgres -d postgres <<EOFTRUNCATE TABLE cerberus.chart_premiums_paid, cerberus.chart_premiums_paid_aggregate, cerberus.chart_iv_term, cerberus.chart_iv_heatmap, cerberus.historical_combined_metrics, cerberus.historical_volume_pcr_metrics, cerberus.historical_oi_pcr_metrics, cerberus.chart_iv_smileRESTART IDENTITY CASCADE;EOFFinally, use psql to restore the dumped data to the destination database.
PGPASSWORD=[DB-PASSWORD] psql -h [IP-CLONE] -U postgres -d postgres -f /tmp/db_dump.sql--data-only to copy just the data, not the schema.-t flags and dump the entire database.With these steps, you can efficiently clone tables or data from one remote PostgreSQL database to another, supporting migrations, environment syncs, and disaster recovery workflows.