Database Migration: How to Move Data Without Losing It
Database migration moves data, schema and access rules from one database to another. Here is how to plan it, test it and cut over without losing a row.
On this page
Short answer
Database migration is the planned move of data, schema and access rules from one database system to another, or from one version to a newer one. It covers exporting and mapping the data, converting the schema, moving it in a tested sequence, and switching the application over. Done well, it finishes with no lost rows and only a short pause; done badly, it corrupts data or stops the business.
What is database migration?
Database migration is the planned move of data, schema and access rules from one database system to another, or from one version to a newer one. It is a project, not a single command: it covers what moves, in what order, how it is checked, and what happens if something goes wrong.
There are three common kinds. A homogeneous migration moves data between the same engine, such as one version of Postgres to a newer one. A heterogeneous migration moves between different engines, such as Oracle to Postgres, which needs the schema and the queries converted too. A cloud migration moves a database from a server you run to a managed service.
The work is mostly about the schema and the rules around the data, not the rows themselves. Tables, indexes, constraints, stored procedures and permissions all have to land in the new system in a form that works. A migration that moves every row but breaks a constraint has still failed.
What are the stages of a database migration?
A database migration follows the same stages whether the database is small or large. Skipping a stage does not save time; it moves the failure to cutover day, when it costs far more.
| Stage | What happens | What to check |
|---|---|---|
| Assess | Inventory tables, size, dependencies, users | Nothing hidden is missed |
| Map and convert | Schema, types and queries converted | Types match, queries still run |
| Move a copy | Data copied to a test system | Row counts and checksums match |
| Rehearse | Full run on a copy, timed end to end | Time fits the cutover window |
| Cut over | Application switched to the new database | Writes stop cleanly, then resume |
| Verify and watch | Checks run, old system kept as fallback | No errors, no missing rows |
The rehearsal is the stage teams most often skip, and the one that saves the most trouble. A full timed run on a copy shows the real duration, the real error rate and the real rollback steps, all while the live system is still untouched. The data side of the work is covered in data engineering services.
How do you plan a database migration?
You plan a database migration by inventorying first, then choosing the approach, then rehearsing it. A plan written from the schema alone will miss the dependencies that live in applications and reports.
- Inventory everything. List every table, its size, who reads it, who writes to it, and which applications and reports depend on it.
- Decide what does not move. Old logs, test data and dead tables should be archived or dropped, not carried into the new system.
- Map types and rules. Write down how each data type, default and constraint converts, and where the new engine differs.
- Choose the cutover window. Pick the quietest hours, agree the allowed downtime with the business, and plan the rollback.
- Assign owners. Name one person for the data, one for the application switch, and one who can call the rollback.
- Rehearse the whole run. Time every step on a copy, including the checks, and fix what the rehearsal breaks.
Write the plan down as a runbook with times, commands and checks. On cutover day nobody should be deciding what to do next; they should be following the list. The platform side of the work is covered in cloud services.
How do you test a database migration?
You test a database migration by running it on a copy of the live data and proving the copy matches, row for row. A test on sample data proves the tooling works; only a test on real data proves the migration works.
- Row counts. Count rows per table before and after. A mismatch means something was dropped or duplicated.
- Checksums. Hash the contents of key tables and compare the hashes, which catches changes a count would miss.
- Spot checks. Compare a sample of real records field by field, especially dates, money and codes, where type conversions go wrong.
- Application tests. Run the real application against the copy: log in, place an order, run a report, and check the results.
- Rollback drill. Practise switching back to the old database, because the day you need it is not the day to learn it.
Keep the test data realistic. A copy made from last month's backup is far more useful than a synthetic set, because it contains the odd rows, the legacy codes and the bad dates that break conversions. The operations side is covered in DevOps services.
Example (hypothetical): moving a shop database
Example (hypothetical): an online shop moves its database of 2,000,000 rows from a self-managed server to a managed service. The team rehearses the move twice on a copy before cutover day.
The timed rehearsal takes 30 minutes to export, 45 minutes to transform types and clean bad dates, and 60 minutes to load, or 135 minutes in total, which is 2 hours and 15 minutes. The team books a 3-hour cutover window to leave room for checks.
The rehearsal also finds 1,200 rows with dates in an old format, or 0.06 percent of the total. The team fixes the conversion rule and re-runs the test until the checksums match. On cutover day the move finishes in 2 hours and 20 minutes, the row counts match, and the old database stays online for a week as a fallback.
Big bang or phased: which approach fits?
A big bang migration moves everything at once during a single cutover window. It is simpler to run, because there is only one switch and no period where two systems must stay in sync. It fits small databases and short windows, where the whole move takes an hour or two.
A phased migration moves one table or one service at a time, with both systems running side by side for a while. It is slower and needs syncing between the two, but each step is small and a failure only affects one part. It fits large databases and systems that cannot stop.
A third option is replication: the new database follows the old one continuously, and the switch is a short flip when the two match. It needs the most setup, but it gives the shortest downtime. Choose by downtime budget, not by preference: if the business allows a 3-hour window, big bang is fine; if it allows 15 minutes, you need replication or phases.
What are the common mistakes?
The most common database migration mistakes are skipping the rehearsal, trusting row counts alone, and having no tested rollback.
- No full rehearsal. A plan that was never run end to end will fail on the step nobody timed.
- Only counting rows. Counts miss changed values, so always compare checksums and spot-check real records.
- Forgetting permissions. Users, roles and connection rules are part of the database, and an application that cannot log in is down.
- Migrating dead data. Carrying old logs and test rows into the new system wastes time and hides real problems.
- No rollback drill. A fallback you have never practised is a hope, not a plan.
Key takeaways
- Migration covers data, schema and access rules, not just rows
- A rehearsal on a copy finds the problems a plan cannot
- Row counts and checksums prove nothing was lost
- Phased moves reduce risk but take longer to finish
- A tested rollback plan is part of the migration, not an extra
Frequently asked questions
How long does a database migration take?
It depends on size, complexity and approach. A small database can move in an hour or two, while a large one with many dependencies can take weeks of preparation and a cutover window of several hours. The preparation and rehearsal usually take far longer than the move itself, so budget for both.
Can you migrate a database with no downtime?
Near-zero downtime is possible with replication, where the new database follows the old one and the switch is a short flip. It needs more setup and careful syncing, and it still needs a brief pause for writes. True zero downtime is rare, so agree the allowed pause with the business first.
What is the difference between database migration and data migration?
Data migration moves records from one place to another, while database migration moves the whole database: schema, indexes, constraints, procedures and permissions as well as the rows. Database migration is the larger job, and it includes data migration as one of its stages.
How do you know no data was lost?
Compare row counts per table, then compare checksums of the table contents, then spot-check real records field by field. Counts alone are not enough, because they miss changed values. Run the application against the copy too, so a broken query or a missing permission shows up before cutover.
Should you migrate to the cloud at the same time?
Only if the team can handle both changes at once. Moving the engine and the platform together doubles the number of new things that can fail. Many teams move the database first on the same platform, then move the platform later, so each change can be tested on its own.
What happens if the migration fails halfway?
Stop, check what moved and what did not, and switch back to the old database using the rollback plan. That is why the old system stays online and why the rollback is practised before cutover day. Fix the cause on a copy, re-run the rehearsal, and only then book a new window.