{"id":2538,"library":"ibm-db","title":"IBM DB2 Python Driver","description":"ibm_db is a Python DBI driver that provides an interface for accessing IBM DB2 (LUW, zOS, iSeries, Cloud Pak for Data) and Informix databases. It implements a subset of the Python DB API 2.0 specification. As of version 3.2.8, it supports Python 3.6 and above and is actively maintained with several releases per year.","status":"active","version":"3.2.8","language":"python","source_language":"en","source_url":"https://github.com/ibmdb/python-ibmdb","tags":["database","db2","ibm","driver","sql","informix"],"install":[{"cmd":"pip install ibm-db","lang":"bash","label":"Install ibm-db"}],"dependencies":[],"imports":[{"symbol":"ibm_db","correct":"import ibm_db"}],"quickstart":{"code":"import ibm_db\nimport os\n\n# Connection details (replace with your actual DB2/Informix details)\n# For security, use environment variables in production.\ndatabase = os.environ.get('IBM_DB_DATABASE', 'SAMPLE')\nhostname = os.environ.get('IBM_DB_HOSTNAME', 'localhost')\nport = os.environ.get('IBM_DB_PORT', '50000')\nprotocol = os.environ.get('IBM_DB_PROTOCOL', 'TCPIP')\nuid = os.environ.get('IBM_DB_UID', 'db2inst1')\npwd = os.environ.get('IBM_DB_PWD', 'password')\n\nconn_string = f\"DATABASE={database};HOSTNAME={hostname};PORT={port};PROTOCOL={protocol};UID={uid};PWD={pwd};\"\n\ntry:\n    # Connect to the database\n    # Note: User/password can be passed separately or embedded in conn_string.\n    # Empty strings for user/pwd are allowed if already in conn_string.\n    conn = ibm_db.connect(conn_string, \"\", \"\")\n    print(\"Connection to DB2 successful!\")\n\n    # Execute a query\n    stmt = ibm_db.exec_immediate(conn, \"SELECT * FROM SYSCAT.TABLES FETCH FIRST 5 ROWS ONLY\")\n\n    # Fetch results\n    result = ibm_db.fetch_both(stmt)\n    while result:\n        print(result)\n        result = ibm_db.fetch_both(stmt)\n\n    # Close the connection\n    ibm_db.close(conn)\n    print(\"Connection closed.\")\n\nexcept Exception as e:\n    print(f\"Connection or query failed: {e}\")","lang":"python","description":"Connects to an IBM DB2 database using environment variables for credentials and executes a simple query to fetch table information."},"warnings":[{"fix":"Download and install the IBM Data Server Driver for ODBC and CLI appropriate for your operating system before running `pip install ibm-db`.","message":"On Windows and Linux, `ibm-db` requires the IBM Data Server Driver for ODBC and CLI to be pre-installed on the system. The `pip install` command only installs the Python bindings. This external driver is typically downloaded from IBM's support portal.","severity":"breaking","affected_versions":"All versions on Windows/Linux"},{"fix":"Refer to the `ibm-db` documentation for platform-specific environment variable configurations. Restart your terminal/IDE after setting them.","message":"After installing the IBM Data Server Driver for ODBC and CLI, you may need to set environment variables such as `IBM_DB_HOME` (pointing to the driver's installation directory) or update `LD_LIBRARY_PATH` (Linux) / `DYLD_LIBRARY_PATH` (macOS) to ensure the Python driver can locate the native libraries.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully review the database connection details provided by your DBA or cloud provider. Test connectivity with a native CLI tool if available.","message":"Connection strings for DB2/Informix databases can be complex. Ensure all parameters (DATABASE, HOSTNAME, PORT, PROTOCOL, UID, PWD) are correctly specified. Common issues include incorrect port numbers or hostname resolutions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install `pip install ibm-db-sa` if you plan to use `ibm-db` with SQLAlchemy or other ORMs.","message":"For ORM integration (e.g., SQLAlchemy), `ibm-db` alone is often not enough. You typically need the `ibm-db-sa` package (SQLAlchemy adapter for ibm-db) in addition to `ibm-db`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2.0':39 '3.2.8':44 '3.6':48 'access':17 'activ':53 'api':38 'cloud':23 'data':26 'databas':29,60 'db':6,37 'db2':2,19,61 'dbi':10 'driver':4,11,63 'ibm':1,5,18,62 'implement':31 'informix':28,65 'interfac':15 'iseri':22 'luw':20 'maintain':54 'pak':24 'per':58 'provid':13 'python':3,9,36,47 'releas':57 'sever':56 'specif':40 'sql':64 'subset':33 'support':46 'version':43 'year':59 'zos':21","created_at":"2026-04-11T01:32:20.381721+00:00","updated_at":"2026-04-16T15:40:11.080921+00:00","problems":[{"fix":"Run `pip install ibm-db` to install the package. If using a specific Python version, use `pip3 install ibm-db`.","cause":"The `ibm_db` Python package is not installed or not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'ibm_db'"},{"fix":"Ensure the `IBM_DB_HOME` environment variable is set to the `clidriver` directory (e.g., `C:\\IBM\\clidriver`), and its `bin` subdirectory is included in the system's PATH. Alternatively, in Python code, add `os.add_dll_directory(os.getenv('IBM_DB_HOME') + '/bin')` before importing `ibm_db`.","cause":"On Windows, this error occurs when the underlying IBM Data Server Driver for ODBC and CLI (clidriver) DLLs (like `db2cli.dll`) are not found in the system's PATH or a location discoverable by Python.","error":"ImportError: DLL load failed: The specified module could not be found."},{"fix":"Install necessary development packages specific to your OS (e.g., `sudo apt-get install build-essential python3-dev` on Debian/Ubuntu, `sudo yum install gcc python3-devel` on RedHat, or Visual C++ build tools on Windows). If you have an existing Db2 CLI driver, set the `IBM_DB_HOME` environment variable to its installation path before running `pip install ibm-db --no-binary :all: --no-cache-dir`.","cause":"This installation error often indicates missing build tools (like GCC, `python-devel` on Linux/macOS, or Visual C++ build tools on Windows) required to compile the native components of `ibm_db`, or issues with the automatic download of the `clidriver`.","error":"ERROR: Command errored out with exit status 1: ... during pip install ibm-db"},{"fix":"Carefully verify all connection string parameters (hostname, port, database name, username, password). Check network connectivity between the client and the database server, including firewall rules and VPN configurations. Ensure the Db2 or Informix server is running and configured to accept remote connections on the specified port.","cause":"This Db2 SQL error indicates a failure to establish a connection to the database server, commonly due to incorrect connection parameters in the connection string, network issues (firewall, VPN), or the database server not running or being inaccessible.","error":"SQL30081N A communication error has been detected."},{"fix":"Review and correct the object name in your SQL query. Confirm that the object exists in the specified schema on the database server. Verify that the user credentials used for the database connection have sufficient permissions to access the object.","cause":"This SQL error signifies that an SQL statement attempted to access a database object (e.g., table, view, or function) that either does not exist, is misspelled, or the connecting user lacks the necessary privileges to access it within the current schema.","error":"DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=<object-name> not found"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.3.0","cli_name":"","cli_version":null,"type":"library","homepage":"http://pypi.python.org/pypi/ibm_db/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/ibm-db/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}