简体   繁体   中英

Unable to connect remote Postgresql server from Ubuntu by ODBC

I have two machines:

  1. Windows, running PostgreSQL
  2. Ubuntu linux

I need to connect from Linux machine to the PostgreSQL on Windows, using C++ code by ODBC library.

I installed ODBC libraries and PostgreSQL ODBC driver on Linux by running the command:

sudo apt-get install unixodbc unixodbc-dev odbc-postgresql

I edited /etc/odbc.ini, added the following DSN:

[Mydb]
Driver          = PostgreSQL Unicode
Description     = Test DSN
Servername      = 192.168.11.1
Port            = 5432
Database        = Mydb
UserName        = postgres
Password        = ***

I'm trying to connect PostgreSQL from Linux by the following C++ code:

SQLDriverConnectA(..., "Mydb", SQL_NTS, "postgres", SQL_NTS, "****", SQL_NTS);

And this code works fine.

But I need a solution without adding any DSN ("DSN-less") . I changed my C++ code this way:

SQLCHAR* cs = "DRIVER=PostgreSQL Unicode; Server=192.168.11.1; Port=5432; Database=Mydb; UID=postgres; PWD=****";
SQLDriverConnectA(..., cs, ...);

Run on Linux machine, this code gives me an error:

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?

The error message looks strange, because I'm trying to connect a remote server, not a local one.

How to fix the DSN-less connection on Linux? I tried "Servername" and "Hostname" instead of "Server" in connection string, but it did not help.

PS. Run on Windows machine, DNS-less code works fine.

PPS. The contents of /etc/odbcinst.ini

[PostgreSQL ANSI]
Description=PostgreSQL ODBC driver (ANSI version)
Driver=psqlodbca.so
Setup=libodbcpsqlS.so
Debug=0
CommLog=1
UsageCount=1

[PostgreSQL Unicode]
Description=PostgreSQL ODBC driver (Unicode version)
Driver=psqlodbcw.so
Setup=libodbcpsqlS.so
Debug=0
CommLog=1
UsageCount=1

The solution is to remove all spaces from the connection string:

DRIVER={PostgreSQL Unicode};Server=dell;Port=5432;Database=Mydb;UID=postgres;PWD=***

Without spaces, it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM