Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2012: Add a linked server to PostgreSQL

I try to connect a PostgreSQL to SQL Server 2012 server as linked server

I found some advices on different forum and follow it. But I'm stuck with an authentication problem.

I explain:

On the SQL Server, I have install the ODBC driver for PostgreSQL (psqlodbc_09_02_0100-x64). I created a system DSN to a specific database on the PostgreSQL. This DSN work correctly.

Using SSMS, I run this command to add the linked server:

EXEC master.dbo.sp_addlinkedserver 
@server = N'lnk_test', 
@srvproduct=N'PostgreSQL Unicode(x64)', 
@provider=N'MSDASQL', 
@provstr=N'Driver=PostgreSQL Unicode(x64);uid=postgres;Server=test1;database=accueil;pwd=MyPassword;SSLmode=disable;PORT=5432'

EXEC master.dbo.sp_addlinkedsrvlogin 
@rmtsrvname=N'lnk_test',
@useself=N'True',
@locallogin=NULL,
@rmtuser=NULL,
@rmtpassword=NULL

After running, I have a new Linked Server called "lnk_test". When I receive the following error message:

Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "pgTest1_accueil".
OLE DB provider "MSDASQL" for linked server "pgTest1_accueil" returned message "[Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed".
OLE DB provider "MSDASQL" for linked server "pgTest1_accueil" returned message "FATAL: authentification par mot de passe échouée pour l'utilisateur  « postgres »". (Microsoft SQL Server, Error: 7303)

The error message is in french, the traduction is: "authentication by password failed for user "postgres". I find the same error message in the log of the PostgreSQL server.

Is someone have an idea for resolving this problem?

Thanks in advance.

Jerome

[Update 2/08/2013] I complete this post with the result of a test I realize today.

WHen using a packet sniffer, I check the packets sended when testing the connection through the ODBS Data Source Administrator (64-bit) and through the Linked Server under SSMS.

The data are the same between the 2 system for:

  • Opening the connection to the PostgreSQL

  • Sending the connection string (all parameters are the same)

  • the response of PostgreSQL asking for password (only different is the salt value, but it's normal)

  • the password are sended in the same way (crypted with MD5)

But after, the response of the server differ:

For the ODBC Data Source, all works correctly. I receive the authentication and can send test query.

For SSMS, I receive an error message (as describe upper).

like image 775
Jérôme Duchêne Avatar asked Aug 01 '13 16:08

Jérôme Duchêne


People also ask

How do I create a linked server from SQL Server to PostgreSQL?

Create Linked ServerOpen SQL Server management studio 🡪 Connect to SQL Server instance 🡪 In object explorer, expand Server Objects 🡪 Right-click on Linked Server and select New Linked Server. A dialog box New Linked Server opens. To configure the linked server, specify the following details on the General page.

Can SQL Server connect to PostgreSQL?

You can use the Microsoft SQL Server Management Studio to connect your PostgreSQL data to an SQL Server instance. Linked Server is a tool of MS SQL Server that allows to execute distributed queries to refer tables stored on non-SQL Server datbase in a single query.

How do I create a Dblink in PostgreSQL?

Load the dblink extension into PostgreSQL. CREATE EXTENSION dblink; Create a persistent connection to a remote PostgreSQL database using the dblink_connect function specifying a connection name (myconn), database name (postgresql), port (5432), host (hostname), user (username) and password (password).


1 Answers

Ok, I found the solution.

In the pg_hba.conf file, I change the method for sending passwords from MD5 to trust. After reloading the server, my linked server connection works.

the entry is now:

Type Database User Address   Method

host all      all  x.x.x.x/x trust

In hope that help others peoples.

like image 56
Jérôme Duchêne Avatar answered Sep 20 '22 08:09

Jérôme Duchêne