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 andspatial.
The most straight forward way to migrate from SQL Server 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 SQL Server tables in required format:
- InMS SQL 2008 and earlier versionsopen 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 versionsopen 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 anyspecifications 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 of SQL Server to PostgreSQL migration is to process the data, which can be accomplished with the use of the MS SQL Management Studio.
- Select the database by right-clicking and then go to Tasks > Export Data menu item
- Navigate across the wizard screens and select the data source “Microsoft OLE DB Provider for SQL Server” and the destination “Flat File Destination”.
As soon as the export is finalized, the exported data will exist in the comma-separated values (CSV) format and ready for the next step of SQL Server to PostgreSQL migration.
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.
SQL Server to PostgreSQL Converter
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 SQL Server or Azure SQL databases to PostgreSQL within just a couple of clicks. One of such solutions is SQL Server 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 SQL Server to PostgreSQL converter at https://www.convert-in.com/mss2pgs.htm.