The IBM_DB_SA adapter provides the Python/SQLAlchemy interface to IBM Data Servers.
Version
0.5.0 (2026/03/18)
Prerequisites
Install Python 3.10 to 3.15 or Jython 2.7.x .
SQLAlchemy version between 1.4.x - 2.1.x.
IBM_DB driver and IBM_DB_DBI wrapper 1.0.1 or higher.
Note: Older SQLAlchemy versions are supported by IBM_DB_SA versions up to and including 0.4.4. Starting with version 0.5.0, IBM_DB_SA supports SQLAlchemy 1.4.x through 2.1.x.
Install ibm_db driver with below commands:
Linux and Windows:
pip install ibm_db
Mac:
pip install --no-cache-dir ibm_db
Install and Configuration
The IBM_DB_SA Python Egg component (.egg) can be installed using the standard setuptools provided by the Python Easy Install through Python Entreprise
Application Kit community portal:
http://peak.telecommunity.com/DevCenter/EasyInstall
Please follow the steps provided to Install "Easy Install" in the link above and follow up with these additional steps to install IBM_DB_SA:
To install IBM_DB_SA from pypi repository(pypi.python.org):
Windows:
A TCP/IP connection can be specified as the following::
from sqlalchemy import create_engine
e = create_engine("db2+ibm_db://user:pass@host[:port]/database")
or
e = create_engine("ibm_db_sa://user:pass@host[:port]/database")
For a local socket connection, exclude the "host" and "port" portions::
from sqlalchemy import create_engine
e = create_engine("db2+ibm_db://user:pass@/database")
or
e = create_engine("ibm_db_sa://user:pass@/database")
Testing
IBM_DB_SA is tested with SQLAlchemy's pytest-based test suite for third-party
dialects, including the SQLAlchemy dialect compliance suite
(sqlalchemy.testing.suite). The tests can be run against SQLAlchemy 1.4.x,
2.0.x and 2.1.x.
Quick start, from the root of the repository:
pip install -e .
pip install -r requirements-test.txt
# tests that do not need a database:
pytest test/test_dialect.py
# all tests, against a Db2 database:
pytest --dburi "ibm_db_sa://user:pass@host:port/DBNAME" test/
For full instructions (configuring the database connection, running single
tests, testing with a specific SQLAlchemy version and investigating
failures), see README.unittests.md.
Logging in ibm_db_sa
The ibm_db_sa module provides built-in logging support to help debug SQLAlchemy dialect operations, connection handling, reflection, and SQL execution.
Logging can be enabled using the ibmdbsa_log query parameter in the database connection URL.
Logging Configuration
Use the ibmdbsa_log parameter at the end of the connection URL.
Possible values:
True -> Enable logging to the console
"filename" -> Enable logging to a file (file is overwritten on each run)
False -> Disable logging
Examples
1. Enable Logging to a File (Current Directory)
Logs will be written to a file in the current working directory.
from sqlalchemy import create_engine
engine = create_engine(
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=log_ibmdbsa.txt"
)
This will create the file:
log_ibmdbsa.txt
2. Enable Logging to a File (Specific Directory)
You can provide an absolute file path.
Windows example
from sqlalchemy import create_engine
engine = create_engine(
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=C:\\Users\\Logs\\log_ibmdbsa.txt"
)
Linux / macOS example
from sqlalchemy import create_engine
engine = create_engine(
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=/var/log/log_ibmdbsa.txt"
)
3. Enable Console Logging
Logs will be printed to the terminal or console.
from sqlalchemy import create_engine
engine = create_engine(
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=True"
)
Using SQLAlchemy URL Object
Logging can also be configured using SQLAlchemy's URL object.
Logging configuration is automatically detected when the SQLAlchemy engine is created.
The ibmdbsa_log parameter is removed internally before the connection parameters are passed to the DBAPI driver.
If logging is not specified, logging remains disabled by default.
Typical Use Cases
Logging can help diagnose:
Connection issues
SQL execution problems
Reflection metadata queries
Dialect initialization
Performance troubleshooting
Supported Databases
IBM DB2 Database for Linux/Unix/Windows versions 11.5 onwards
IBM Db2 on Cloud
IBM Db2 on ZOS
IBM Db2 on Iseries
Note
By default, all tables and schemas stored in a Db2 database are created
using capital letters only.
However, if you have a table name in lowercase letters, you can still reference
it by enclosing the name in single quotes inside double quotes.
For example
if users table is in small letter inside database
So, you can use single quotes "'users'".
If you will not use single quotes such as "users", it will
be refered as "USERS".
metadata = sqlalchemy.MetaData(schema="schema1")
table = sqlalchemy.Table("'users'", metadata, autoload_with=engine)
Known Limitations in ibm_db_sa adapter for DB2 databases
Non-standard SQL queries are not supported. e.g. "SELECT ? FROM TAB1"
For updations involving primary/foreign key references, the entries should be made in correct order. Integrity check is always on and thus the primary keys referenced by the foreign keys in the referencing tables should always exist in the parent table.
Unique key which contains nullable column not supported
UPDATE CASCADE for foreign keys not supported
DEFERRABLE INITIALLY deferred not supported
Subquery in ON clause of LEFT OUTER JOIN not supported
PyODBC support is experimental
A unique index created with EXCLUDE NULL KEYS is reflected as a UNIQUE constraint
Enum columns are created as VARCHAR(n CODEUNITS32) on Db2 LUW, which requires Db2 LUW 10.5 or later. Db2 for z/OS uses VARCHAR(n) and Db2 for i uses VARCHAR(n) CCSID 1208
UnicodeText columns are created as DBCLOB (previously LONG VARGRAPHIC, which is deprecated in Db2)
Credits
ibm_db_sa for SQLAlchemy was first produced by IBM Inc., targeting version 0.4.
The library was ported for version 0.6 and 0.7 by Jaimy Azle.
Port for version 0.8 and modernization of test suite by Mike Bayer.
Contributing to IBM_DB_SA python project
See CONTRIBUTING <https://github.com/ibmdb/python-ibmdbsa/tree/master/ibm_db_sa/contributing/CONTRIBUTING.md>_.
The developer sign-off should include the reference to the DCO in remarks(example below):
DCO 1.1 Signed-off-by: Random J Developer <random@developer.org>
Alternatives
Use instead of ibm-db-sa: tools that do the same job, closest first.