Connecting SQL Server to PostgreSQL
Installing the PostgreSQL ODBC driver
- Download the PostgreSQL ODBC driver for your Windows platform.
- Install and license the PostgreSQL ODBC driver on the machine where your SQL Server instance is installed.
To do this, execute the file distribution that you downloaded in the previous step, and follow the on screen instructions.
The install program starts the Easysoft License Manager, because you cannot use your Easysoft product until a license is obtained.
The following types of license are available:
- Free time-limited trial license, which gives you free and unrestricted use of the product for a limited period (usually 14 days).
- Full license if you have purchased the product. On purchasing the product, you are given an authorization code, which you use to obtain a license.
- In Easysoft License Manager, enter your details.
Complete the Name, E-Mail Address, and Company fields.
The E-Mail Address must be the same as the address used to register and download from the Easysoft web site or you will be unable to obtain trial licenses.
- Choose Request License.
You're asked for a license type.
- Do one of the following:
- For a trial license, choose Time Limited Trial and then choose Next.
The License Manager asks what software you are licensing. Select your product from the drop-down list and then choose Next.
- If you have obtained an authorization code for a purchased license, select Non-expiring License and then choose Next.
The License Manager requests your authorization code.
Enter the authorization code and then choose Next.
- For a trial license, choose Time Limited Trial and then choose Next.
- The License Manager displays a summary of the information you entered and allows you to choose the method of applying for your license.
- Do one of the following:
- Choose On-line Request if your machine is connected to the internet and can make outgoing connections to port 8884.
The License Manager then sends a request to the Easysoft license server to activate your license key automatically. This is the quickest method and results in your details being entered immediately into our support database.
- The remaining three options (Email Request, Print Request, and View Request) are all ways to obtain a license if your machine is off-line (does not have a connection to the internet).
Each of these methods involves providing Easysoft with information including your machine number (a number unique to your machine) and then waiting to receive your license key.
Instead of emailing your details to Easysoft, you can enter them directly at the Easysoft web site and your license key will be emailed to you automatically.
To use this method, choose View Request, and then visit:
In the licensing page, enter your machine number (and authorization code for a purchased license), choose Submit and your license key will be emailed to you.
When you receive the license key, you can activate it either by double-clicking the email attachment or by choosing Enter License on the License Manager main screen and pasting the license key into the dialog box.
- Choose On-line Request if your machine is connected to the internet and can make outgoing connections to port 8884.
- Choose Finish to exit the License Manager.
The installation is complete.
Configuring an ODBC data source
Before you can use the PostgreSQL ODBC driver to connect your application to PostgreSQL, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (in this case, PostgreSQL) and the ODBC driver that is required to connect to it (in this case, the PostgreSQL ODBC driver).
You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows.
There are two versions of ODBC Data Source Administrator. The version of ODBC Data Source Administrator that you need to run depends on whether the application you want to connect to PostgreSQL is 32-bit or 64-bit. For the applications covered in this article, refer to the following table to find out the application's architecture.
Application | Notes |
---|---|
SQL Server |
There is both a 32-bit and a 64-bit version of SQL Server. To find out which version of SQL Server you have, connect to your SQL Server instance, and then run this SQL statement:
select SERVERPROPERTY('edition') If you have the 64-bit version of SQL Server and want to use a linked server with the PostgreSQL ODBC driver, your application is 64-bit. If you have the 32-bit version of SQL Server or want to use SSIS with the PostgreSQL ODBC driver, your application is 32-bit. |
If you have a 64-bit application, you need to run 64-bit version of ODBC Data Source Administrator. To do this, open Administrative Tools in Control Panel, and then open Data Sources (ODBC). (On Windows Server 2003 and earlier, the Control Panel applet that launches ODBC Data Source Administrator is labelled Data Sources. On Windows 8 and later, the Control Panel applet is labelled ODBC Data Sources (64-bit).)
If you have a 32-bit application, you need to run 32-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:
%windir%\syswow64\odbcad32.exe
Use ODBC Data Source Administrator to create a PostgreSQL ODBC driver data source:
- Choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose Easysoft ODBC-Postgres Driver, and then choose Finish.
- Complete these fields in the Easysoft ODBC-Postgres Driver DSN Setup dialog box:
Setting Value DSN POSTGRESQL User Name The name of your PostgreSQL user. Password The password for your PostgreSQL user. Server The host name or IP address of the machine on which your PostgreSQL server is running. - Use the Test button to verify that you can successfully connect to your PostgreSQL database.
Connecting SQL Server to PostgreSQL
Linked server
These steps show you how to use a linked server to work with PostgreSQL data, just as if you were working with data stored in a SQL Server table:
- In Microsoft SQL Server Management Studio, connect to the SQL Server instance you want to create the linked server against.
You need to log on with an account that is a member of the SQL Server
sysadmin
fixed server role to create a linked server. - Right-click Server Objects. From the pop-up menu, choose New > Linked Server.
- In the Linked server box, enter
POSTGRESQL
. - From the Provider list, choose Microsoft OLE DB Provider for ODBC Drivers.
- In the Data source box, enter the name of your Postgresql ODBC data source, and then choose OK.
- Enter your PostgreSQL user name and password in the spaces provided. Choose OK.
SQL Server verifies the linked server by testing the connection.
- If you get the error "Specified driver could not be loaded due to system error 126: The specified module could not be found," choose Yes when prompted whether to keep the linked server. You need to restart your SQL Server instance before you can use the linked server. If SQL Server was already running when you installed the PostgreSQL ODBC driver, it will not have the latest version of the System
PATH
environment variable. The PostgreSQL ODBC driver Setup program adds entries for the driver to the SystemPATH
. Restarting the instance makes these changes available to SQL Server, allowing it to load the PostgreSQL ODBC driver. - If you made a mistake when specifying the PostgreSQL ODBC driver data source, you get the error "Data source name not found and no default driver specified." If you get this error, choose No when prompted whether to keep the linked server and edit the value in the Data source box.
- If you get the error "Specified driver could not be loaded due to system error 126: The specified module could not be found," choose Yes when prompted whether to keep the linked server. You need to restart your SQL Server instance before you can use the linked server. If SQL Server was already running when you installed the PostgreSQL ODBC driver, it will not have the latest version of the System
- You can query your PostgreSQL data either by using a:
- Four part table name in a distributed query.
A four part table name has the format:
server_name.[database_name].[schema_name].table_name
For PostgreSQL you need to omit the database name and schema. For example:
SELECT * from POSTGRESQL...employees
- Pass-through query in an
OPENQUERY
function. For example:SELECT * FROM OPENQUERY(POSTGRESQL, 'SELECT * FROM employees')
SQL Server sends pass-through queries as uninterpreted query strings to the PostgreSQL ODBC driver. This means that SQL Server does not apply any kind of logic to the query or try to estimate what that query will do.
- Four part table name in a distributed query.
SQL Server Integration Services (SSIS)
These instructions assume that you have Microsoft Visual Studio and SQL Server Data Tools for Visual Studio installed.
- Create a
.csv
file named postgresql.csv with the data your want to insert into PostgreSQL. - In Visual Studio, create a new Integration Services Project.
- Drag a Data Flow Task from the Toolbox to the Control Flow tab.
- Choose the Data Flow tab.
- Drag a Flat File Source from the Toolbox to the Data Flow tab, and then press Return.
Flat File Source is under the Other Sources list.
- In the Flat File Source Editor, choose New.
- In the Flat File Connection Manager Editor dialog box, browse for your
.csv
file. - In the text qualifier box, enter the column delimiter you used in your
.csv
file. - In the Error Output section, change the truncation action for description to
Ignore failure
. Choose OK. - Drag an ODBC Destination from the Toolbox to the Data Flow tab, and then press Return.
ODBC Destination is in the Other Destinations list.
- Select the Flat File Source. Drag the blue arrow over to the ODBC Destination.
- Select the ODBC Destination, and then press Return.
- In the ODBC Destination dialog box, choose New.
- In the Configure ODBC Connection Manager dialog box, choose New.
- In the Connection Manager dialog box, choose your PostgreSQL ODBC data source, and then use the OK button to return to the ODBC Source dialog box.
- In the Name of the table or the view list, enter
film
. Choose Mappings and then choose OK. - Choose the Start button to insert the records from
postgresql.csv
file into PostgreSQL.