通過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]#


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章