Access SQL Server tables from PostgreSQL through the tds_fdw foreign-data wrapper.
Migrating gradually, unifying reporting, or joining SQL Server data with PostgreSQL tables becomes simpler when both sources are available in one place. tds_fdw lets you read SQL Server tables as if they were local.
Use the tds_fdw
extension. It leverages FreeTDS to speak the Tabular Data Stream protocol that SQL Server uses.
Install the OS package (e.g., apt install postgresql-15-tds-fdw freetds-bin
).Then load it per database with CREATE EXTENSION tds_fdw;
Create a foreign server that stores host, port, and default database.
Attach SQL Server credentials to a PostgreSQL role so queries can authenticate.
Define which remote tables should appear locally. Use IMPORT
for bulk or CREATE FOREIGN TABLE
for single tables.
Once foreign tables exist, use standard JOIN syntax.PostgreSQL handles pushing as much work as possible to SQL Server and merges the rest locally.
Select only needed columns, filter early, and add indexes on SQL Server in the same way you would for local queries. Enable USE_REMOTE_ESTIMATE on
for accurate planner stats.
Mis-matching data types and forgetting to quote mixed-case SQL Server column names cause most errors.Review type mapping and use double quotes where necessary.
Yes—tds_fdw supports INSERT, UPDATE, and DELETE if the SQL Server login has rights. FOREIGN TABLE must include a primary key.
.
Only SQL authentication is officially supported. For Windows integrated authentication, use a DSN and Kerberos via odbc_fdw
.
Yes. GROUP BY, aggregates, and ORDER BY are usually pushed to SQL Server when tds_fdw
deems it cheaper.
Set encrypt=yes
in the SERVER options or FreeTDS DSN to enable TLS between PostgreSQL and SQL Server.