A Rust interface for connecting to IBM DB2 databases (z/OS, LUW, i) with connection pooling support using r2d2.
- Quick Start
- Prerequisites
- Installation
- Configuration
- Running Examples
- Documentation
- Troubleshooting
- License Requirements
- Contributing
If you're new to Rust and want to get started quickly:
- Install Prerequisites (15 minutes)
- Run Setup Utility (5-10 minutes)
- Configure Environment (5 minutes)
- Run First Example (2 minutes)
Total time: ~30 minutes to your first database connection!
Before starting, make sure you have installed:
- Rust (version 1.45 or newer) β Install Rust
- Git β Install Git
- A C compiler:
- Windows: Microsoft Visual Studio Build Tools
- Linux: GCC or Clang (usually pre-installed)
- macOS: Xcode Command Line Tools
Open a terminal/command prompt and run:
rustc --version # Should show Rust version β₯ 1.45
git --version # Should show Git versionIf either command fails, follow the installation links above.
Depending on your operating system, you'll need to install additional libraries. These contain the necessary files for database connections.
# Option 1: Using Chocolatey (recommended)
choco install openssl
# Option 2: Manual installation
# Download from: https://slproweb.com/products/Win32OpenSSL.html
# Choose "Win64 OpenSSL v3.0.x" (for 64-bit Windows)β ODBC is included: Windows comes with ODBC development files pre-installed, so you don't need to install anything extra!
# Install OpenSSL and ODBC development files
sudo apt-get update
sudo apt-get install -y libssl-dev unixodbc-dev# Install OpenSSL and ODBC development files
sudo yum install -y openssl-devel unixODBC-devel# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install OpenSSL and ODBC
brew install openssl unixodbc
# Set up environment variables (add to your shell profile)
echo 'export LDFLAGS="-L/usr/local/opt/openssl/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/usr/local/opt/openssl/include"' >> ~/.zshrc
source ~/.zshrcCheck that your system dependencies are installed:
Windows (Command Prompt):
openssl versionLinux/macOS (Terminal):
openssl versionYou should see output like: OpenSSL 3.0.x ...
# Clone the repository (or download the zip file)
git clone https://github.com/ibmdb/rust-ibm_db.git
cd rust-ibm_dbThe setup utility will automatically download the correct driver for your system. Simply run:
cargo run --bin setupWhat happens:
- β Detects your operating system (Windows / Linux / macOS)
- β Detects your system architecture (32-bit / 64-bit)
- β Downloads the appropriate IBM DB2 CLI Driver
- β Extracts the files to your system
- β Sets up environment variables (see next step)
Expected output:
Setting up IBM DB2 CLI Driver...
Downloading CLI Driver for your platform...
[Download progress...]
Installation complete! IBM_DB_HOME is now set to: /path/to/clidriver
Let's make sure everything was installed correctly:
Windows (Command Prompt):
echo %IBM_DB_HOME% # Should print the path
dir %IBM_DB_HOME% # Should show many filesLinux/macOS (Terminal):
echo $IBM_DB_HOME # Should print the path
ls -la $IBM_DB_HOME # Should show many filesBoth commands should show output. If you see errors, check Troubleshooting.
Now that the CLI driver is installed, you need to install the Rust crate that provides the Rust API.
Option A: Install from crates.io (recommended)
cargo install ibm_dbOption B: Install from current repository
cargo install --path .Why two options?
- Option A: Always gets the latest published version from crates.io
- Option B: Installs from the repo you cloned, useful if you're modifying the code
β Installation complete! You now have:
- β IBM DB2 CLI Driver installed
- β ibm_db Rust crate installed
- β Environment variables configured
You ONLY need setup.rs if:
- β The CLI driver is NOT yet installed
- β You're setting up on a fresh machine
You DON'T need setup.rs if:
- β CLI driver is already installed
- β You're using a project that already has the driver
- β You're installing from crates.io
After the setup utility completes, your environment variables should be configured. However, you may need to set them manually in a new terminal session. Follow the steps for your operating system:
Temporary (current session only):
set IBM_DB_HOME=C:\IBM\IBM_DATA_SERVER_DRIVER\clidriver
set PATH=%IBM_DB_HOME%\bin;%PATH%Permanent (recommended):
- Press
Win + RBreakto open System Properties - Click Environment Variables
- Click New (under System variables)
- Add two variables:
- Name:
IBM_DB_HOME| Value:C:\IBM\IBM_DATA_SERVER_DRIVER\clidriver - Name:
PATH| Value:C:\IBM\IBM_DATA_SERVER_DRIVER\clidriver\bin;(prefix to existing PATH)
- Name:
- Click OK and restart your terminal
Temporary (current session):
export IBM_DB_HOME=/IBM/IBM_DATA_SERVER_DRIVER/clidriver
export LD_LIBRARY_PATH=$IBM_DB_HOME/bin:$LD_LIBRARY_PATHPermanent (recommended):
# Add to ~/.bashrc or ~/.zshrc
echo 'export IBM_DB_HOME=/IBM/IBM_DATA_SERVER_DRIVER/clidriver' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=$IBM_DB_HOME/bin:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrcTemporary (current session):
export IBM_DB_HOME=/IBM/IBM_DATA_SERVER_DRIVER/clidriver
export DYLD_LIBRARY_PATH=$IBM_DB_HOME/bin:$DYLD_LIBRARY_PATHPermanent (recommended):
# For Zsh (default in modern macOS)
echo 'export IBM_DB_HOME=/IBM/IBM_DATA_SERVER_DRIVER/clidriver' >> ~/.zshrc
echo 'export DYLD_LIBRARY_PATH=$IBM_DB_HOME/bin:$DYLD_LIBRARY_PATH' >> ~/.zshrc
source ~/.zshrc
# OR for Bash
echo 'export IBM_DB_HOME=/IBM/IBM_DATA_SERVER_DRIVER/clidriver' >> ~/.bash_profile
echo 'export DYLD_LIBRARY_PATH=$IBM_DB_HOME/bin:$DYLD_LIBRARY_PATH' >> ~/.bash_profile
source ~/.bash_profileWhen you connect to a database, you'll need a connection string. This tells the driver where to find your database.
Connection String Format:
DRIVER={IBM DB2 ODBC DRIVER};DATABASE=mydb;HOSTNAME=server.com;PORT=50000;UID=myuser;PWD=mypassword
Breaking it down:
DRIVER={IBM DB2 ODBC DRIVER}β Always use this (it's the driver name)DATABASE=mydbβ Your database nameHOSTNAME=server.comβ Your server addressPORT=50000β Your database portUID=myuserβ Your usernamePWD=mypasswordβ Your password
Example with real values:
let connection_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=SAMPLE;HOSTNAME=db.mycompany.com;PORT=60000;UID=admin;PWD=secretpass123";To use the CLI driver in your cargo commands, you must set the IBM_DB_HOME environment variable:
Windows (Command Prompt):
cargo run --package ibm_db --example connectIBM_DB_HOME should already be set from Configuration section
Linux/macOS (Terminal):
IBM_DB_HOME=. cargo run --package ibm_db --example connectOr use the path where you installed the clidriver
Windows:
cargo run --package ibm_db --example connectLinux/macOS:
IBM_DB_HOME=. cargo run --package ibm_db --example connectYou'll be prompted to enter a SQL query. Try:
SELECT * FROM SYSCAT.TABLESThe project includes many examples to learn from:
| Example | What it does |
|---|---|
connect |
Basic database connection |
list_tables |
Lists all tables in a database |
execute_query |
Runs SQL queries |
bind_params |
Uses parameterized queries (safe for user input) |
transaction_control |
Demonstrates transactions (commit/rollback) |
data_types |
Shows how to work with different DB2 data types |
error_handling |
Proper error handling patterns |
connect_pool |
Connection pooling for performance |
Run any example:
Windows:
cargo run --package ibm_db --example <example_name>Linux/macOS:
IBM_DB_HOME=. cargo run --package ibm_db --example <example_name>Example commands:
# Windows
cargo run --package ibm_db --example connect
cargo run --package ibm_db --example list_tables
# Linux/macOS
IBM_DB_HOME=. cargo run --package ibm_db --example connect
IBM_DB_HOME=. cargo run --package ibm_db --example list_tables- Complete API Reference: https://docs.rs/ibm_db/
- Crates.io Page: https://crates.io/crates/ibm_db
- GitHub Repository: https://github.com/ibmdb/rust-ibm_db
- Examples Folder: See
examples/directory in this repository
cargo new my_db_app
cd my_db_appEdit Cargo.toml and add:
[dependencies]
ibm_db = "1.0.6"Edit src/main.rs:
use ibm_db::environment::Environment;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an ODBC environment
let env = Environment::new()?;
// Connect to the database
let connection_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=SAMPLE;HOSTNAME=your.server.com;PORT=60000;UID=youruser;PWD=yourpass";
let conn = env.connect_with_connection_string(connection_string)?;
println!("β
Connected to database!");
Ok(())
}Windows:
REM Make sure IBM_DB_HOME is set (from Configuration section)
cargo runLinux/macOS:
IBM_DB_HOME=. cargo runYou should see: β
Connected to database!
This project contains several key parts (don't worry if you don't understand all of this yet):
| Component | Purpose | Example Use |
|---|---|---|
| Setup Utility | Downloads and installs the DB2 driver | Run once: cargo run --bin setup |
| Connection Module | Opens/manages database connections | Used internally by functions |
| ODBC Interface | Communicates with the database | Used internally by functions |
| Statement Processor | Executes SQL queries | execute_query.rs example |
| Error Handler | Shows detailed error messages | When something goes wrong |
| Connection Pooling | Reuses connections efficiently | For advanced applications |
- ODBC: The "universal language" that lets Rust talk to database drivers
- OpenSSL: Encryption library for secure communication
- IBM DB2 CLI Driver: The translator between ODBC and the actual DB2 database
RUST should be installed (Rust version should be >=1.45) Confirm by typing below in command prompt:
>rustc --version
GIT should be installed Confirm by typing below in command prompt:
>git --version
- OpenSSL: Download and install from https://slproweb.com/products/Win32OpenSSL.html (or use
choco install openssl) - ODBC Development Files: Included with Windows SDK
# Install OpenSSL development files
sudo apt-get install libssl-dev
# Install ODBC development files
sudo apt-get install unixodbc-dev# Install OpenSSL development files
sudo yum install openssl-devel
# Install ODBC development files
sudo yum install unixODBC-devel# Install OpenSSL using Homebrew
brew install openssl
# Install ODBC (if not already installed)
brew install unixodbcAfter installing OpenSSL on macOS, you may need to set environment variables:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"OpenSSL handles the security aspects of your database connection. It ensures:
- π Encryption: Your data is scrambled in transit
- π Authentication: The server proves it's really who it claims to be
- β Security: Prevents eavesdropping and man-in-the-middle attacks
Windows:
openssl version -a
where opensslLinux/macOS:
openssl version -a
pkg-config --cflags --libs opensslIf you see error messages, go back to the System Dependencies section.
| β Problem | π Reason | β Solution |
|---|---|---|
undefined reference to 'SSL_CTX_new' |
OpenSSL not installed | Install OpenSSL (see System Dependencies) |
library not found for -lssl (macOS) |
OpenSSL in wrong location | Run: export LDFLAGS="-L/usr/local/opt/openssl/lib" |
libssl.so.1.1: cannot open shared object |
Missing runtime library | Ubuntu: sudo apt-get install libssl1.1 |
| β Error | π§ What to Try |
|---|---|
"libssl-1_1-x64.dll not found" |
Install OpenSSL: https://slproweb.com/products/Win32OpenSSL.html |
"IBM DB2 ODBC DRIVER not found" |
Set IBM_DB_HOME and PATH (see Configuration section) |
"The setup.rs crashed/hung" |
Close the terminal and try again. Check internet connection. |
| β Error | π§ What to Try |
|---|---|
"libssl.so.1.1: cannot open shared object file" |
Run: sudo apt-get install libssl1.1 |
"unixodbc-dev not found" |
Run: sudo apt-get install unixodbc-dev |
"LD_LIBRARY_PATH not persisting" |
Add export statements to ~/.bashrc and run: source ~/.bashrc |
| β Error | π§ What to Try |
|---|---|
"dyld: Library not loaded: libdb2.dylib" |
Set DYLD_LIBRARY_PATH before running (see Configuration section) |
"library not found for -lssl" |
Set: export LDFLAGS="-L/usr/local/opt/openssl/lib" |
"Homebrew OpenSSL not found" |
Run: brew install openssl and set LDFLAGS/CPPFLAGS |
Still have issues? Try these steps:
- Close and reopen your terminal β Updates to environment variables won't appear until you do this
- Verify each step β Run the verification commands in the Configuration section
- Check internet connection β The setup utility needs to download the CLI Driver (100+ MB)
- Look for typos β Environment variable paths are case-sensitive on Linux/macOS
- Ask for help β Visit https://github.com/ibmdb/rust-ibm_db/issues
You can connect to these without additional licenses:
- β DB2 for Linux/Unix/Windows (LUW)
- β DB2 on premises
- β IBM Cloud Db2
Connecting to these requires purchased licenses:
β οΈ DB2 for z/OS (mainframe)β οΈ DB2 for i (AS/400)
What to do if you need a license:
- Contact IBM Customer Support
- Purchase DB2 Connect Unlimited for your platform
- Copy the license file to:
$IBM_DB_HOME/license/
We welcome contributions! Whether you find a bug, want to add a feature, or improve documentation:
- Report Issues: https://github.com/ibmdb/rust-ibm_db/issues
- Submit Pull Requests: https://github.com/ibmdb/rust-ibm_db/pulls
- Read Guidelines: See CONTRIBUTING.md
When contributing, please include:
- What problem you're solving
- How to test your changes
- Any new dependencies (with versions)
All contributions require a sign-off referencing the DCO:
DCO 1.1 Signed-off-by: Your Name <your.email@example.com>
rust-ibm_db/
βββ src/
β βββ lib.rs # Main library file
β βββ connection.rs # Database connection logic
β βββ statement/ # SQL statement execution
β βββ environment/ # ODBC environment setup
β βββ diagnostics.rs # Error handling
β βββ ffi.rs # Low-level C bindings
βββ src/bin/
β βββ setup.rs # CLI Driver download utility
βββ examples/
β βββ connect.rs # Basic connection example
β βββ execute_query.rs # Run SQL example
β βββ bind_params.rs # Parameterized queries
β βββ ... # More examples
βββ build.rs # Build script (compiles C code)
βββ Cargo.toml # Project dependencies
βββ README.md # This file
- The Rust Book - Official Rust guide
- Rust by Example - Learn by doing
- Rustlings - Micro-lessons
- IBM DB2 Documentation
- ODBC Documentation
- SQL Tutorial - Learn SQL basics
- API Documentation - Full function reference
- GitHub Repository - Source code
- Examples Folder - Real code examples
A: You need basic Rust knowledge. Check out The Rust Book for a quick introduction.
A: The setup utility automates this, but if it fails: Check your internet, ensure you have 500MB free disk space, and see Troubleshooting section.
A: Yes! Ensure you install OpenSSL via Homebrew and set the LDFLAGS/CPPFLAGS environment variables.
A: No, if you make the changes permanent (see Configuration section), they'll persist.
A: That's fine! Just include the port in your connection string (e.g., PORT=52000).
A: Never hardcode passwords! Use environment variables:
let password = std::env::var("DB_PASSWORD")?;
let conn_str = format!("DRIVER={{IBM DB2 ODBC DRIVER}};...;PWD={}", password);- ibm_db (Python) - IBM DB2 for Python
- ibm_db_nodejs - IBM DB2 for Node.js
- ibm_db_go - IBM DB2 for Go
This project is licensed under the Apache License 2.0. See LICENSE file for details