This website use cookies to help you have a superior and more admissible browsing experience on the website.
Loading...
In daily database operation and maintenance, copying a PostgreSQL database to another server is a critical high-frequency task—whether for migrating to a high-performance server, deploying a production-like environment across infrastructure, or building a redundant backup server. Mastering reliable PostgreSQL copy database to another server methods is essential for smooth DevOps and data management workflows. This article will walk you through the core prerequisites, two proven cross-server copying methods (with reusable code examples for every step), and also cover a quick same-server copy workflow as a foundational prep step—all optimized for seamless migration to a remote server.
Copying a PostgreSQL database across servers is a foundational operation for modern data infrastructure, with three key business and technical use cases:
Ensure these requirements are met to avoid copy/migration failures:
While the core goal is copying to another server, a same-server database copy is often a necessary prep step—e.g., creating a clean snapshot of the production database on the source server (to avoid copying live, changing data) before migrating it remotely. Use the CREATE DATABASE…WITH TEMPLATE command for fast local cloning (the only same-server method you’ll need).
Step 1: Terminate Active Connections to the Source Database
Local cloning will fail if the source database has active user/ application connections—first query and end all open sessions:
bash
psql -U postgres
SQL
SELECT pid, usename, client_addr
FROM pg_stat_activity
WHERE datname = 'dvdrental';
SQL
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'dvdrental';
Step 2: Create a Local Snapshot Copy
Clone the source database to a local snapshot (this clean copy is what you’ll migrate to the remote server):
SQL
CREATE DATABASE dvdrental_snapshot WITH TEMPLATE dvdrental;
This command creates an exact replica of the source database. The runtime depends on the database size—no additional tools are needed for this native PostgreSQL operation.
Step 3: Verify the Local Snapshot
Confirm the snapshot is created successfully (to ensure a valid copy for remote migration):
SQL
\l
You’ll see the snapshot database (e.g., dvdrental_snapshot) in the PostgreSQL database list—this is your stable source for copying to another server.
These are the two industry-standard methods for copying a PostgreSQL database to a remote target server—each optimized for different network and data volume scenarios, with step-by-step code examples for direct implementation.
This is the most stable and widely used method for cross-server PostgreSQL copying, ideal for large databases (10GB+) or low-speed network connections between servers. It involves exporting the database to a portable SQL dump file on the source server, transferring the file to the target server via SSH, and restoring the file to a new database on the remote server. No real-time network connection is required during restoration—making it immune to network drops.
Step 1: Export the Database Snapshot to a SQL Dump File (Source Server)
On the source server’s command line (not the PostgreSQL terminal), use pg_dump (PostgreSQL’s native backup tool) to export your local snapshot (or direct source database) to a SQL file. Replace dvdrental_snapshot with your database name:
bash
pg_dump -U postgres -d dvdrental_snapshot -f dvdrental_snapshot.sql
Step 2: Transfer the Dump File to the Target Server (SSH/SCP)
Use scp (secure copy protocol) to transfer the SQL dump file from the source server to the target server—this is the most secure way to transfer database files over the network. Run this command on the source server:
bash
scp dvdrental_snapshot.sql postgres@target_server_ip:/home/postgres/
Log in to the PostgreSQL terminal on the target server and create a blank database to hold the restored data—this database name can match your source database for consistency:
bash
psql -U postgres
SQL
CREATE DATABASE dvdrental;
Step 4: Restore the Dump File to the New Database (Target Server)
On the target server’s command line (exit the PostgreSQL terminal first), use psql to restore the SQL dump file to the empty database you just created. Run this command:
bash
psql -U postgres -d dvdrental -f /home/postgres/dvdrental_snapshot.sql
The restore process will create all tables, indexes, and data exactly as they exist in the source server’s snapshot.
For small to medium databases (under 10GB) and high-speed, stable network connections between servers (e.g., same cloud VPC, dedicated private network), use a one-line pipe command to copy the PostgreSQL database directly to another server—no intermediate SQL dump file required. This method is faster and more streamlined, as it pipes the output of pg_dump (source server) directly to psql (target server) in real time.
Run the One-Line Cross-Server Copy Command:
Execute this command on the source server (or your local machine with access to both servers)—this single command handles export, transfer, and restoration all at once:
bash
pg_dump -C -h source_server_ip -U postgres dvdrental_snapshot | psql -h target_server_ip -U postgres dvdrental
-C: Automatically creates the target database on the remote server—no need to run CREATE DATABASEmanually (eliminates a step on the target server).
Post-Copy Verification: Ensure Data Integrity on the Target Server
After copying the database to another server, verify data consistency between the source and target servers—this step is non-negotiable to avoid missing data or corrupted tables in the remote database. Run these checks on the target server:
bash
psql -U postgres -d dvdrental
Sql
-- List all tables (confirm matching table count with source) \dt
-- Check total rows in a core table (replace with your key table) SELECT COUNT(*) FROM customer;
Compare the table count and row numbers with the source server—they must match exactly.
Query a sample of data from a critical table to confirm values are identical to the source:
Sql
SELECT * FROM payment LIMIT 10;
While the native pg_dump/pipe methods work for basic cross-server copying, they have clear limitations: manual command-line operations are error-prone (especially for non-technical users), real-time incremental replication is not supported (you can only copy a static snapshot), and there’s no centralized monitoring or visual management. For enterprise environments—where zero downtime, real-time sync, and ease of management are critical—Information2 Software’s i2Stream is a game-changing solution that addresses these pain points and offers unique advantages over native methods.
If you’d like to learn more information about PostgreSQL copy all database to another Server, please contact technical support to request a demo.
PostgreSQL copy database from one server to another is key for migration, testing, and disaster recovery. Choose native methods—dump-and-restore for large databases/slow networks, or direct pipe transfer for small data/fast networks—for basic, cost-free needs. For enterprise scenarios requiring real-time sync, zero production impact, and easy management, i2Stream is the superior choice.