Nowadays many companies migrate their databases to free open-source DBMS in order to reduce total cost of ownership. PostgreSQL looks really good option among all free systems because it is 100% compliant with SQL standard, supports point-in-time recovery and sophisticated locking mechanisms, provides advanced data types such as multi-dimensional arrays and spatial.
The most straight forward way to migrate from MS SQL to PostgreSQL is based on popular extract-transfer-load (ETL) model:
- All table definitions, indexes and constraints are extracted from SQL Server database in form of CREATE-statements
- Those statements are converted according to PostgreSQL specification of CREATE-queries (with respect to types mapping and naming rules) and loaded to the target database
- Next step is to export SQL Server data into CSV files as external intermediate storage
- The CSV files must be transformed to comply with PostgreSQL format when it is necessary
- Finally, the transformed data must be loaded to the target database
This is how to extract definitions of MS SQL tables in required format:
- In MS SQL 2008 and earlier versions open Management Studio, right-click on the database name, then click on ‘Tasks > Generate Scripts’ menu item. Make sure that “data” checkbox is off.
- In MS SQL 2012 and later versions open Management Studio, right-click on the database name, then click on ‘Tasks > Generate Scripts’ menu item. In appeared window go to “Set scripting options” tab, click on Advanced link and select “data and schema” in the ‘General’ section.
Don’t forget to transform the resulting script according to PostgreSQL format before moving to the next step:
- remove square brackets around types
- change square brackets around database object names by double quotes
- replace all occurrences of schema “dbo” by “public”
- remove all optional MS SQL keywords that are not supported by PostgreSQL (i.e. “WITH NOCHECK”, “CLUSTERED”)
- remove any specifications of filegroup, for example “ON PRIMARY”
- change MS SQL auto-number types “INT IDENTITY(…)” by “SERIAL”
- convert types that are not supported by PostgreSQL into equivalents (i.e. “DATETIME” becomes “TIMESTAMP”, “MONEY” becomes NUMERIC(19,4))
- replace SQL Server query terminator “GO” by the PostgreSQL one “;”
The next move is to process the data, which can be accomplished with the use of the MS SQL Management Studio.
- Right-click on database, then click Tasks, Export Data
- Go through the wizard and select “Microsoft OLE DB Provider for SQL Server” as data source, and “Flat File Destination” as destination.
As soon as the export is finalized, the exported data will exists in the destination file in the comma-separated values (CSV) format.
Now it is time to load data from CSV files to PostgreSQL tables. For this purpose, use the “COPY” statement as follows:
COPY <table name> FROM <path to csv file> DELIMITER ‘,’ CSV;
If you receive a “Permission denied” error, try the “\COPY” command instead.
The steps above illustrate that manual conversion is a time-consuming procedure with high risk of data loss or corruption. Fortunately, there are some special tools which can migrate database from MS SQL to PostgreSQL within just a couple of clicks. One of such solutions is MS SQL to PostgreSQL converter, a program having all necessary features to handle migration of large and complicated databases between the two DBMS. It is developed by Intelligent Converters, software company focusing on database conversion and synchronization techniques since 2001.