How to connect Node.js to IBM DB2 database - windows

moyarich profile image moyarich Updated on ・2 min read

It is good to connect with the right people. For months now I thought that we had to pay over 30k for the license to use DB2 with node. Today I contacted the right person at IMB. I sent a message to the main person on the node team through LinkedIn, he messaged me back right away and told me about the right tools to use. In less than 10 minutes, guess who connected node to DB2? Me.

I swear I have been googling this for months. Maybe I was typing in the wrong search terms because I wasn't finding what I needed, I kept seeing information about ibm-db.

So if you come across this issue and you want to use IBM DB2 with node, what you need is one out of the following node modules. By the way, none of them have licensing fees:

Choose one of the following (in order of preference):

Note: The odbc option is most flexible and works outside of the i-series environment, on operating systems such as Windows and Linux.

  • odbc

  • idb-pconnector

  • idb-connector

Note: These package only installs on IBM i systems.

Connect Node to a DB2 Database

First of all, you will need a connection string

*If you have used php in the past *, your connection string might look like this:

$dbconn = odbc_connect('usrProd', 'username1', 'password1', SQL_CURSOR_FORWARD_ONLY)

** But in node your connection string is like this**

const cn = "DRIVER=IBM i Access ODBC Driver;SERVER=host;UID=user;PWD=password;DATABASE=dbname"

If you already have a DNS setup on your system, you can shorten the connection syntax like so, by providing the DSN information and the username and password

const cn = "DSN=datasourceName;UID=username;PWD=password";

You can learn more about how to install the odbc driver and setup the DSN by visiting IBM/ibmi-oss-examples on github

Sample code to connect Node to DB2

const odbc = require("odbc");

const cn = "DSN=usrProd;UID=username1;PWD=password1";

odbc.connect(cn, (error, connection) => {
    (error, result) => {
      if (error) {
        throw error;

% post moyarich/how-to-connect-node-js-to-ibm-db2-database-16m6 %}


Editor guide
bimalkjha profile image
Bimal Kumar Jha

To connect a Db2 for z/OS, Db2 for AS400, Db2 for LUW or Db2 on Cloud/Dashdb or bigsql server using node.js application, you just need to install ibm_db package.

npm install ibm_db

and you are ready to go. You can install ibm_db on MacOS, Windows, Linux, zLinux, AIX, Sun, Sunamd, Linuxppc and z/OS platforms.
Check it for more info: github.com/ibmdb/node-ibm_db

bskimball profile image
Brian Kimball

What about the DB2Connect licensing mentioned in the readme? I think that was the point of using the odbc connection instead

michelc profile image

That sounds pretty good. Did you use the "odbc" package on Windows? Because I am currently connecting to DB2 with ODBC from a WinForm client, and with Node I could try to automate some queries.

moyarich profile image
moyarich Author

Ohh I sure did:

this is a github link with a massive amount of mocha test cases that you can use to practice how to run queries using node: github.com/markdirish/node-odbc-ac...

odbc datasource