Skip to content

Data

Database Migration to PostgreSQL: Step-by-Step Guide

Database migration to PostgreSQL step by step: assess the source, convert schema and code, pick a data move method, test, and cut over with low downtime.

By Innam Dustgir, Founder & CEO ·

Cover reading Database Migration to PostgreSQL: Step-by-Step Guide, InSol Technologies

What a database migration to PostgreSQL involves

A database migration to PostgreSQL moves your schema, data and database-side code from another engine, such as Oracle, SQL Server or MySQL, into PostgreSQL, then points your applications at the new database. Teams usually make the move to reduce licensing costs, standardize on an open-source engine, or adopt managed services like Amazon RDS and Aurora PostgreSQL, Azure Database for PostgreSQL or Google Cloud SQL and AlloyDB.

Copying rows is the easy part. The real work sits in data type differences, stored procedures and triggers, SQL dialect changes inside application code, performance tuning and a cutover plan that keeps downtime inside what the business can accept. This guide covers the database itself. If you are also moving applications to the cloud, our cloud migration strategy guide covers the wider portfolio decisions.

Step 1: Assess the source database

Start with an inventory, not a tool. You need to know what you are moving and who depends on it before you can estimate effort or pick a method.

  • Schemas, tables, indexes, constraints, sequences or identity columns, views and materialized views.
  • Stored procedures, functions, triggers, packages and scheduled jobs, with a rough count of lines of database-side code.
  • Data volume, growth rate, largest tables and any binary or large-object columns.
  • Every application, report, ETL job and integration that reads or writes the database, with an owner for each.
  • Engine-specific features in use, such as Oracle packages and hierarchical queries, SQL Server T-SQL and linked servers, or MySQL-specific SQL modes.
  • Downtime tolerance, recovery targets and compliance rules for where data may live.

Use assessment tooling to size the conversion

Assessment reports give you a first estimate of how much converts automatically. The AWS Schema Conversion Tool produces a migration assessment for Oracle, SQL Server and other sources. For Oracle, the open-source Ora2Pg tool can also estimate migration effort. Treat these reports as a starting point; someone who knows the application still needs to review the complex objects.

Step 2: Convert the schema and data types

Most tables convert cleanly, but data types are where silent bugs start. Map each source type deliberately and check the PostgreSQL data types documentation rather than assuming names line up.

Common type mappings to review when moving to PostgreSQL
Source typeTypical PostgreSQL targetWhat to check
Oracle NUMBERinteger, bigint or numericPrecision and scale; using numeric everywhere can slow queries
Oracle VARCHAR2 / SQL Server NVARCHARvarchar or textLength limits and character encoding (use UTF-8)
Oracle DATEtimestampOracle DATE stores a time part; PostgreSQL date does not
SQL Server DATETIME / DATETIME2timestamp or timestamptzTime zone handling and precision
SQL Server BIT / MySQL TINYINT(1)booleanApplication code that compares with 0 and 1
MySQL AUTO_INCREMENT / SQL Server IDENTITY / Oracle sequencesidentity columns or sequencesReset sequence values after loading data
BLOB / VARBINARY / IMAGEbyteaSize of large objects and how the app streams them

Watch for behavior differences, not just types

PostgreSQL treats empty strings and NULL as different values, while Oracle treats an empty string as NULL. Unquoted identifiers fold to lowercase in PostgreSQL. Default collation and case sensitivity in comparisons can differ from SQL Server and MySQL. Each of these can change query results without throwing an error, so write tests for them.

Step 3: Rewrite database code and application SQL

Stored procedures, packages and triggers usually need the most manual effort. PL/SQL and T-SQL logic is rewritten in PL/pgSQL, or moved into the application layer when that makes it easier to test and maintain. Conversion tools handle simple procedures well; complex ones with dynamic SQL, cursors or engine-specific packages need an engineer.

Application code needs the same attention. Search for vendor-specific SQL such as TOP, ROWNUM, NVL, ISNULL, string concatenation operators, date functions and query hints. ORMs reduce the work but do not remove it, because raw queries and reports tend to hide in corners. This is often the right moment to retire unused objects instead of converting them, which our legacy application modernization guide discusses in more depth.

Step 4: Choose how to move the data

The right method depends on data volume, source engine and how much downtime you can take. The main options are:

Dump and restore (offline)

Export, transform and load the data while the application is stopped. For PostgreSQL-to-PostgreSQL moves, pg_dump and pg_restore are the standard tools. For MySQL or SQL Server sources, pgloader can load data and much of the schema in one pass. This is the simplest option and works well when a maintenance window covers the load time.

