SQLMentor // learn db2

DB2 Installation & Setup

Difficulty: Beginner · ~10 min read

Overview

Db2 Community Edition is the free version of Db2 LUW that you can run locally for development and learning. It supports up to 4 CPU cores and 16 GB of memory — plenty for a study environment. You have three main installation paths:

  1. Docker — fastest and most portable (recommended for this tutorial)
  2. Native Linux install via the IBM installer
  3. Native Windows install via the IBM installer

The Docker image is the quickest way to get a working DB2 in under five minutes, with no system-wide changes.

Syntax

Two main entry points exist once Db2 is running:

# Open the DB2 Command Line Processor (interactive)
db2

# Or run a single statement from the OS shell
db2 "SELECT current_timestamp FROM sysibm.sysdummy1"

Examples

Example 1: Docker install (fastest)

# Pull the official IBM image
docker pull icr.io/db2_community/db2

# Run it (give it ~5 GB RAM, accept the license)
docker run -itd --name db2-dev --privileged=true \
  -p 50000:50000 \
  -e LICENSE=accept \
  -e DB2INST1_PASSWORD=MyStr0ngP@ss \
  -e DBNAME=SAMPLE \
  icr.io/db2_community/db2

# Wait ~3 minutes for SAMPLE to be created, then jump into the container
docker exec -it db2-dev bash -c "su - db2inst1"

# You're now the db2inst1 user. Connect:
db2 connect to SAMPLE

Output:

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.5.x.x
 SQL authorization ID   = DB2INST1
 Local database alias   = SAMPLE

Example 2: Native Linux install (Ubuntu example)

# Download Db2 Community Edition from IBM, extract, then:
cd server_dec
sudo ./db2_install

# After install, as the instance user:
db2sampl                 # creates the sample database
db2start
db2 connect to sample

Example 3: Windows install

  1. Download Db2 Community Edition for Windows from the IBM site.
  2. Run the setup.exe. Choose Typical install. Use the default instance name DB2.
  3. Open the DB2 Command Window (Start menu → IBM DB2 → Command Window — Administrator) and run:
db2 create db sample
db2 connect to sample
db2 "SELECT current_timestamp FROM sysibm.sysdummy1"

Example 4: Verifying the install

-- Inside the db2 CLP, after connecting:
SELECT service_level, fixpack_num
FROM   TABLE(sysproc.env_get_inst_info()) AS T;

Output:

SERVICE_LEVEL    FIXPACK_NUM
--------------   -----------
DB2 v11.5.8.0              0

Notes & Tips

  • The default port is 50000. If you change it, remember to use -p <host>:50000 with Docker.
  • For Docker, don't lose your container — by default, data is stored inside the container. Mount a volume (-v db2data:/database) if you want it to persist across docker rm.
  • On Windows, always open the DB2 Command Window (not a regular cmd) — it sets up the necessary environment variables.
  • If db2 connect hangs, check that the Db2 service is running: db2start (CLI) or via Services on Windows.
  • Default instance user on Docker/Linux is db2inst1 with the password you supplied via DB2INST1_PASSWORD.

Practice Exercises

  1. Start a Db2 Community Edition container, log in as db2inst1, and connect to SAMPLE.
  2. List all tables in the DB2INST1 schema using SYSCAT.TABLES.
  3. Run SELECT current_server FROM sysibm.sysdummy1 and confirm the database name matches what you connected to.

Quick Quiz

Q1. What is the default DB2 LUW listener port?

Show answer

50000. You'll typically map it to your host with -p 50000:50000 when running in Docker.

Q2. Which environment variable do you set to accept the Db2 license in the Docker image?

Show answer

LICENSE=accept. Without it the container won't start.

Q3. What command creates the sample database that ships with Db2?

Show answer

db2sampl. It populates an SAMPLE database with tables like EMPLOYEE, DEPARTMENT, and PROJECT that the IBM docs reference.

Next Up

With DB2 running, the next topic walks through every data type DB2 supports — and which one to choose.