Moving from SQL Server to PostgreSQL
16 Nov 2015SQL Server and PostgreSQL are both relational database systems and as such share similarities that should allow you to migrate your data structures between them. In today’s post, I’m going to go through the process of migrating SQL Server data schema objects over to PostgreSQL.
Immediate differences
Whilst both relational database systems implement a core set of the standard language, there are implementation-specific features which need special consideration. So long as you are capable of wrangling text in your favorite editor, the conversion task shouldn’t be that hard.
The batch terminator GO
gets replaces by a much more familiar ;
.
Tables
First thing to do for tables is to generate your create scripts. Make sure that you:
- Turn off
DROP
statement generation for your objects - Turn on index and keys generation
To safely qualify the names of objects within the database, SQL Server will surround its object names with square brackets []
, so you’ll see definitions like this:
PostgreSQL uses double-quotes on object names and doesn’t use the owner (in the above case [dbo]
) to qualify names.
In the above example, Table1
is using IDENTITY
on its primary key field ID
. This gives us the auto-increment functionality that’s so natural in relational database systems. There is a little extra work in PostgreSQL to emulate this behavior through the use of CREATE SEQUENCE and nextval.
The SQL Server definition above now looks like this for PostgreSQL:
Stored Procedures
Stored procedures in SQL Server are considered a much more common citizen in the database world than Stored procedures in PostgreSQL. If your database design hinges on extensive use of stored procedures, you’ll be in for a bit of redevelopment.
Both stored procedures and functions are created using the same syntax in PostgreSQL. The actions that either can perform differ though:
Stored Procedure | Function | |
---|---|---|
Used in an expression | No | Yes |
Return a value | No | Yes |
Output parameters | Yes | No |
Return result set | Yes | Yes |
Multiple result sets | Yes | No |
A simple function that will square its input value looks as follows:
This can be invoked using SELECT
.
A more in-depth example involves returning a result set from within a stored procedure. You can do this in an unnamed fashion; you won’t control the name of the cursor coming back.
You can pull out a single record set:
Multiple record sets:
Invoking these stored procedures so that you can gather the information being returned, requires you to FETCH
these details:
Re-writing retrieve_entities
, we can give the caller the option to name their cursor:
The invocation of this procedure now requires a name:
A much more comprehensive run down of stored procedures/functions can be found here and here.
Views
Views fall into the same category as Tables. The syntax remains very much the same with functions that change between the database platforms.