SQL Server
Microsoft SQL Server is a proprietary relational database management system developed by Microsoft.
The SQL Server Wrapper allows you to read data from Microsoft SQL Server within your Postgres database.
Preparation
Before you can query SQL Server, you need to enable the Wrappers extension and store your credentials in Postgres.
Enable Wrappers
Make sure the wrappers
extension is installed on your database:
1 |
|
Enable the SQL Server Wrapper
Enable the mssql_wrapper
FDW:
1 2 3 |
|
Store your credentials (optional)
By default, Postgres stores FDW credentials inside pg_catalog.pg_foreign_server
in plain text. Anyone with access to this table will be able to view these credentials. Wrappers is designed to work with Vault, which provides an additional level of security for storing credentials. We recommend using Vault to store your credentials.
1 2 3 4 5 6 7 |
|
The connection string is an ADO.NET connection string, which specifies connection parameters in semicolon-delimited string.
Supported parameters
All parameter keys are handled case-insensitive.
Parameter | Allowed Values | Description |
---|---|---|
Server | <string> |
The name or network address of the instance of SQL Server to which to connect. Format: host,port |
User | <string> |
The SQL Server login account. |
Password | <string> |
The password for the SQL Server account logging on. |
Database | <string> |
The name of the database. |
IntegratedSecurity | false | Windows/Kerberos authentication and SQL authentication. |
TrustServerCertificate | true, false | Specifies whether the driver trusts the server certificate when connecting using TLS. |
Encrypt | true, false, DANGER_PLAINTEXT | Specifies whether the driver uses TLS to encrypt communication. |
ApplicationName | <string> |
Sets the application name for the connection. |
Connecting to SQL Server
We need to provide Postgres with the credentials to connect to SQL Server. We can do this using the create server
command:
1 2 3 4 5 |
|
1 2 3 4 5 |
|
Create a schema
We recommend creating a schema to hold all the foreign tables:
1 |
|
Options
The full list of foreign table options are below:
table
- Source table or view name in SQL Server, required.
This can also be a subquery enclosed in parentheses, for example,
1 |
|
Entities
SQL Server Tables
This is an object representing SQL Server tables and views.
Ref: Microsoft SQL Server docs
Operations
Object | Select | Insert | Update | Delete | Truncate |
---|---|---|---|---|---|
table/view | ✅ | ❌ | ❌ | ❌ | ❌ |
Usage
1 2 3 4 5 6 7 8 9 |
|
Notes
- Supports both tables and views as data sources
- Can use subqueries in the
table
option - Query pushdown supported for:
where
clausesorder by
clauseslimit
clauses
- See Data Types section for type mappings between PostgreSQL and SQL Server
Query Pushdown Support
This FDW supports where
, order by
and limit
clause pushdown.
Supported Data Types
Postgres Type | SQL Server Type |
---|---|
boolean | bit |
char | tinyint |
smallint | smallint |
real | float(24) |
integer | int |
double precision | float(53) |
bigint | bigint |
numeric | numeric/decimal |
text | varchar/char/text |
date | date |
timestamp | datetime/datetime2/smalldatetime |
timestamptz | datetime/datetime2/smalldatetime |
Limitations
This section describes important limitations and considerations when using this FDW:
- Large result sets may experience slower performance due to full data transfer requirement
- Only supports specific data type mappings between Postgres and SQL Server
- Only support read operations (no INSERT, UPDATE, DELETE, or TRUNCATE)
- Windows authentication (Integrated Security) not supported
- Materialized views using these foreign tables may fail during logical backups
Examples
Basic Example
First, create a source table in SQL Server:
1 2 3 4 5 6 7 8 9 10 11 |
|
Then create and query the foreign table in PostgreSQL:
1 2 3 4 5 6 7 8 9 10 11 |
|
Remote Subquery Example
Create a foreign table using a subquery:
1 2 3 4 5 6 7 8 9 10 11 |
|