I'm migrating data from a PostgreSQL database (with PostGIS), which contains geospatial data to SQL Server '12 and having some import errors. I'm assuming these errors are due to the geospatial data types.
I originally ran pg_dump dbname > outfile
and then loaded the output file which is a .bak
into SQL Server '12 using the GUI import wizard.
Import Errors:
- Error 0xc02020a1
- Error 0xc020902a
- Error 0xc0202092
- Error 0xc0047038
Question: What is the best way to migrate PostgreSQL geospatial data into SQL Server '12?
Best Answer
A python method of doing this (for us a daily scripted process) involves reading the PostGIS table using psycopg2 and casting the spatial data as text.
I then use an insert and dump the data into MSSQL using pypyodbc, while using a 'temp_geom' table to hold the 'geometry text', then run a quick function to turn that text back into geometry, then remove the temp_geom column.
Of course, the PostGIS table exists, and the MSSQL table needs to be created in the same schema you're going to be loading into (using a create table (columns) statement)
So the python script looks like this:
It may look like a lot, but we have a few dozen of these deployed and scripted on Windows and Ubuntu servers and they hum along every morning!
Another HUGE benefit is when you're selecting the geom from PostGIS you can use any of the spatial functions to reproject the data, buffer, etc. etc. etc., so the select would look like this:
So that means we can store our PostGIS data in WGS84, then load it into MSSQL as State Plane CO Central (2877) for our MSSQL clients...