Database migration between advanced DBMS is non-trivial task that requires a lot of time and efforts. This article explores different tools for database migration from SQL Server or Azure SQL to PostgreSQL.
First open-source solution is pgloader
, a well-known tool that migrates
schemas, data, indexes, and foreign keys from SQL Server into PostgreSQL.
Features:
pgloader
is distributed as open-source software under free licensepgloader
is a cross-platform software as it is a set of LISP scriptspgloader
uses the FreeTDS driver to access SQL Server databases and
expects the data is sent in UTF-8 encoding
Simplest example of running SQL Server to Postgres migration via
pgloader
is illustrated by the following command line:
$ pgloader mssql://mssql_user@mssql_host/mssql_dbname pgsql://pgs_user@pgs_host/pgs_dbname
where mssql_user
, mssql_host
, mssql_dbname
,
pgs_user
, pgs_host
, pgs_dbname
must
be replaced by actual SQL Server and PostgreSQL username, host and database name.
Next open-source solution to automate database migration from Microsoft
SQL Server to Postgres is Sqlserver2pgsql
script written in Perl.
Features:
Although Sqlserver2pgsql is a cross-platform tool, it is recommended to run it inside a Docker container to simplify distribution of the required environment. So, here are the key statements for building proper container for SQL Server to Postgres migration using Sqlserver2pgsql:
FROM adoptopenjdk/openjdk8
ENV MIGRATIONDIR=/opt/mss2pgs_migration RUN mkdir -p $MIGRATIONDIR
RUN apt-get update; apt-get install perl netcat -y; \ apt-get install wget unzip postgresql-client -y
RUN wget --progress=dot:giga https://sourceforge.net/projects/pentaho/files/latest/download?source=files -O /tmp/kettle.zip; \ unzip /tmp/kettle.zip -d /tmp/kettle; \ mv /tmp/kettle/data-integration $MIGRATIONDIR; \ chmod -R +x $MIGRATIONDIR/data-integration/*.sh
RUN wget https://sourceforge.net/projects/jtds/files/latest/download?source=files -O /tmp/jtds.zip; \ unzip /tmp/jtds.zip -d /tmp/jtds; \ cp /tmp/jtds/jtds-*.jar $MIGRATIONDIR/data-integration/lib/; \ rm -Rf /tmp/jtds;rm -f /tmp/jtds.zip
COPY ./scripts /scripts RUN chmod +x /scripts/*.sh WORKDIR $MIGRATIONDIR
./sqlserver2pgsql.pl -b before.sql -a after.sql -u unsure.sql -k kettlejobs -stringtype_unspecified -f conf/$SCHEMA_FILE \ -sh $SRC_HOST -sp $SRC_PORT -su $SRC_USER -sw $SRC_PWD -sd $SRC_DB \ -ph $DEST_HOST -pp $DEST_PORT -pu $DEST_USER -pw $DEST_PWD -pd $DEST_DB
This statement generates script before.sql that creates all schemas (it must be run first), after.sql that creates indexes and constraints (it must be run after the data migration completes) and Kettles job file at $MIGRATIONDIR/kettlejobs/migration.kjb.
PGPASSWORD=$DEST_PWD psql -h $DEST_HOST -p $DEST_PORT -U $DEST_USER -d $DEST_DB -f before.sql
data-integration/kitchen.sh -file=kettlejobs/migration.kjb -level=rowlevel
PGPASSWORD=$DST_PWD psql -h $DST_HOST -p $DST_PORT -U $DST_USER -d $DST_DB -f after.sql
docker build -t sqlserver2psql .
Now you can run the migration through the following command line statement:
docker run --name sqlserver2psql --rm -e SRC_HOST=\ -e SRC_USER= -e SRC_PWD=" " \ -e SRC_DB= -e DEST_HOST= \ -e DEST_PORT=5432 -e DSET_USER= \ -e DEST_PWD= -e DEST_DB=" " --mount type=bind,source="$(pwd)"/conf,target=/opt/data_migration/conf \ sqlserver2psql /scripts/migrate.sh
Both of these tools do not migrate views (as CREATE VIEW statements), stored procedures, triggers and have no advanced capabilities on customization of metadata and migration rules. Besides, pgloader and Sqlserver2pgsql are only available in form of console tools that may be not comfortable for users not so familiar with command line.
For those who look for easy way of running SQL Server to Postgres migration or need to deeply customize the process, the most reasonable option is dedicated commercial software with intuitive graphical interface. One of those tools is SQL Server to PostgreSQL converter.