A multi-tenant platform on SQL Server is usually many databases of one shape: one per customer, often spread over several instances. Moving it to PostgreSQL in a single cutover puts every customer into the same window and the same rollback decision. A phased migration moves one tenant, or one small group, at a time. The old and the new platform run side by side until the last tenant has moved.
This page describes that method: when it fits, what has to exist before the first tenant moves, what the cutover of one tenant looks like, and what two platforms ask of the team in the meantime. It names no duration and no price. Both follow from the estate.
When a phased migration fits
- Tenants are separated in the data. A database, a schema or a tenant key on every table. Rows that several tenants share have to be separated first.
- The application can choose the platform per tenant. Somewhere a request is resolved to a connection. That place has to learn a second answer.
- One application can serve both engines. Either one release that runs against SQL Server and PostgreSQL, or two builds kept in step for as long as the migration runs.
- Customers can take different windows. Each tenant gets its own freeze, its own acceptance and its own way back.
It does not fit a platform with a handful of tenants, where one rehearsed cutover is less work than two platforms, or a platform whose transactions span tenants.
What has to exist before the first tenant moves
One converted schema, under version control
The conversion is done once, not per tenant. A schema conversion tool such as AWS SCT converts tables and attempts the procedural code, and its report of what it could not convert is a first estimate of the manual work. Procedures and functions are rewritten and tested; see T-SQL to PL/pgSQL conversion. Every tenant then gets the same PostgreSQL schema from the same scripts. A difference between tenants on the SQL Server side, such as a column added by hand for one customer, is settled before the conversion and not during a cutover.
A transfer that is the same command for every tenant
A loader such as pgloader reads one SQL Server database and loads PostgreSQL. Scripts cover what no tool does: the order of the steps for one tenant, the state of identity columns and sequences, the reconciliation. The transfer is rehearsed until moving a tenant is the same command with another tenant name. The migration tools guide says what each tool does and does not do.
A routing record
One place has to answer the question "where does this tenant live today". A table is enough:
CREATE TABLE tenant_platform (
tenant_id integer PRIMARY KEY,
tenant_name text NOT NULL,
platform text NOT NULL DEFAULT 'sqlserver'
CHECK (platform IN ('sqlserver', 'postgresql')),
wave integer,
moved_at timestamptz
);
The application, or the layer that hands out connections, reads it. Support, monitoring and the migration scripts read the same row, so nobody keeps a list of their own.
A decision on the layout of the target
On SQL Server a report can join tables of two tenant databases by naming the database in the query. PostgreSQL refuses a query that names another database. That makes the layout of the target a decision of its own:
- A database per tenant keeps backup, restore and isolation per tenant. Every report across tenants needs another path.
- A schema per tenant keeps all tenants inside one database, one backup and one upgrade. The search path of the session decides which tenant an unqualified table name means, so the connection layer has to set it for every session without fail.
The choice is made before the first tenant moves. Changing it later is a second migration.
Operations for two platforms
Monitoring, backups, a tested restore and a person on call have to exist for PostgreSQL before it holds the first production tenant, while SQL Server keeps all of them. The first of the Production Migration Notes is about exactly this: operations and migration are one delivery system.
The cutover of one tenant
- Rehearse with a copy of this tenant. The measured time of the load and the reconciliation is the window to ask for.
- Freeze. The writes of this tenant stop. A weekend is the usual choice when the business of the tenant is closed then. Every other tenant keeps working.
- Load and reconcile. Row counts, checksums on the critical tables and agreed business totals are compared between source and target, as in the migration checklist.
- Switch the routing record. One statement, one row.
- Accept against a fixed list. The SQL Server database of the tenant stays intact and closed to writes until sign-off.
- Close. After the agreed period the SQL Server database is archived and leaves the jobs, the backups and the monitoring.
UPDATE tenant_platform
SET platform = 'postgresql', moved_at = now()
WHERE tenant_id = 42 AND platform = 'sqlserver';
The condition on the old platform makes the switch safe to run twice: the second run changes nothing. Setting the row back is the rollback, and it is that simple only until the tenant has written on PostgreSQL. After that a return has to carry the new writes, which is why rollback is rehearsed, not merely documented.
The same table answers how far the migration is:
SELECT platform, count(*) AS tenants
FROM tenant_platform
GROUP BY platform;
The order of tenants
- First, a small tenant with ordinary usage. It should exercise the common paths. Not the largest customer, and not the one with the most custom work.
- Then waves of tenants that share something. The same modules, the same integrations, the same time zone. What one tenant of a wave teaches applies to the rest.
- The largest and the most customised late, not last. They need the experience of the earlier waves, and the plan needs room after them.
- New tenants start on PostgreSQL. Once the first cutovers have held, a new customer is created on the new platform. The SQL Server side stops growing, and every new tenant is one that never has to move.
Reporting while tenants live on two platforms
Reports across all tenants are the first thing a phased migration breaks: part of the data is in one engine and part in the other. Either the reporting layer reads from both for the duration, or the tenants that have moved feed a reporting database on PostgreSQL. For the second, PostgreSQL has native logical replication: a publication in each tenant database, a subscription in the reporting database.
CREATE PUBLICATION reporting_feed FOR TABLE orders, invoices;
- It runs from PostgreSQL to PostgreSQL. It is not how the data leaves SQL Server. That is the transfer described above.
- Rows arrive in the table of the same name on the reporting side. When every tenant publishes a table called
orders, the reporting table needs the tenant in its key. Otherwise order 1 of two tenants collides and the feed stops. - A published table without a primary key refuses updates and deletes until it is given a replica identity.
- Schema changes are not carried. A new column has to exist on the reporting side before rows that use it arrive.
- Each subscription holds a replication slot on its source. A feed that is switched off and forgotten keeps WAL on the disk of the tenant database.
What two platforms ask of the team
- Every application release is tested on both engines until the last tenant has moved.
- Every schema change is written twice and applied to both sides in step.
- Monitoring, backup, restore and on-call knowledge exist twice.
- A SQL Server instance can be switched off only when its last tenant has left. Waves that empty whole instances end that work earlier than waves that thin every instance a little.
- The middle is slow. A plan without a date and a rule for the last wave ends with a few tenants on the old platform for good.
Questions to ask before approving the plan
- Where is the one record that says which platform serves a tenant, and who may change it?
- Is moving a tenant the same command every time, and how often has it been rehearsed?
- What is compared before the switch, and which difference stops it?
- How does a tenant return after it has written on PostgreSQL?
- Which reports cover all tenants, and where do they read from during the migration?
- Who keeps both platforms running, and until which date?
Questions about a phased migration
Can a multi-tenant SQL Server database be migrated to PostgreSQL one tenant at a time?
Yes, when tenants are separated in the data and the application can choose the platform per tenant. The schema is converted once, each tenant is moved and reconciled in its own window, and a routing record says which platform serves the tenant.
Should each tenant get its own PostgreSQL database or its own schema?
A database per tenant keeps backup, restore and isolation per tenant, and a query cannot name another database, so reports across tenants need a separate path. A schema per tenant keeps all tenants inside one database, one backup and one upgrade. The choice is made before the first tenant moves.
Is PostgreSQL logical replication used to migrate from SQL Server?
No. Native logical replication runs between PostgreSQL servers. In a phased migration it can feed a reporting database from tenants that have already moved. The data leaves SQL Server through a load tool or scripts.
How is one tenant rolled back?
The SQL Server database of the tenant stays intact until sign-off, and the routing record is set back. Once the tenant has written on PostgreSQL, a return also has to carry those writes, and that path is rehearsed before the cutover.
Where this leads
Datrick leads compatibility, conversion, validation, cutover and stabilization under SQL Server to PostgreSQL migration services. When the estate has not been read yet, the migration risk assessment comes first.
Related: SQL Server to PostgreSQL migration · migration risk assessment · SQL Server migration checklist · SQL Server data type mapping · SQL Server migration tools · Production Migration Notes
