Convert SQL Server queries to Postgres on the fly Convert SQL Server queries to Postgres on the fly postgresql postgresql

Convert SQL Server queries to Postgres on the fly


If I were in your position I would have a look on upgrading your SQL Sever to 2019 ASAP (as of today, you can find on Twitter that the officially supported production ready version is available on request). Then have a look on the Polybase feature they (re)introduced in this version. In short words it allows you to connect your MSSQL instance to other data source (like Postgres) and query the data in as they would be "normal" SQL Server DB (via T-SQL) then in the background your queries will be transformed into the native pgsql and consumed from your real source.There is not much resources on this product (as 2019 version) yet, but it seems to be one of the most powerful features coming with this release.

This is what BOL is saying about it (unfortunately, it mostly covers the old 2016 version).

There is an excellent, yet very short presentation by Bob Ward ( Principal Architect @ Microsoft) he did during SQL Bits 2019 on this topic.


The only thing I can think of that might be worth trying is SQL::Translator. It's a set of Perl modules that have been around for ages but seem to be still maintained. Whether it does what you want will depend on how detailed those queries are.


The no-brainer solution is to keep a SQL Server Express in place and introduce Triggers that call out to the Postgres database.

If this is too heavy, you can look at creating a Tabular Data Stream (TDS is SQL Server network transport) gateway with limited functionality and map each possible incoming query with any parameters to a static Postgres query. This limits any testing to a finite, small, number of cases.

This way, there is no SQL Server, and you have more control than with the trigger option.

If your terminals have a limited dialect demand then this may be practical. Attempting a general translation is very likely to be worth more than the devices cost to replace (unless you have zillions already deployed).

There is an open implementation FreeTDS that you could use if you are happy with C or Java.