Easiest way to copy a table from one database to another?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The easiest way to copy a table depends on where the source and target databases live. If both databases are on the same server and the same database engine, a single CREATE TABLE AS SELECT or INSERT ... SELECT is usually enough. If they are on different servers or different engines, a dump-and-load or ETL approach is usually simpler and safer.
Same Server, Same Engine
If both databases are reachable in one SQL session, the job is straightforward.
To copy structure and data in one step, many engines support a form of:
This is easy, but there is a catch: depending on the database engine, indexes, constraints, defaults, triggers, and foreign keys may not come across the way you expect.
If the target table already exists and you only want the rows, use:
That is usually the simplest answer when the schemas already match.
Copy Schema First When Metadata Matters
For production work, copying just the data is often not enough. You may need:
- primary keys
- indexes
- constraints
- generated columns
- triggers
In that case, create the table structure explicitly, then copy the rows:
This is more work up front, but it avoids the false confidence that comes from a quick copy that silently loses metadata.
Different Servers or Different Engines
If the two databases cannot be queried together directly, the easiest path is usually export and import.
For example:
- '
mysqldumpand reload for MySQL' - '
pg_dumpandpsqlfor PostgreSQL' - CSV export plus import when cross-engine compatibility matters
A CSV workflow is primitive but portable:
Export the result to CSV from the source, then import it into the destination with the engine’s bulk-load tool. It is rarely the most elegant option, but it is often the most universal.
Bulk Copy Tools Are Better for Large Tables
For large data transfers, prefer the engine’s bulk tools over row-by-row application code. Bulk operations are faster, easier to resume, and less likely to produce partial inconsistent results.
If the source table is huge, you may also need to copy in batches:
Then continue with the next range. This is especially useful when the table is live and you want to control transaction size.
Watch for Identity Columns and Constraints
A table copy is often blocked or corrupted by details such as:
- identity or auto-increment columns
- foreign key dependencies
- collation differences
- incompatible data types
- existing rows in the target
That is why “easiest” depends on the operational goal. A throwaway analytics copy is different from a production migration that must preserve integrity and downtime limits.
A Good Default Strategy
A solid practical sequence is:
- copy or define the target schema
- bulk-load the data
- recreate or validate indexes and constraints
- verify row counts and spot-check critical data
That is still simple enough to execute, but safer than assuming one SQL statement finishes the whole migration correctly.
Common Pitfalls
The biggest mistake is assuming CREATE TABLE AS SELECT creates a perfect clone. It usually copies data and basic column structure, not every piece of table metadata.
Another issue is using SELECT * when source and target schemas are not guaranteed to stay identical. Explicit column lists are safer for anything important.
Developers also often forget transaction size and locking impact on large tables. A single giant copy may work in testing and become disruptive in production.
Finally, verify the result. Matching row counts, null expectations, and key integrity should be part of the copy process, not an afterthought.
Summary
- If both databases are on the same server,
INSERT ... SELECTorCREATE TABLE AS SELECTis usually the easiest path. - For production copies, handle schema and metadata deliberately.
- For different servers or engines, dump-and-load or CSV import/export is often simpler.
- Use bulk operations for large tables instead of row-by-row application code.
- Always validate rows, keys, and constraints after the copy.
Related reading
- ECONNREFUSED for Postgres on nodeJS with dockers
- EF Core add-migration Build Failed
- EF LINQ include multiple and nested entities
- Effectively sorting when your data is distributed across different microservices
- Efficiency of Querying 10 Billion Rows (with High Cardinality) in ScyllaDB
- Efficient archiving monthly older than 1 year old for distributed Mongo DB
- Efficient modeling of versioned hierarchies in Cassandra
- Efficient substring Search in DynamoDB

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.