Full load plus change data capture (near-zero downtime)

Copy the existing data while the source stays live, then stream ongoing changes until you are ready to switch. AWS Database Migration Service, Google Cloud Database Migration Service and the Azure Database for PostgreSQL migration service all support this pattern for supported sources. Between PostgreSQL databases, native logical replication can do the same job.

Phased migration by module

Large systems sometimes move one bounded domain at a time, with the application reading and writing to both databases for a period. This lowers the risk of any single cutover but adds sync logic and a longer project, so reserve it for systems where a single switch is too risky.

Step 5: Validate data, performance and behavior

Testing is where migrations succeed or fail. Plan at least two full rehearsals in a production-like environment before the real cutover.

  • Data checks: row counts per table, checksums or sampled row comparisons, and totals on key financial or inventory columns.
  • Functional checks: run the application's automated test suite and critical user journeys against PostgreSQL.
  • Performance checks: replay representative queries, compare execution plans with EXPLAIN, add or adjust indexes, and run ANALYZE after loading so the planner has current statistics.
  • Operational checks: backups, restores, monitoring, connection pooling and user permissions all work on the new database.

Automate the comparison

Manual spot checks do not scale past a few tables. Script your validation so every rehearsal produces the same report, and keep it for the final cutover. Our test automation strategy guide covers how to build checks that teams actually maintain.

Step 6: Plan the cutover and rollback

Write the cutover as a runbook with named owners and timings, and rehearse it. A typical sequence:

  1. Freeze schema changes on the source and confirm replication lag is near zero, or start the maintenance window.
  2. Stop or pause writes from applications and background jobs.
  3. Apply the final changes, reset sequences and run the validation scripts.
  4. Switch connection strings or DNS to PostgreSQL and restart application services.
  5. Run smoke tests on critical journeys, then reopen traffic and watch error rates and query latency closely.
  6. Keep the source database read-only and available until you are confident; decide in advance what would trigger a rollback and how long you will keep that option open.

Common database migration mistakes to avoid

The same problems show up across many migrations:

  • Treating the project as a data copy and underestimating stored procedure and application SQL rewrites.
  • Mapping every number to numeric and every date to date without checking precision and time parts.
  • Forgetting to reset sequences, so the first new insert fails with a duplicate key.
  • Skipping performance testing, then finding slow queries in production because indexes or statistics were missing.
  • Missing hidden consumers such as reports, spreadsheets and integrations that still point at the old database.
  • Having no rehearsed rollback plan. Our cloud disaster recovery plan guide covers backup and restore practices that also apply here.

How InSol Technologies helps with PostgreSQL migration

InSol Technologies plans and runs database migrations to PostgreSQL for product and enterprise teams in the US and worldwide. Our data and analytics engineers assess the source, convert schemas and database code, and build the validation scripts. Our cloud and DevOps team sets up managed PostgreSQL, replication and cutover automation on AWS, Azure or Google Cloud. Our enterprise applications and SaaS products teams update application SQL and integrations, and our quality engineering team runs functional and performance testing before go-live.

If you are weighing a move to PostgreSQL or need help with one already underway, contact our team.

Frequently asked questions

How long does a database migration to PostgreSQL take?
It depends on data volume, the amount of stored procedure and application SQL to rewrite, and how many systems connect to the database. A small database with little server-side code can move quickly, while a large Oracle or SQL Server estate with heavy PL/SQL or T-SQL needs a longer project with several rehearsals. An assessment of the source gives you a realistic estimate.
Can you migrate to PostgreSQL without downtime?
You can get close. A full load followed by change data capture, using a managed migration service or logical replication, keeps the source live while data copies. You still need a short window to stop writes, apply final changes and switch connections.
What is the hardest part of migrating from Oracle or SQL Server to PostgreSQL?
Usually database-side code. PL/SQL packages and complex T-SQL procedures often need manual rewriting in PL/pgSQL or moving into the application. Behavior differences, such as how empty strings, NULLs, dates and case sensitivity work, are the next most common source of issues.
Which tools help with a PostgreSQL migration?
Common choices include AWS Schema Conversion Tool and AWS Database Migration Service, Google Cloud Database Migration Service, the Azure Database for PostgreSQL migration service, Ora2Pg for Oracle, pgloader for MySQL and SQL Server, and pg_dump, pg_restore and logical replication for PostgreSQL-to-PostgreSQL moves.

← All insights

Let’s talk

Have a system to build, modernize, or scale?

Tell us where you are and what needs to change. You will hear back from a person with clear, practical next steps.