通过unixODBC访问PostgreSQL数据库

步骤1,先安装PostgreSQL

具体不详述,参考PostgreSQL的官方文档或下载、解压后的INSTALL文件中的指令即可

运行configure时指定安装目录:./configure --prefix=/usr/local/pgsql

我安装的是 PostgreSQL9.1.2

 

步骤2,再安装unixODBC

下载 unixODBC-2.3.1.tar.gz,以postgres 用户身份进行解压。

然后运行:./configure --prefix=/usr/local/unixodbc

然后 make

       sudo make install

 

步骤3,再安装psqlodbc

一开始安装的是psqlodbc-09.01.0200.tar.gz,make的时候,发现和unixODBC有冲突。

查阅文档,解决方法是-----换版本!

http://postgresql.1045698.n5.nabble.com/Compile-error-08-04-0200-with-unix-odbc-2-2-14p2-1-on-Debian-Squeeze-AMD64-td3244665.html

所以,下载  psqlodbc-09.02.0100.tar.gz

以postgres用户身份解压缩,然后运行:

export LD_LIBRARY_PATH=/usr/local/unixodbc/lib:$LD_LIBRARY_PATH

./configure --prefix=/usr/local/psqlodbc --with-libpq=/usr/local/pgsql/ --with-unixodbc=/usr/local/pgsql/unixodbc/

再运行:

     make

     sudo make install

 

步骤4,配置unixODBC 

[root@server etc]# pwd
/usr/local/unixodbc/etc
[root@server etc]# ls
ODBCDataSources  odbc.ini  odbcinst.ini
[root@server etc]# cat odbcinst.ini
[PostgreSQL]
Description=PostgreSQL driver for Linux
Driver=/usr/local/psqlodbc/lib/psqlodbcw.so
Setup=/usr/local/psqlodbc/lib/psqlodbcw.so
UsageCount=1




[root@server etc]# cat odbc.ini
[GaoTest]
Description=Test for Mr Gao
Driver=PostgreSQL
Trace=Yes
TraceFile=sql.log
Database=postgres
Servername=localhost
UserName=postgres
Password=postgres
Port=5432
Protocol=6.4
ReadOnly=No
RowVersioning=No
ShowSystemTables=No
ShowOidColumn=No
FakeOidIndex=No
ConnSettings=
[root@server etc]#

步骤5,验证

通过isql来验证是否可以连接到postgresql,

运行中通过strace跟踪发现,isql需要在几个目录中寻找libpq.so.5

open("/usr/local/unixodbc/lib/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/lib64/tls/x86_64/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/tls/x86_64", 0x7fff7fdcd0b0) = -1 ENOENT (No such file or directory)
open("/lib64/tls/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/tls", 0x7fff7fdcd0b0)      = -1 ENOENT (No such file or directory)
open("/lib64/x86_64/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/x86_64", 0x7fff7fdcd0b0)   = -1 ENOENT (No such file or directory)
open("/lib64/libpq.so.5", O_RDONLY)     = -1 ENOENT (No such file or directory)
stat("/lib64", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
open("/usr/lib64/tls/x86_64/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/tls/x86_64", 0x7fff7fdcd0b0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/tls/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib64/x86_64/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/x86_64", 0x7fff7fdcd0b0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/libpq.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64", {st_mode=S_IFDIR|0755, st_size=106496, ...}) = 0
于是在lib64目录下建立到 libpq.so.5的链接:
[root@server lib64]# pwd                        
/lib64                        
[root@server lib64]# 

[root@server lib64]# ls -lrt libpq.so.5                        
lrwxrwxrwx 1 root root 31  9月 17 17:48 libpq.so.5 -> /usr/local/pgsql/lib/libpq.so.5 
然后再次运行isql:
[root@server etc]# cd /usr/local/unixodbc/bin                    
[root@server bin]# ./isql -v GaoTest                    
+---------------------------------------+                    
| Connected!                            |                    
|                                       |                    
| sql-statement                         |                    
| help [tablename]                      |                    
| quit                                  |                    
|                                       |                    
+---------------------------------------+                    
SQL> quit                    
[root@server bin]#


你可能感兴趣的:(PostgreSQL,unixODBC)