Moving from Microsoft SQL Server to PostgreSQL

Plenty of small companies in Denmark still run a business system on Microsoft SQL Server because that was the default when it was built. The question comes up when the licence is due or the server has to move: can we switch to PostgreSQL? This page gives the licence prices from Microsoft’s own price sheet,…

Last checked:

Step by stepThe load file we ran
  1. A SYSUTCDATETIME() default
  2. "Illegal UTF-8 character"
  3. Without the uniqueidentifier transform
  4. Amounts lost their decimals
  5. Time zones

Why smaller businesses switch

Usually it is the licence. PostgreSQL costs nothing to use, in production too. Microsoft lists SQL Server Standard at $3,945 per 2-core pack and Enterprise at $15,123 per 2-core pack. If you would rather pay as you go, Standard is $1,418 a year per 2 cores as a subscription, or $73 per core per month billed through Azure Arc. These are Microsoft’s estimated retail prices in dollars, excluding VAT; your reseller sets the final price.

The second reason is that new projects tend to start on PostgreSQL now. Ours do as well – new systems go on PostgreSQL, while MySQL and MariaDB stay where the data already lives. We have deliberately left more than 130 million rows in MySQL because moving them would not make them better. A migration should pay for itself; otherwise it is only risk.

The honest counter-argument: SQL Server Express is free. From SQL Server 2025 an Express database may grow to 50 GB, but the engine uses at most 4 cores and 1,410 MB of buffer pool. If your database is small and the system is a packaged product whose vendor supports only SQL Server, Express is often the cheapest answer. You don’t migrate – you change edition.

Licence cost for a small server

The formula is list price × number of packs × 6.80. We assume 4 cores for core licensing and 10 users for server + CAL. Put in your own numbers, and check the licensing guide’s minimum-core rules before you buy.

ModelCalculationIn DKK
Standard, server + 10 CALs (one-off)$989 + 10 × $230 = $3,289about 22,365
Standard, 4 cores (one-off)2 × $3,945 = $7,890about 53,652
Standard, 4 cores, subscription2 × $1,418 = $2,836 a yearabout 19,285 a year
Standard, 4 cores, pay as you go (Azure Arc)4 × $73 = $292 a monthabout 1,986 a month
Enterprise, 4 cores (one-off)2 × $15,123 = $30,246about 205,673
PostgreSQL on your own Hetzner server (CPX22)€16.99 a month, licence 0about 127 a month
Microsoft’s estimated list prices converted at DKK 6.80 per dollar (ECB, 20.11.2026), excluding VAT. Server OS and reseller margin not included.

The licence is only half the sum. The migration itself costs hours, and those hours decide whether it pays off. Three tables and no procedures can move in a day; a system with a hundred stored procedures can take weeks. The Hetzner row is a server with 2 vCPU and 4 GB RAM at €16.99 a month excluding VAT, checked 20.11.2026 – and you run it yourselves.

The types you have to translate

Most types have a direct counterpart. The trouble comes from small differences: amounts that lose their decimals, timestamps that move by an hour, and lookups that suddenly care about upper and lower case.

SQL Server → PostgreSQL type mapping

SQL ServerPostgreSQLWhat we saw and did
INT IDENTITY(1,1)bigserial or integer GENERATED … AS IDENTITYpgloader created bigserial and set the sequence correctly
BITbooleanWorked as is
TINYINTsmallintSQL Server allows 0–255, so smallint is required
MONEYnumeric(19,2) or numeric(19,4)Became numeric without scale; we set numeric(19,2)
NVARCHAR(n), VARCHAR(n)text or varchar(n)Became text; Danish letters, Cyrillic and emoji survived
DATETIME, DATETIME2timestamptz or timestampRead in the session time zone – see below
DATETIMEOFFSETtimestamptzCarries its own offset, the easy one
UNIQUEIDENTIFIERuuidNeeds a transform; values matched on both sides
VARBINARY(MAX)bytea–
Computed column AS (…) PERSISTEDGENERATED ALWAYS AS (…) STOREDBecame a plain column; we recreated it
DEFAULT NEWID() / SYSUTCDATETIME()gen_random_uuid() / now()SYSUTCDATETIME() stopped the whole run
Collation …_CI_AScase-sensitiveemail = 'SOEREN@…' found 1 row in SQL Server, 0 in PostgreSQL
Column 3 is what pgloader 3.6.10 did in our test on 20.11.2026 and what we changed afterwards.

pgloader’s documented defaults turn bit into boolean, money into numeric, text types into text and uniqueidentifier into uuid. AWS DMS has its own type table and, among other things, does not carry over a column’s identity property.

Three tools for three different jobs

pgloader, AWS DMS or Babelfish?

