Skip to content

db2_fdw

foreign data wrapper for DB2 access

Overview

PackageVersionCategoryLicenseLanguage
db2_fdw18.2.0FDWPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
8630db2_fdwNoYesNoYesNoNo-

Latest PGDG RPM/catalog version is 18.2.0; no DEB package is available.

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG18.2.01817161514db2_fdw-
RPMPGDG18.2.01817161514db2_fdw_$v-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64N/AN/AN/AN/AN/A
el9.x86_64
el9.aarch64N/AN/AN/AN/AN/A
el10.x86_64
el10.aarch64N/AN/AN/AN/AN/A
d12.x86_64N/AN/AN/AN/AN/A
d12.aarch64N/AN/AN/AN/AN/A
d13.x86_64N/AN/AN/AN/AN/A
d13.aarch64N/AN/AN/AN/AN/A
u22.x86_64N/AN/AN/AN/AN/A
u22.aarch64N/AN/AN/AN/AN/A
u24.x86_64N/AN/AN/AN/AN/A
u24.aarch64N/AN/AN/AN/AN/A
u26.x86_64N/AN/AN/AN/AN/A
u26.aarch64N/AN/AN/AN/AN/A

Install

You can install db2_fdw directly. First, make sure the PGDG repository is added and enabled:

pig repo add pgdg -u          # Add PGDG repo and update cache

Install the extension using pig or apt/yum/dnf:

Install
pig install db2_fdw;          # Install for current active PG version
pig
pig ext install -y db2_fdw -v 18  # PG 18
pig ext install -y db2_fdw -v 17  # PG 17
pig ext install -y db2_fdw -v 16  # PG 16
pig ext install -y db2_fdw -v 15  # PG 15
pig ext install -y db2_fdw -v 14  # PG 14
dnf
dnf install -y db2_fdw_18       # PG 18
dnf install -y db2_fdw_17       # PG 17
dnf install -y db2_fdw_16       # PG 16
dnf install -y db2_fdw_15       # PG 15
dnf install -y db2_fdw_14       # PG 14

Create Extension:

CREATE EXTENSION db2_fdw;

Usage

Sources:

db2_fdw 18.2.0 queries and modifies IBM Db2 tables through PostgreSQL foreign tables. It pushes down supported filters and only the required columns. Upstream requires PostgreSQL 10.1 or later and an IBM Db2 client 11.1 or later with the same architecture as PostgreSQL. The server process must be able to load that client and access the configured database; installing this extension does not supply the external client or a Db2 server.

Connect and Import Tables

The example assumes that the Db2 client can connect to the catalogued SAMPLE database and that DB2INST1.EMPLOYEE exists. Install the extension as a superuser, then create the server and a role-specific mapping. Use the actual Db2 credentials instead of the example password:

CREATE EXTENSION db2_fdw;
CREATE SERVER db2srv FOREIGN DATA WRAPPER db2_fdw
  OPTIONS (dbserver 'SAMPLE');
CREATE USER MAPPING FOR CURRENT_USER SERVER db2srv
  OPTIONS (user 'db2inst1', password 'change-me');
CREATE SCHEMA db2_remote;
IMPORT FOREIGN SCHEMA "DB2INST1" LIMIT TO ("EMPLOYEE")
  FROM SERVER db2srv INTO db2_remote;
SELECT empno, firstname, lastname, salary
FROM db2_remote.employee
WHERE empno = '000010';

Importing obtains the Db2 column metadata required by the wrapper. Default smart case folding lowercases all-uppercase names. A separate application role needs USAGE on the foreign server, its own user mapping, and appropriate schema/table privileges. Avoid a PUBLIC mapping when credentials are meant for one role. Empty user/password strings select the upstream external-authentication path and depend on the Db2 client environment.

Options and Writes

  • Server dbserver selects the Db2 connection; no_encoding_error controls handling of encoding-conversion errors. batch_size is reserved in this release and should not be treated as working batch insertion.
  • Table schema and table identify the remote object; readonly prohibits modifications. prefetch defaults to 100 and accepts 0–1024; fetch_size is accepted but currently fixed to 1. sample_percent controls ANALYZE sampling.
  • Column key must identify every remote primary-key column for UPDATE and DELETE. Imported metadata includes db2type, db2size, db2bytes, db2chars, db2scale, db2null and db2ccsid; preserve it when altering imported tables.
  • IMPORT FOREIGN SCHEMA options case and readonly control name folding and whether imported tables permit writes.

INSERT, UPDATE and DELETE additionally require Db2-side privileges. Review column mapping and remote key definitions before enabling writes; PostgreSQL declarations alone do not create a remote primary key. Unsupported filters are evaluated locally, so inspect EXPLAIN before assuming pushdown.

Diagnostics and Transactions

SELECT db2_diag();
SELECT db2_diag('db2srv');
SELECT db2_close_connections();

db2_diag() reports local client/build diagnostics and optionally remote-server details. db2_close_connections() closes connections cached by the current backend; do not call it within a transaction that has modified Db2 data. Long-lived PostgreSQL sessions can retain remote connections and transaction resources.

Types and Maintenance

Common mappings include character types to text/character, BLOB to bytea, integer types to PostgreSQL integer types, and DATE/TIMESTAMP/TIME to their corresponding types. The declared PostgreSQL type and width must accommodate the remote values; failed conversions surface at query time. The control version is 18.2.0 and is relocatable. Normal use requires no shared preload. Follow the versioned README for IBM client environment and connectivity requirements, and distinguish a package upgrade from validation against the actual external database.

Was this page helpful?