How to Self-Host SQL Server in PostgreSQL

Galaxy Glossary

How can I self-host Microsoft SQL Server and query it from PostgreSQL?

Run Microsoft SQL Server locally via Docker and access it from PostgreSQL with tds_fdw.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What does “self-host SQL Server” mean?

Self-hosting places Microsoft SQL Server on your own machine or server—no Azure or AWS required. You control versions, patches, and data residency.

What are the prerequisites?

Install Docker 24+, psql 15+, and your favorite SQL editor. Allocate ≥4 GB RAM and open port 1433.

How do I start a local SQL Server container?

Run the Docker command in the Syntax section. It pulls the 2022 image, accepts the EULA, exposes port 1433, and persists data in a named volume.

How do I create ecommerce tables in SQL Server?

Connect with sqlcmd or Azure Data Studio and execute T-SQL:

CREATE DATABASE shop;
GO
USE shop;
CREATE TABLE Customers(id INT PRIMARY KEY, name NVARCHAR(50), email NVARCHAR(80), created_at DATETIME);
CREATE TABLE Orders(id INT PRIMARY KEY, customer_id INT, order_date DATETIME, total_amount DECIMAL(10,2));
CREATE TABLE Products(id INT PRIMARY KEY, name NVARCHAR(60), price DECIMAL(10,2), stock INT);
CREATE TABLE OrderItems(id INT PRIMARY KEY, order_id INT, product_id INT, quantity INT);

How do I connect PostgreSQL to SQL Server with tds_fdw?

Install tds_fdw, then run:

CREATE EXTENSION IF NOT EXISTS tds_fdw;
CREATE SERVER mssql_srv FOREIGN DATA WRAPPER tds_fdw OPTIONS (servername 'localhost', port '1433', database 'shop');
CREATE USER MAPPING FOR postgres SERVER mssql_srv OPTIONS (username 'sa', password 'Str0ngPass!');
IMPORT FOREIGN SCHEMA dbo LIMIT TO (Customers, Orders, Products, OrderItems) FROM SERVER mssql_srv INTO public;

How can I query SQL Server data from PostgreSQL?

Once foreign tables exist, reference them like local tables:

SELECT c.name, SUM(o.total_amount) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
GROUP BY c.name
ORDER BY lifetime_value DESC;

Best practices for self-hosting SQL Server

Use strong SA passwords, enable automatic backups via --mount, monitor container logs, and schedule OS-level updates.

Common mistakes and fixes

See the section below for quick remedies.

Why How to Self-Host SQL Server in PostgreSQL is important

How to Self-Host SQL Server in PostgreSQL Example Usage


-- Total quantity sold per product across SQL Server data
SELECT p.name, SUM(oi.quantity) AS total_sold
FROM Products p
JOIN OrderItems oi ON oi.product_id = p.id
GROUP BY p.name
ORDER BY total_sold DESC;

How to Self-Host SQL Server in PostgreSQL Syntax


docker run \
  --name mssql_local \
  -e "ACCEPT_EULA=Y" \
  -e "SA_PASSWORD=Str0ngPass!" \
  -p 1433:1433 \
  -v mssql_data:/var/opt/mssql \
  -d mcr.microsoft.com/mssql/server:2022-latest

-- tds_fdw connection from PostgreSQL
CREATE EXTENSION IF NOT EXISTS tds_fdw;
CREATE SERVER mssql_srv FOREIGN DATA WRAPPER tds_fdw
  OPTIONS (servername 'localhost', port '1433', database 'shop');
CREATE USER MAPPING FOR postgres SERVER mssql_srv
  OPTIONS (username 'sa', password 'Str0ngPass!');
IMPORT FOREIGN SCHEMA dbo LIMIT TO (Customers, Orders, Products, OrderItems)
  FROM SERVER mssql_srv INTO public;

Common Mistakes

Frequently Asked Questions (FAQs)

Is Docker the only way to self-host SQL Server?

No. You can install the native Linux or Windows packages, but Docker offers faster setup and isolation.

Does tds_fdw support writes to SQL Server?

Yes—INSERT, UPDATE, and DELETE work if the foreign table has a primary key and you set row_estimate_method properly.

Will this work on Apple Silicon?

Use the Azure SQL Edge image or run SQL Server in an x86-64 VM, because the official image targets AMD64.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.