I need to import a SQL Server database which is in the .mdf format into a PostgreSQL database. My system is Mac OS X.

How to do it in the easiest way?

  • Do you want just the tables and data, or do you need stored procedures and functions as well? If so, you are likely going to have to export those and manually compile them in postgresql, making syntax changes as required. – datagod May 14 '12 at 20:12

You'll need to attach the database to a Microsoft SQL Server database, the export the data into the Postgres database. Assuming that you've got the Windows Postgres ODBC drivers handy you can use SQL Server's Import/Export wizard to copy the data into the Postgres database.

| improve this answer | |

I've had to do the opposite of this by which I used this process: https://dba.stackexchange.com/a/15833/4490

So you could reverse the insert ...

Link the postgres server

use INSERT INTO OPENQUERY([ServerName],'select << column list >> from << table name >>') select << column list >> from << table name >> as the query to use in the cursor.

This would require that you have already set up the database schema on the postgres server.

Later, in other answers, the user discovered Kettle with which you can build an ETL process to connect from MSSQL to postgres. I've never had success with the Import/Export wizard in SSMS, but if you can get that to work then that'd definitely be the lowest LOE.


Another option might be to install the odbc_fdw (foreign data wrapper) http://www.postgresonline.com/journal/archives/249-ODBC-Foreign-Data-wrapper-to-query-SQL-Server-on-Window---Part-2.html

As @AaronBertrand noted, it is unclear as to whether or not you have a SQL Server instance available to you, or if it is just a data file that you have in your possession.

| improve this answer | |
  • 1
    Seems to me this would also require setting up an instance of SQL Server, which I'm not sure the OP has (only talked about an MDF file). Just saying that for clarity, so the user can expect to have to find an instance of SQL Server (or install one) and then attach the database to it in order to proceed (and Express/Workgroup/Standard might not be sufficient due to database size or feature usage). It's also good to note that just because you have an MDF file it does not mean the file is valid - it might not have been properly detached. – Aaron Bertrand May 14 '12 at 19:36
  • You're very right. Thanks for calling out my assumptions. – swasheck May 14 '12 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy