Creating a Linked Server for a PostgreSQL Database

This is a post on how to create a Linked Server to a PostgreSQL database.

In my previous post, I created a Linked Server to another SQL Server instance. This time, it’s a link to a Postgres database. I recommend checking out my other post for pre-requisite information including testing connectivity to the remote database server.

Follow these steps to create a Linked Server to a Postgres server:
1. Create Linked Server Login on Postgres.
2. Install the Postgres ODBC Driver.
3. Configure ODBC Data Source Settings.
4. Create Linked Server to a Postgres Database.

1. Create Linked Server Login on Postgres

We first need to create a login for the Linked Server on Postgres:

Postgres Create New User

For this example I’m making the user in Postgres a Superuser. This is not ideal, we should be configuring it with the least permissions it requires. This is just a demo though.

Postgres User Options

For demo purposes, I’m also showing how this would be done using a terminal:

psql Create and View Users

2. Install Postgres ODBC Driver

The ODBC driver must be installed on both ends. This step is a reminder to ensure you have this installed.

You can use Stack Builder to install it, or download the Postgres ODBC driver package on its own:

Postgres ODBC Driver Download

We need to install this:

psqlODBC Setup Wizard

Accept License stuff.

Specify directory & we don’t need the documentation.

psqlODBC Feature Selection

It’s a straight-forward install:

psqlODBC Install Wizard

3. Configure ODBC Data Source Settings

On our SQL Server, we need to configure the ODBC driver for the Postgres database.

To do this, open ODBC Data Sources as Administrator:

Run ODBC Source as Administrator

Click the System DSN tab and click Add.

ODBC Data Source System DSN

Select the PostgreSQL Unicode driver:

Create New Postgres ODBC Data Source

Enter all details required, In this example I created a ‘sales’ database which I’m connecting to:  

ODBC Data Source Settings for Postgres

Finally, we should test the connection to verify all is good:

ODBC Test Connection

4. Create Linked Server to a Postgres Database

To create a Linked Server to Postgres:
1.
Expand Server Objects, right-click Linked Servers and select New Linked Server.

SSMS New Linked Server

2. The New Linked Server Wizard will prompt.
Configure your settings, choose a provider for ODBC, entering the correct Data Source Name which you entered within the ODBC driver settings above in this post.

SSMS New Linked Server Postgres

3. Enter the Postgres Login Details as created above within this post in the Security tab.

SSMS Linked Server Login Details

4. We can skip the Server Options tab and hit OK.

5. Next, we should test the Linked Server connection.

SSMS Test Linked Server Connection

It should show this:

Linked Server Connection Test

We can now verify the Linked Server is working as it should by running the following query:

-- linked server query example 
select *
from [linked-server-name].[database-name].[schema-name].[table-name];
Linked Server Query with Postgres

I hope this was a good and useful post for you. Feel free to check out the links you see around here for more random useful info from a random database guy!


Comments

2 responses to “Creating a Linked Server for a PostgreSQL Database”

  1. Steve Martin Avatar

    Thank-you for this post Peter, its been more than 5 years (2015) since I last put together a linked server to Postgres and this is for access to the Tableau Server db so was concerned about updating the driver.
    For any other user, TS will install the drivers, but not an environment. Given the driver specification for TS, it is fine to skip the driver install, starting your journey from: Configure ODBC Data Source Settings.

    Steve

    1. pete Avatar
      pete

      That’s interesting to hear Steve, thank you.
      I’ve done a bunch with Tableau Desktop & Tableau Online but not yet had the pleasure of meeting Tableau Server. I had a wee look at the Tableau driver docs (https://www.tableau.com/support/drivers) and you’re right we can see from there “The Windows driver for PostgreSQL is installed by default with Tableau Server”. This should show up within Programs & Features (appwiz.cpl) as far as I know. Definitely best to check existing drivers before proceeding with an install of one.

      Cheers

Leave a Reply

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