CriterionpgloaderAWS DMSBabelfish for PostgreSQL
What it doesCopies schema and data in one goCopies data and can keep it in sync (CDC)Lets PostgreSQL speak SQL Server’s wire protocol and T-SQL
PriceFree, open sourcePer hour for a replication instance, or per capacity on serverless; schema conversion is freeOpen source; on AWS as part of Aurora PostgreSQL
DowntimeWrites must stop during the copyCan be kept short with ongoing replicationThe app can keep much of its SQL
Procedures and triggersNot migratedNot part of the data copyRun as T-SQL “with some differences”
RequirementsNetwork access to both databasesFull or Bulk-logged recovery model plus MS-CDC or MS-Replication for CDCAurora PostgreSQL or your own build of the project
FitsSmall and mid-size databases with a maintenance windowLarge databases that cannot stopAn application whose code you cannot change
Described from each tool’s own documentation, checked 20.11.2026. Only pgloader was run by us.

pgloader is what we use for MySQL migrations, so it is what we tested here. Its documentation now describes a version 4 with Microsoft’s JDBC driver, but the Debian package is 3.6.10 with FreeTDS, and that is what most people get from apt install pgloader.

AWS DMS is built for large databases and short downtime. Ongoing replication needs full backups and the Full or Bulk-logged recovery model, and the limitations list is long: sparse columns, temporal tables and changes to computed columns are among the things it does not handle. You pay per hour for the instance doing the copy, plus storage.

Babelfish takes a different route: the data moves, but the application keeps talking T-SQL on port 1433 while PostgreSQL clients use 5432. AWS itself says T-SQL runs “with some differences”. It suits an old application without source code, but it ties you to Aurora or to building and updating the project yourselves.

The load file we ran

The test ran in throwaway Docker containers on our own test machine: SQL Server 2022 Developer, PostgreSQL 18.6 and pgloader 3.6.10. The source database had Danish and Ukrainian names, amounts in MONEY, a computed VAT column, a foreign key, a view and a stored procedure. The containers were deleted afterwards.

-- shop.load  –  run with:  pgloader shop.load
-- The password comes from the environment: export MIGRATE_PASSWORD=...
LOAD DATABASE
     FROM mssql://migrate:{{MIGRATE_PASSWORD}}@sqlserver:1433/butik
     INTO postgresql://migrate:{{MIGRATE_PASSWORD}}@localhost:5432/butik

 WITH include drop, create tables, create indexes, foreign keys,
      reset sequences, workers = 2, concurrency = 1

 SET timezone to 'UTC'

 CAST type datetime2 to timestamptz drop default,
      type money to numeric drop typemod using float-to-string,
      type uniqueidentifier to uuid drop default using sql-server-uniqueidentifier-to-uuid

 INCLUDING ONLY TABLE NAMES LIKE '%' IN SCHEMA 'dbo'

 ALTER SCHEMA 'dbo' RENAME TO 'public'

 AFTER LOAD DO
   $$ ALTER TABLE kunde ALTER COLUMN oprettet SET DEFAULT now(); $$,
   $$ ALTER TABLE kunde ALTER COLUMN eksternid SET DEFAULT gen_random_uuid(); $$,
   $$ ALTER TABLE ordre ALTER COLUMN beloeb TYPE numeric(19,2); $$,
   $$ ALTER TABLE ordre DROP COLUMN moms,
        ADD COLUMN moms numeric(19,2) GENERATED ALWAYS AS (beloeb * 0.25) STORED; $$;

pgloader 3.6.10 talks to SQL Server through FreeTDS, and without this file in the home directory the run crashed on Danish characters:

# ~/.freetds.conf
[global]
    tds version = 7.4
    client charset = UTF-8

What we ran into, in the order it happened:

  1. A SYSUTCDATETIME() default stopped the whole run because pgloader wrote it as a string literal into CREATE TABLE. Fix: drop default in the CAST rule and set the default again after the load.
  2. “Illegal UTF-8 character” followed by a crash. Fix: client charset = UTF-8 for FreeTDS.
  3. Without the uniqueidentifier transform pgloader tried to write raw bytes into a uuid column. With sql-server-uniqueidentifier-to-uuid the values matched SQL Server exactly.
  4. Amounts lost their decimals (1995.0) and the VAT column became a plain column. Both fixed in AFTER LOAD.
  5. Time zones. With SET timezone to 'Europe/Copenhagen' both time columns moved by an hour – including the one stored in UTC with SYSUTCDATETIME(). Work out which columns hold local time (usually GETDATE()), load in UTC and convert only those.
-- Only columns written with GETDATE() in SQL Server (Danish local time)
UPDATE ordre
SET bestilt = (bestilt AT TIME ZONE 'UTC') AT TIME ZONE 'Europe/Copenhagen';

Finally we counted rows on both sides: 3 customers and 5 orders in each, and the sequence carried on at 1005. The view and the procedure did not come across, which is expected – you rewrite those yourselves. The row-count query is on our MySQL migration page. One more trap: our source script first failed in sqlcmd on the table with the computed column, because sqlcmd runs with QUOTED_IDENTIFIER off unless you pass -I.

T-SQL to PL/pgSQL: one worked example

