Export data from Postgres to SQL Server using SSIS

Problem

Sometimes it is necessary to migrate data from Postgres to SQL Server.  Unfortunately, there is not too much information on the Internet about this topic. In this tip we will outline the steps to import a single table from Postgres to SQL Server with SQL Server Integration Services.

Solution

In this example, the database used to import data is the ‘Postgres’ database which is installed by default. We will use this database to export data from the students table in Postgres to SQL Server with SQL Server Integration Services.

Here are the prerequisites needed:

  • Install the Postgres Database.
  • Install the PGAdmin.
  • Install the Postgres OLE DB Provider on the machine where SQL Server Integration Services will run, because this will install a Postgres OLE DB provider.
  • Install or have access to SQL Server Enterprise or Standard edition.  In this example, I am using SQL Server 2008 R2, but SQL Server 2005 is an option as well.

In this example, a table named students will be created in Postgres and then it will be imported to the Adventureworks sample SQL Server database.

Demonstration – Export data from Postgres to SQL Server

Let’s walk through a step by step example to export data from Postgres to SQL Server.

  1. Open the PostgreSQL>pgAdmin III tool.
  2. Start the pgAdmin tool

  3. In the pgAdmin III tool, right click on the PostgreSQL instance and select the ‘Connect’ option.
  4. Connect to the Postgres database in pgAdmin III

  5. Specify the login and password.  The ‘postgres’ user is a super user created by default. In this tip, the ‘postgres’ user will be used.

  6. Open the Server tree to PostgreSQL | Databases | postgres | Schemas| public| tables and right click and select New Table.
  7. Create a New Table in Postgres

  8. In the ‘Name’ field enter ‘Students’ and in the ‘Owner’ combo box, select ‘postgres
  9. ‘.
    Specify the table properties in Postgres

  10. In the columns Tab, press the ‘Add’ button.
  11. Add columns to a table in Postgres

  12. Add the ‘ID’ column with data type integer and ‘Name’ with the data type character(30)
  13. Create the ID and name columns in Postgres

  14. Click the ‘Constraints’ tab, select the ‘Primary Key’ option in the combo box and press the ‘Add’ button.
  15. In the New Primary Key window, select the ‘ID’ column in the combo box and press the ‘Add’ button.
  16. Create a Primary Key on a table in Postgres

  17. Click the ‘students’ table in the Object browser and press the View the data in the selected object icon.
    View the data in a Postgres table

  18. Add data to the Student table.
  19. Add data to the Postgres table

  20. Now, we have a Student table in Postgres database with data. Now, let’s import the data to SQL Server using the Integration Services. Open the SQL Server Business Intelligence Development Studio and open a new Integration Services Project.
  21. Create a SSIS package to export data from Postgres

  22. In the Toolbox drag and drop the Data Flow task to the design pane.
  23. Drag and drop the Data Flow in SQL Server Integration Services

  24. Double click in the Data flow Task in order to create a Data Flow.
  25. In the toolbar drag and drop the OLE DB Source and OLE DB Destination and join both tasks with the green arrow.
  26. Add the Ole DB Source and Destination to the SSIS Data Flow Tab

  27. Double click on the OLE DB Source task and press the New button to create a new OLE DB connection manager..
  28. Add the OLE DB Source properties in SSIS

  29. In the Configure OLE DB Connection Manager, press ‘New’.
  30. Create a Data Connection in SSIS

  31. In the Connection Manager, select the Native OLE DB\PostgresSQL Native Provider. Note that this option will be available only if you installed the prerequisites specified at the beginning of this tip.

  32. In the Server or File name, specify the name of the Postgres Server.  In this example, the localhost is used.

  33. Select the option ‘Use a specific user name and password’.

  34. Check the allow saving password option.

  35. In user name specify the user to connect to postgres and the user password.  In this example, the postgres user is used.

  36. In the initial catalog, select the database used to create the table.  Remember in step 4, we used the postgres database to create the students table.
  37. Specify the Postgres connection properties in SQL Server Integration Services

  38. Press OK twice.

  39. In the OLE DB Source Editor, in the name of the table or the view, select the “public”.”Students” table and press OK.
  40. Specify the Table Name in the SSIS OLE DB Source Editor

  41. In the design pane, double click the OLE DB Destination task.

  42. In the OLE DB connection Manager, press the New button
  43. Configure the table properties in the SSIS OLE DB Destination Editor

  44. In the Configure OLE DB Connection Manager, press ‘New
  45. ‘.
    Select the Data Connection in SSIS for Postgres

  46. In the Connection Manager, select the Native OLE DB\SQL Server Native Client
  47. .
  48. In the Server name specify the name of the SQL Server Instance name.  In this example, the localhost is used.

  49. Specify the log on to the server options in order to login to SQL Server.  In this example, the windows authentication is used.

  50. In the select or enter the database name combo box, select the SQL Server database where you want to import the Postgres Students table.  In this example, the Adventureworks will be used and press OK.
  51. Specify the destination Database in SQL Server Integration Services

  52. In the OLE DB Destination Editor, in the Name of the table or view press the New button./li>
    Select the table destination in SSIS

  53. In the Create table windows, use this code and press OK:
  54. CREATE TABLE [Students] (
    [ID] int,
    [Name] nvarchar(30)
    )

    Create the students table in SSIS

  55. In the OLE BD Destination Editor, click the Mapping page and press OK
  56. Map the tables in SSIS between the source and destination

  57. Now the project is ready to start. Press the start debugging icon. If everything is successful, the tasks should be colored green and the numbers of rows imported should be displayed.
  58. Run the SSIS package

  59. In order to verify that the table was imported, in SQL Server Management Studio, go to the Adventureworks database (or the database that you select in step 32) and verify that the students table was created. Open the table to verify that the data is correct.
  60. Verify the table was created with the data in SQL Server Management Studio
  61. In this tip, you have learned how to import tables from Postgres to SQL Server.

Next Steps

One comment

  1. A good article on using those tools to transfer data.

    I have developed an alternative tool: SQLpipe. It is an open-source solution that can transfer data from PostgreSQL to SQL Server.

    Here is a step by step guide on using it transfer data from PostgreSQL to SQL Server: https://sqlpipe.com/transfer-data-from-postgresql-to-sql-server/

Leave a Reply

Your email address will not be published. Required fields are marked *