Home:ALL Converter>Connect to IBM DB2 on Cloud using python api ibm_db

Connect to IBM DB2 on Cloud using python api ibm_db

Ask Time:2021-07-21T02:56:28         Author:Ashis Sahoo

Json Formatter

Can you please let me know how to connect to DB2 on IBM Cloud using python?

I have tried the below steps.

  1. installed ibm_db using pip install ibm_db
  2. Created a free tier Db2 service on IBM cloud
  3. Generated Service credentials key
  4. Trying to establish a connection with the database with the credential details (Database, host, port, user id, and password) extracted from the Service credentials key
import ibm_db

print("Creating connection.......")

conn_string = "DATABASE=bludb;HOSTNAME=54a2f15b-5c0f-46df-8954-7e38e612c2bd.c1ogj3sd0tgtu0lqde00.databases.appdomain.cloud;PORT=32733;PROTOCOL=TCPIP;UID=<userId>;PWD=<password>;"

conn = ibm_db.connect(conn_string,"","")
if conn:
    print("Connection ...... [SUCCESS]")
else:
    print("Connection ...... [FAILURE]")

I am getting below error message:

SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001

Author:Ashis Sahoo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68460039/connect-to-ibm-db2-on-cloud-using-python-api-ibm-db
data_henrik :

It seems like you are on the new Db2 on Cloud lite plan with non-standard ports and SSL enforced. When you connect to Db2 using the Python driver and use SSL, you have to add the SECURITY=SSL property, e.g.:\nconn_string = "DATABASE=bludb;HOSTNAME=yourhostname;PORT=<port>;PROTOCOL=TCPIP;UID=<userId>;PWD=<password>;SECURITY=SSL"\n",
2021-07-21T12:31:21
Wizard74 :

I think the easy way to do this is with SQL Magic. This way, you can just type a SQL statement just by adding %sql before your query\nFirst install the packages\n!pip install sqlalchemy==1.3.9\n!pip install ibm_db_sa\n\nThen,\n%load_ext sql\n\nFinally, run the following code by replacing your username, password, hostname and SSL. You can find these in your IBM DB2 under credentials.\n%sql ibm_db_sa://my-username:my-password@hostname:port/BLUDB?security=SSL \n\nNow you can run any SQL by using %sql before or %%sql if the whole cell is going to be SQL. Example:\n%sql SELECT * FROM TABLENAME;\n\nor\n%%sql \nSELECT *\nFROM TABLENAME;\n",
2021-07-29T20:42:29
yy