{"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":"en","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,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}