To run any SQL or perform runstats or other actions, you must establish a connection to a DB2 database.

Connecting to a Database

Connecting to an existing database on the same database server the database exists on, or connecting to an already cataloged database is fairly simple. For more on cataloging databases, see /how-to-catalog-a-db2-database/

Command Line

Linux/UNIX

On Linxux and UNIX systems, the ID that you want to use to connect must source the SQLLIBdb2profile for the DB2 instance that the database is on before it can run DB2 commands. This is usually accomplished by lines like the following in the .profile, .bash_profile, or .bashrc file:

. <INSTHOME>/sqllib/db2profile



where INSTHOME is the home directory of the db2 instance owner. This line can also be executed at the command line to source the db2profile only for the current session or to change which DB2 instance you are working with. If you are logged in as the db2 instance owner, this part should already be done.

If you have not properly sourced the db2 profile, you will get an error like this for every db2 command attempted:

-bash: db2: command not found 


Once your command line is properly set up, you can list the database names available (local and remote) using this command:

$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 3

Database 1 entry:

 Database alias                       = TEST
 Database name                        = TEST
 Local database directory             = /db2home/db2inst1
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = BCUDB
 Database name                        = BCUDB
 Node name                            = BCU_PRIM
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Remote
 Catalog database partition number    = -1
 Alternate server hostname            =
 Alternate server port number         =

Database 3 entry:

 Database alias                       = SAMPLE
 Database name                        = SAMPLE
 Local database directory             = /db2home/db2inst1
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =



You can connect to any of the local (Directory entry type = Indirect) databases listed in the output using syntax like this:

db2 connect to <database_alias>



An example of that is

$ db2 connect to SAMPLE

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.4
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE


If you want to connect to one of the databases listed in the database directory that has a diretory entry type of “Remote”, you must usually specify a user name and password. The general form of this syntax is:

db2 connect to <database_alias> user <user_id>



and it will then prompt you for the password. This looks like:

$ db2 connect to BCUDB user kjaneway
Enter current password for kjaneway: 

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.4
 SQL authorization ID   = KJANEWAY
 Local database alias   = BCUDB


Windows

The same commands used above are used at a windows command prompt, but you must pull up the right command prompt to use DB2 commands. You can do this by selecting “DB2 Command Window” from the IBM DB2 menu from the start menu:
Screenshot_102115_030207_PM

Data Studio

If you’re using IBM Data Studio, simply right-click on the database name in the left pane and select “Connect”:
Screenshot_102115_030605_PM
You will likely have to specify a user name and password.

Share This