zoukankan      html  css  js  c++  java
  • 通过unixODBC访问PostgreSQL数据库

    磨砺技术珠矶,践行数据之道,追求卓越价值
    回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页
    [作者 高健@博客园  luckyjackgao@gmail.com]

    步骤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("/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]#                     

     

    [作者 高健@博客园  luckyjackgao@gmail.com]
    回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页
    磨砺技术珠矶,践行数据之道,追求卓越价值

  • 相关阅读:
    .Net 平台兼容性分析器
    编程中常见的Foo,是什么意思?
    SoC里住着一只“猫” 网络性能全靠它【转】
    Linux内核:VFIO Mediated Device(vfio-mdev)内核文档(翻译)【转】
    vfio-mdev逻辑空间分析【转】
    29. secure world对smc请求的处理------monitor模式中的处理【转】
    一步步教你:如何用Qemu来模拟ARM系统【转】
    2. [mmc subsystem] mmc core数据结构和宏定义说明【转】
    OP-TEE驱动篇----驱动编译,加载和初始化(一)【转】
    Forkjoin线程池
  • 原文地址:https://www.cnblogs.com/gaojian/p/3327790.html
Copyright © 2011-2022 走看看