Convert IBM DB2 to PostgreSQL

A free online converter that translates IBM DB2 queries into PostgreSQL. Paste your SQL, press Translate, and Query Studio rewrites the syntax that differs between the two databases — instantly, with no login and nothing stored.

DB2 is closer to the SQL standard than most, so a surprising amount carries over untouched — FETCH FIRST works in PostgreSQL as written. The changes cluster around date keywords, the dummy table, and identifier case folding running in the opposite direction.

Open in the editor →All Query Studio tools

IBM DB2PostgreSQL example

Here is a real IBM DB2 query and the PostgreSQL output Query Studio produces:

IBM DB2 input
SELECT u.id, u.name, COUNT(o.id) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at > '2024-01-01' AND u.active = 1
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 3
ORDER BY orders DESC
FETCH FIRST 10 ROWS ONLY;
PostgreSQL output
SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.created_at > '2024-01-01'
  AND u.active = 1
GROUP BY
  u.id,
  u.name
HAVING
  COUNT(o.id) > 3
ORDER BY
  orders DESC
LIMIT
  10

What changes from IBM DB2 to PostgreSQL

  • Row limiting is converted from IBM DB2's FETCH FIRST n ROWS ONLY to PostgreSQL's LIMIT n, including any OFFSET for pagination.
  • The result is re-indented and keyword-cased in PostgreSQL style so it's ready to paste and run.

IBM DB2 to PostgreSQL data type mapping

Query Studio translates queries, not schemas. When you come to move the tables themselves, this is what changes between IBM DB2 and PostgreSQL:

Data type equivalents from IBM DB2 to PostgreSQL
IBM DB2PostgreSQLWatch out for
GENERATED ALWAYS AS IDENTITYGENERATED ALWAYS AS IDENTITYSame standard syntax — one of the few things that carries over unchanged.
VARCHAR(n) FOR BIT DATABYTEADirect equivalent.
TIMESTAMPTIMESTAMP / TIMESTAMPTZDirect equivalent.
DECFLOATNUMERICPostgres has no decimal-floating-point type; NUMERIC is arbitrary-precision and the closest match.
GRAPHIC / VARGRAPHICTEXTDirect equivalent.
FETCH FIRST n ROWS ONLYLIMIT nPostgres also accepts FETCH FIRST, so this one can be left alone if you prefer the standard form.

IBM DB2 to PostgreSQL: what actually catches people out

Syntax is the part a translator can fix. These are the differences that survive a clean conversion and show up later as wrong results rather than as errors.

FETCH FIRST already works in PostgreSQL

DB2's FETCH FIRST n ROWS ONLY is the SQL-standard spelling, and PostgreSQL supports it directly. Converting it to LIMIT is optional — the translator does it because LIMIT is what Postgres code usually looks like, but leaving the standard form is equally valid and more portable.

The dummy table is named differently

DB2 requires a FROM clause on every SELECT and provides SYSIBM.SYSDUMMY1 for the purpose. PostgreSQL allows a bare SELECT with no FROM at all, so those references simply disappear.

IBM DB2
SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1;
PostgreSQL
SELECT CURRENT_DATE;

Date arithmetic keywords differ

DB2 writes CURRENT DATE and CURRENT TIMESTAMP as two words and supports arithmetic like `date + 1 MONTH`. PostgreSQL uses CURRENT_DATE with an underscore and interval literals: `date + INTERVAL '1 month'`.

Identifiers fold to upper case, not lower

DB2 uppercases unquoted identifiers; PostgreSQL lowercases them. A schema migrated verbatim ends up with everything renamed, which is fine as long as it happens consistently — and painful if half the code quotes identifiers and half does not.

What this converter will not do

Query Studio translates SQL syntax. Being honest about the boundary is more useful than claiming there isn’t one — and on IBM DB2PostgreSQL specifically, these are the three that matter most:

Stored procedures, functions and triggers

Procedural code — PL/pgSQL, T-SQL procedures, MySQL routines — is a different language in every engine, with different control flow, error handling, variable declaration and transaction semantics. Query Studio translates queries, not programs. These have to be ported by hand.

Anything that depends on data rather than syntax

Whether a value fits the target type, whether a date is real, whether a text column's contents are valid UTF-8 — none of that is visible in the query. A translation can be syntactically perfect and still fail on the first row of the import.

Vendor-specific extensions

PostGIS geometry, MySQL spatial functions, SQL Server's FOR XML and hierarchyid, BigQuery's nested/repeated model, Snowflake's time travel. Where there is no equivalent concept, there is no translation — only a redesign.

The full list of what a syntax translator cannot do is on the Query Studio page.

How to convert IBM DB2 to PostgreSQL

  1. Open the Query Studio editor and choose IBM DB2 as the “From” dialect.
  2. Choose PostgreSQL as the “To” dialect.
  3. Paste your IBM DB2 query and press Translate — copy the PostgreSQL result.

Try it with your own query

The editor is preloaded with IBM DB2PostgreSQL. You can also explain, format, validate and analyze the result.

Convert IBM DB2 to PostgreSQL now →

Working with the data rather than the schema? Open a large CSV, JSON or Parquet file and query it with SQL — no upload, no row limit, and files far past what Excel will open.

IBM DB2 to PostgreSQL FAQ

Is this IBM DB2 to PostgreSQL converter free?

Yes — it's completely free with no account, no sign-up and no usage limits. Your query is processed to return the result and never stored.

Is the IBM DB2 to PostgreSQL conversion accurate?

Query Studio rewrites syntax deterministically using real SQL parsers, so it gives the same result every time — there is no AI involved and no variation between runs. It handles the differences listed above automatically. What it cannot do is anything semantic: stored procedures, triggers, vendor extensions and performance characteristics all need a human. Review complex, vendor-specific queries before running them in production.

What breaks when migrating from IBM DB2 to PostgreSQL?

The differences that most often cause problems on this pair: FETCH FIRST already works in PostgreSQL; The dummy table is named differently; Date arithmetic keywords differ; Identifiers fold to upper case, not lower. Each is explained in full above, with before-and-after examples where seeing it is quicker than reading about it.

How do IBM DB2 data types map to PostgreSQL?

The full mapping table is above and covers 6 types. The ones that are not a straight rename: GENERATED ALWAYS AS IDENTITY → GENERATED ALWAYS AS IDENTITY, DECFLOAT → NUMERIC, FETCH FIRST n ROWS ONLY → LIMIT n. Note that Query Studio translates queries rather than schemas — the table is there to tell you what your CREATE TABLE statements need, not to rewrite them for you.

Does this IBM DB2 to PostgreSQL converter use AI?

No. Every result is computed by real SQL parsers and rule engines, which is what makes it free, instant, unlimited and identical on every run. Nothing is sent to a model, so there are no rate limits, no per-request cost to pass on, and no possibility of a confidently wrong answer that looks plausible.

Other conversions

MySQL to PostgreSQLPostgreSQL to MySQLSQL Server to PostgreSQLPostgreSQL to SQL ServerMySQL to SQL ServerSQL Server to MySQL