Many companies follow the long-term modern trend to migrate databases from commercial DBMS to free open-source equivalent 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 andspatial.
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:
- InMS 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
- replace all square brackets around names of database entries 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 “;”
Next step of the database migration procedure 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
- Using intuitive interface of the wizard specify “Microsoft OLE DB Provider for SQL Server” as data source and “Flat File Destination” as destination.
After the export is completed, the resulting data will appear in the specified destination file according to the comma-separated values (CSV) format.
Now it is time toload 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 conversionisa 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.
Learn more about MS SQL to PostgreSQL converter at https://www.convert-in.com/mss2pgs.htm