As you may know, Microsoft SQL Server is very popular RDBMS with highly restrictive licencing and high cost of ownership if the database is of significant size, or is used by a significant number of clients. It provides a very user-friendly interface and easy to learn. This has resulted in a large installed user base.
PostgreSQL is the world's most advanced open source database. The PostgreSQL community is very strong and continuously improving existing features and implementing new features. As per db-engine popularity rank , PostgreSQL was the DBMS of the year 2017.
Why Migrate from MS SQL Server to PostgreSQL? MS SQL Server is a proprietary database from Microsoft, while PostgreSQL is developed and maintained by a global community of open source developers. If cost is an issue, then definitely you should go with PostgreSQL. You can check the pricing here . PostgreSQL is a cross platform database engine and it is available for windows, Mac, Solaris, FreeBSD and linux while SQL Server only runs on Windows operating system. As you may know, PostgreSQL is open source and completely free while mssql Server cost depends on the number of users and database size. Flexible open source licencing and easy availability from public cloud providers like AWS, Google cloud etc. Benefit from open source add-ons to improve performance. What You Should KnowAlthough both Microsoft SQL Server database and PostgreSQL database are ANSI-SQL compliant but there are still differences between their SQL syntax, data types, case sensitivity, and it makes transferring data not so trivial.
Before migration, understand the differences between MSSQL and PostgreSQL. There are many features in both databases so you should know the behaviour of those features/functions in MSSQL and PostgreSQL. Please check some important differences you should know before migration.
Data Type MappingSome of the data types of MSSQL don’t match directly with PostgreSQL data types, so you need to change it to corresponding PostgreSQL data type.
Please check the below table.
Microsoft SQL Server PostgreSQL BIGINT 64-bit integer BIGINT BINARY(n) Fixed length byte string BYTEA BIT 1, 0 or NULL BOOLEAN CHAR(n) Fixed length char string, 1 <= n <= 8000 CHAR(n) VARCHAR(n) Variable length char string, 1 <= n <= 8000 VARCHAR(n) VARCHAR(max) Variable length char string, <= 2GB TEXT VARBINARY(n) Variable length byte string , 1 <= n <= 8000 BYTEA VARBINARY(max) Variable length byte string , <= 2GB BYTEA NVARCHAR(n) Variable length Unicode UCS-2 string VARCHAR(n) NVARCHAR(max) Variable length Unicode UCS-2 data, <= 2GB TEXT TEXT Variable length character data, <= 2GB TEXT NTEXT Variable length Unicode UCS-2 data, <= 2GB TEXT DOUBLE PRECISION Double precision floating point number DOUBLE PRECISION FLOAT(p) Floating point number DOUBLE PRECISION INTEGER 32 bit integer INTEGER NUMERIC(p,s) Fixed point number NUMERIC(p,s) DATE Date includes year, month and day DATE DATETIME Date and Time with fraction TIMESTAMP(3) DATETIME2(p) Date and Time with fraction TIMESTAMP(n) DATETIMEOFFSET(p) Date and Time with fraction and time zone TIMESTAMP(p) WITH TIME ZONE SMALLDATETIME Date and Time TIMESTAMP(0) TINYINT 8 bit unsigned integer, 0 to 255 SMALLINT UNIQUEIDENTIFIER 16 byte GUID(UUID) data CHAR(16) ROWVERSION Automatically updated binary data BYTEA SMALLMONEY 32 bit currency amount MONEY IMAGE Variable length binary data, <= 2GB BYTEADownload the Whitepaper Today
PostgreSQL Management & Automation with ClusterControl
Learn about what you need to know to deploy, monitor, manage and scale PostgreSQL
Download the Whitepaper
Incompatibilities in MS SQL Server and PostgreSQLThere are many incompatibilities present in MS SQL Server and PostgreSQL, You can see some of them here. You can automate them by creating extensions so that you can use the MS SQL Server function as it is in PostgreSQL and you can save your time.
DATEPARTDATEPART must be replaced by DATE_PART in PostgreSQL.
ExampleMS SQL:
DATEPART( datepart , date )PostgreSQL:
date_part( text , timestamp ) date_part( text , interval ) ISNULLISNULL function must be replaced by COALESCE function in PostgreSQL.
ExampleMS SQL Server:
ISNULL(exp, replacement)PostgreSQL:
COALESCE(exp, replacement) SPACESPACE function in MS SQL Server must be replaced by REPEAT function in PostgreSQL.
ExampleMS SQL Server:
SPACE($n)Where $n is the number of spaces to be returned.
PostgreSQL:
REPEAT(‘ ’, $n) DATEADDPostgreSQL does not provide DATEADD function similar to MS SQL Server, you can use datetime arithmetic with interval literals to get the same results.
ExampleMS SQL Server:
--Add 2 day to the current date SELECT DATEADD(day, 2, GETDATE());PostgreSQL:
--Add 2 day to the current date SELECT CURRENT_DATE + INTERVAL ‘2 day’; String ConcatenationMS SQL Server uses ‘+’ for String Concatenation whereas PostgreSQL uses ‘||’ for the same.
ExampleMS SQL Server:
SELECT FirstName + LastName FROM employee;PostgreSQL:
SELECT FirstName || LastName FROM employee; CHARINDEXThere is CHARINDEX function in PostgreSQL. You can replace this function by PostgreSQL equivalent POSITION function.
ExampleMS SQL Server:
SELECT CHARINDEX('our', 'resource');PostgreSQL:
SELECT POSITION('our' in 'resource'); GETDATEGETDATE function returns the current date and time. There is no GETDATE function in PostgreSQL, but there is NOW() function for the same purpose. If there are multiple occurrences of the GETDATE function then you can automate them using extension. Please check how to create modules using extension .
ExampleMS SQL Server:
SELECT GETDATE();PostgreSQL:
SELECT NOW(); ToolsYou can use some tools to migrate MS SQL Server database to PostgreSQL. Please test the tool before use it.
PgloaderYou can use the pgloader tool to migrate MS SQL database to PostgreSQL. The commands in the pgloader load the data from MS SQL database. Pgloader supports automatic discovery of the schema, including build of the indexes, primary key and foreign keys constraints.
Pgloader provides various casting rules which can convert the MS SQL data type to a PostgreSQL data type.
Sqlserver2pgsqlThis is another open source migration tool to convert Microsoft SQL Server database into a PostgreSQL database, as automatically as possible. Sqlserver2pgsql is written in Perl.
Sqlserver2pgsql tool does two things:
It converts a SQL Server schema to a PostgreSQL schema It can produce a Pentaho Data Integrator (Kettle) jib to migrate all the data from SQL Server to PostgreSQL. This is an optional part.