IBM Support

Tech Tip: Setting up the UnixODBC driver manager and ODBC environment for DB2

Technical Blog Post


Tech Tip: Setting up the UnixODBC driver manager and ODBC environment for DB2


An ODBC driver manager is not supplied on UNIX platforms as part of the operating system. Using ODBC on UNIX systems, therefore, requires a separate commercial or open source ODBC driver manager.
The unixODBC Driver Manager is an open source ODBC driver manager for use on UNIX platforms. This driver manager is supported for ODBC applications on supported DB2® platforms.
To run ODBC applications on Linux or UNIX operating systems, you must configure the UnixODBC Driver Manager.

 Here are the steps to install and configure UnixODBC for ODBC applications connecting to DB2 database using IBM DB2 ODBC driver:
1) Download the latest unixODBC source code from here.                          
2) Untar the source files.
 For example:
gzip -d unixODBC-2.3.1.tar.gz
tar xf unixODBC-2.3.1.tar

3) For AIX® only: Configure the C compiler to be thread-enabled:
export CC=xlc_r
export CCC=xlC_r

To compile a 64-bit version of the driver manager using the xlc_r compilers, set the environment variables OBJECT_MODE and CFLAGS:
export OBJECT_MODE=64

4) Install the driver manager in either your home directory or the default /usr/local prefix:

(Home directory) Issue the following command in the directory where you untarred the source files:

./configure --prefix=$HOME -DBUILD_REAL_64_BIT_MODE --enable-gui=no --enable-drivers=no

(/usr/local as root) Issue the following command:

./configure --enable-gui=no --enable-drivers=no

Here is an example to install the UnixODBC driver manager in a DB2 instance home directory on a 32 bit Linux server:                       

Log in as the DB2 instance owner(example: db2v97) and  run  the following command from the directory where the UnixODBC source files are untarred:
   ./configure --prefix=$HOME --enable-gui=no --enable-drivers=no

5) Build and install the driver manager:

5.1)    make                                                           
5.2)    make install         
<The above steps will create the  /lib, /include, /bin for the UnixODBC driver manager under DB2 instance home directory -  /home/db2v97>
6) Add that path to the lib to LD_LIBRARY_PATH: 

export LD_LIBRARY_PATH=/home/db2v97/lib:/home/db2v97/sqllib/lib
7) Create odbc.ini and odbcinst.ini as per the documentation:

             For example:
[db2v97@myserver ~]$ more odbc.ini                          
Driver = /home/db2v97/sqllib/lib/                       
DESCRIPTION = Connection to DB2                                  
DRIVER = /home/db2v97/sqllib/lib/                       
[db2v97@myserver ~]$ more odbcinst.ini                     
Description = Default Driver                                     
Driver = /home/db2v97/sqllib/lib/                       
Description = Default Driver                                     
Driver = /home/db2v97/sqllib/lib/                       
Trace = yes                                                      
Tracefile = /home/db2v97/trc.log                                 

8) Add the absolute paths of the file location to the environment:
              For example, if the files are created under /home/db2v97:
8.1) export ODBCHOME=/home/db2v97                                  
8.2)  export ODBCINI=/home/db2v97/odbc.ini          
9) Build the application and ensure it is linked to the unixODBC Driver Manager by including the -L[prefix]/lib -lodbc option in the compile and link command.

For example, to Compile a sample conn.c program          
cc -I /home/db2v97/include conn.c -L /home/db2v97/lib -lodbc   
NOTE: For AIX and 64 bit environments, please refer the DB2 information center links below:


[{"Business Unit":{"code":"BU029","label":"Data and AI"}, "Product":{"code":"SSEPGG","label":"DB2 for Linux- UNIX and Windows"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":""}]