SQLServer open source lets PostgreSQL query Microsoft SQL Server via the free tds_fdw extension.
Running reports across PostgreSQL and Microsoft SQL Server is simpler when you can query both from one place. The tds_fdw
foreign data wrapper (FDW) is open-source and makes SQL Server tables appear local inside PostgreSQL.
Install PostgreSQL 13+ and compile tds_fdw
against FreeTDS. Obtain a SQL Server login with read rights to the target database.
Load the shared library, then create the extension: CREATE EXTENSION tds_fdw;
Use CREATE SERVER
with SQL Server host, port, and TDS version. Specify OPTIONS
like database
and tds_version
.
CREATE SERVER mssql_srv FOREIGN DATA WRAPPER tds_fdw OPTIONS (host '10.0.0.25', port '1433', database 'ecommerce', tds_version '7.4');
Create a user mapping to pass SQL Server credentials safely.
CREATE USER MAPPING FOR analytics_user SERVER mssql_srv OPTIONS (username 'report', password 'S3cRe7');
Either define foreign tables manually or use IMPORT FOREIGN SCHEMA
for bulk import.
```sqlCREATE FOREIGN TABLE sqlserver_products ( id int, name text, price numeric, stock int) SERVER mssql_srv OPTIONS (schema_name 'dbo', table_name 'Products');```
Once foreign tables exist, treat them like local ones. PostgreSQL handles the push-down.
```sqlSELECT p.name, p.price, oi.quantityFROM sqlserver_products pJOIN OrderItems oi ON oi.product_id = p.idORDER BY oi.quantity DESC;```
Limit column lists, create statistics with ANALYZE
, and isolate FDW usage in a dedicated schema. Monitor network latency because cross-DB joins may be chatty.
Avoid huge cross-joins without filters; they pull entire SQL Server tables. Always match data types between PostgreSQL and SQL Server to prevent silent casts.
No. It is a community extension, but it is actively maintained and widely used.
Yes. INSERT, UPDATE, and DELETE are supported when the SQL Server user has proper privileges.
Yes. Configure FreeTDS with tds_encrypt_login = require
and set encrypt_level
in the server definition.