The procedure in the source database creates an order, raises an error when the customer is unknown and returns the new order:

-- SQL Server (T-SQL)
CREATE PROCEDURE dbo.OpretOrdre @KundeId INT, @Beloeb MONEY, @OrdreId INT OUTPUT
AS
BEGIN
  SET NOCOUNT ON;
  IF NOT EXISTS (SELECT 1 FROM dbo.Kunde WHERE KundeId = @KundeId AND Aktiv = 1)
    THROW 50001, 'Kunden findes ikke eller er inaktiv', 1;
  INSERT INTO dbo.Ordre (KundeId, Beloeb) VALUES (@KundeId, @Beloeb);
  SET @OrdreId = SCOPE_IDENTITY();
  SELECT TOP (1) OrdreId, Beloeb, Moms FROM dbo.Ordre WHERE OrdreId = @OrdreId;
END

The same logic in PostgreSQL. SCOPE_IDENTITY() becomes RETURNING, THROW becomes RAISE EXCEPTION, and the result comes back as a table from a function:

-- PostgreSQL (PL/pgSQL)
CREATE OR REPLACE FUNCTION opret_ordre(p_kunde_id integer, p_beloeb numeric)
RETURNS TABLE (ordre_id bigint, beloeb numeric, moms numeric)
LANGUAGE plpgsql AS $$
DECLARE
  v_id bigint;
BEGIN
  IF NOT EXISTS (SELECT 1 FROM kunde k WHERE k.kundeid = p_kunde_id AND k.aktiv) THEN
    RAISE EXCEPTION 'Kunden findes ikke eller er inaktiv' USING ERRCODE = 'P0001';
  END IF;

  INSERT INTO ordre (kundeid, beloeb) VALUES (p_kunde_id, p_beloeb)
  RETURNING ordre.ordreid INTO v_id;          -- replaces SCOPE_IDENTITY()

  RETURN QUERY
    SELECT o.ordreid, o.beloeb, o.moms FROM ordre o WHERE o.ordreid = v_id;
END;
$$;

SELECT * FROM opret_ordre(2, 500);   -- new order, VAT computed by the database

It ran on PostgreSQL 18.6 and matched the SQL Server procedure: a new order of 500.00 with VAT 125.00, and an error for an unknown customer. The application now calls SELECT * FROM opret_ordre(…) instead of EXEC, so the app code changes too. If a migration tool such as Alembic compares schemas on top of the database, learn its traps before you let it near a shared database.

Test checklist before the switch

Tests before leaving SQL Server

AreaTestGreen when
DataRow count per table on both sidesEqual in every table
MoneySum of amount columns per tableEqual to the øre
Time5 rows with known timestamps comparedNo one-hour shift
TextNames with æ, ø, å and other alphabetsIdentical text, emoji included
SearchLogin and lookups typed in capitalsSame result as before (citext or a lower() index)
KeysInsert a new row in every tableNo duplicate from the sequence
CodeAll procedures, triggers and views rewrittenTest suite green against PostgreSQL, not another engine
OperationsBackup and restore test of the new databaseFirst nightly copy restored
Way backOld database kept read-onlyAt least 14 days after the switch
What we check. One item that is not green means we postpone.

Run the tests on the real engine. We have seen 320 green tests on SQLite while four tables were dead in production on MySQL. Take a dump right before the switch, as we do before every deploy on bkg.dk. The PostgreSQL backup recipe is on the backup page.

Straight about us: we don’t sell a database and take no commission from Microsoft, AWS or anyone else. We move databases for a fixed price. We have not run Babelfish or AWS DMS ourselves; both are described from their documentation.

Sources

Last reviewed: 2026-12-03

Questions about SQL Server and PostgreSQL

How much is a SQL Server licence?

Microsoft’s estimated price for Standard is $3,945 per 2 cores, or $989 per server plus $230 per user or device. As a subscription, $1,418 a year per 2 cores. Excluding VAT; the final price comes from your reseller.

Is there a free SQL Server?

Yes, Express is free. From SQL Server 2025 a database may be 50 GB, but the engine uses at most 4 cores and 1,410 MB of buffer pool. The Developer editions are free for development and test only.

Can pgloader migrate stored procedures?

No. pgloader moves tables, data, indexes, foreign keys and sequences. Procedures, triggers and views are rewritten in PL/pgSQL. In our test the view and the procedure did not come across.

Why are timestamps off after the migration?

Because DATETIME has no time zone. pgloader reads the value in the session time zone. Load in UTC and convert only the columns that were stored in Danish local time.

When should we stay on SQL Server?

When the system is a packaged product that only supports SQL Server, or when the database fits the free Express edition. Then a migration is risk without a payoff.

Fixed price, no commitment

Get a fixed price for your database task

Tell us briefly what you use today and what you want to achieve. A person answers by e-mail with a proposal and a fixed price before any work starts.

  • A fixed price before we start
  • An answer by e-mail, no calls
  • Your details are used only to answer you

Fields marked with * are required.

What is it about?