Migrating from MSSQL to PostgreSQL - What You Should Know

52 篇文章 0 订阅
18 篇文章 0 订阅

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?

  1. 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.
  2. 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.
  3. Flexible open source licencing and easy availability from public cloud providers like AWS, Google cloud etc.
  4. Benefit from open source add-ons to improve performance.

What You Should Know

Although 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 Mapping

Some 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 ServerPostgreSQL
BIGINT64-bit integerBIGINT
BINARY(n)Fixed length byte stringBYTEA
BIT1, 0 or NULLBOOLEAN
CHAR(n)Fixed length char string, 1 <= n <= 8000CHAR(n)
VARCHAR(n)Variable length char string, 1 <= n <= 8000VARCHAR(n)
VARCHAR(max)Variable length char string, <= 2GBTEXT
VARBINARY(n)Variable length byte string , 1 <= n <= 8000BYTEA
VARBINARY(max)Variable length byte string , <= 2GBBYTEA
NVARCHAR(n)Variable length Unicode UCS-2 stringVARCHAR(n)
NVARCHAR(max)Variable length Unicode UCS-2 data, <= 2GBTEXT
TEXTVariable length character data, <= 2GBTEXT
NTEXTVariable length Unicode UCS-2 data, <= 2GBTEXT
DOUBLE PRECISIONDouble precision floating point numberDOUBLE PRECISION
FLOAT(p)Floating point numberDOUBLE PRECISION
INTEGER32 bit integerINTEGER
NUMERIC(p,s)Fixed point numberNUMERIC(p,s)
DATEDate includes year, month and dayDATE
DATETIMEDate and Time with fractionTIMESTAMP(3)
DATETIME2(p)Date and Time with fractionTIMESTAMP(n)
DATETIMEOFFSET(p)Date and Time with fraction and time zoneTIMESTAMP(p) WITH TIME ZONE
SMALLDATETIMEDate and TimeTIMESTAMP(0)
TINYINT8 bit unsigned integer, 0 to 255SMALLINT
UNIQUEIDENTIFIER16 byte GUID(UUID) dataCHAR(16)
ROWVERSIONAutomatically updated binary dataBYTEA
SMALLMONEY32 bit currency amountMONEY
IMAGEVariable length binary data, <= 2GBBYTEA

 

Incompatibilities in MS SQL Server and PostgreSQL

 

There 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.

DATEPART

DATEPART must be replaced by DATE_PART in PostgreSQL.

Example

MS SQL:

1

DATEPART( datepart , date )

PostgreSQL:

1

2

date_part( text , timestamp )

date_part( text , interval )

ISNULL

ISNULL function must be replaced by COALESCE function in PostgreSQL.

Example

MS SQL Server:

1

ISNULL(exp, replacement)

PostgreSQL:

1

COALESCE(exp, replacement)

SPACE

SPACE function in MS SQL Server must be replaced by REPEAT function in PostgreSQL.

Example

MS SQL Server:

1

SPACE($n)

Where $n is the number of spaces to be returned.

PostgreSQL:

1

REPEAT(‘ ’, $n)

DATEADD

PostgreSQL does not provide DATEADD function similar to MS SQL Server, you can use datetime arithmetic with interval literals to get the same results.

Example

MS SQL Server:

1

2

--Add 2 day to the current date

SELECT DATEADD(day, 2, GETDATE());

PostgreSQL:

1

2

--Add 2 day to the current date

SELECT CURRENT_DATE + INTERVAL ‘2 day’;

String Concatenation

MS SQL Server uses ‘+’ for String Concatenation whereas PostgreSQL uses ‘||’ for the same.

Example

MS SQL Server:

1

SELECT FirstName + LastName FROM employee;

PostgreSQL:

 

1

SELECT FirstName || LastName FROM employee;

CHARINDEX

There is CHARINDEX function in PostgreSQL. You can replace this function by PostgreSQL equivalent POSITION function.

Example

MS SQL Server:

1

SELECT CHARINDEX('our', 'resource');

PostgreSQL:

1

SELECT POSITION('our' in 'resource');

GETDATE

GETDATE 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.

Example

MS SQL Server:

1

SELECT GETDATE();

PostgreSQL:

1

SELECT NOW();

Tools

You can use some tools to migrate MS SQL Server database to PostgreSQL. Please test the tool before use it.

  1. Pgloader

    You 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.

  2. Sqlserver2pgsql

    This 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:

    1. It converts a SQL Server schema to a PostgreSQL schema
    2. It can produce a Pentaho Data Integrator (Kettle) jib to migrate all the data from SQL Server to PostgreSQL. This is an optional part.

Testing

Testing the application and migrated database is very important because some of the functions are the same in both the databases, however, the behaviour is different.

Some common scenarios need to be checked:

  • Check whether all database objects are correctly converted or not.
  • Check the behaviour of all the functions in DML is working correctly or not.
  • Load sample data into both databases and check the result of all the DML queries in both the databases. The result of all the SQL’s should be the same.
  • Check the performance of the DML and improve it if necessary.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值