Skip to main content

Dump any supported source database into a PostgreSQL SQL file

Project description

CI status on main (Python 3.9–3.14 + functional) PyPI Docs Cover Python Code Style Pre-Commit License

db2sql is a Python package providing a command-line utility to move any supported source database (SQLite, MySQL, MSSQL, PostgreSQL, Oracle) into a target dialect — PostgreSQL (default) or Microsoft SQL Server — selectable via --target.

Two output modes are supported:

  • Dump mode (default) — write a SQL file (or stream to stdout) that can later be replayed with psql -f or sqlcmd -i.

  • Migrate mode — open a live connection to the target database and apply the same DDL and data directly, without an intermediate file. The DDL produced is byte-identical to dump mode: a single SqlEmitter is the source of truth in both paths.

Installation

db2sql is compatible with Python 3.9+.

Use pip to install the latest stable version. Note the distribution name on PyPI is python-db2sql while the importable Python package is db2sql (same convention as python-dateutil):

$ pip install --upgrade python-db2sql

After installation, the CLI is available as db2sql and the importable module as db2sql:

from db2sql.interface.cli import main

The current development version is available on GitHub.com and can be installed directly from the git repository:

$ pip install git+https://github.com/sismicfr/python-db2sql.git

Live migration mode

Stream a source database directly into a live target — same DDL as the file dump, but without the round-trip through a .sql file:

# SQLite source  live Postgres target
$ db2sql --driver sqlite --dbname mydb.sqlite migrate \
    --target-host localhost --target-port 5432 \
    --target-dbname mytarget --target-user postgres --target-password s3cr3t

The migrate subcommand uses the SqlEmitter of the chosen --target to produce DDL and a dialect-specific TargetWriter (e.g. psycopg2.copy for Postgres, batched executemany for MSSQL) to bulk-load rows. See the CLI reference for all --target-* flags and migration options (--on-existing, --transaction-mode, --batch-size).

Replayable dumps

By default, the dump emits CREATE TABLE statements only — replaying the file against a database that already contains the target tables fails. Pass --on-existing drop (or set dump.on_existing: drop in the config) to prepend a DROP TABLE IF EXISTS for every table in reverse-dependency order:

$ db2sql --driver sqlite --dbname mydb.sqlite --on-existing drop -f dump.sql

Pass --on-existing truncate to produce a data-only script: no DDL is emitted, the dump just TRUNCATEs every managed table and reloads its rows. Use it to refresh data into a pre-existing schema:

$ db2sql --driver sqlite --dbname mydb.sqlite --on-existing truncate -f refresh.sql

Validating a configuration

Before launching a long dump, check the configuration file and (optionally) preview the export plan without producing any SQL:

# syntax check + plugin name resolution (no DB connection)
$ db2sql validate db2sql.yml

# connect to the source and print the plan, no SQL emitted
$ db2sql validate db2sql.yml --dry-run

# same plan plus one SELECT COUNT(*) per kept table
$ db2sql validate db2sql.yml --dry-run --with-counts

See the CLI reference for full details, exit codes, and the lookup order when the positional CONFIG_FILE is omitted.

Extensibility

Beyond the built-in drivers (SQLite, MySQL, MSSQL, PostgreSQL, Oracle) and targets (PostgreSQL, MSSQL), db2sql discovers third-party plugins through three entry-point groups:

  • db2sql.readers — register a new source driver (--driver)

  • db2sql.emitters — register a new target dialect for the file dump (--target)

  • db2sql.writers — register a new target writer for live migration (used by db2sql migrate)

A step-by-step authoring guide lives in the Plugins section of the documentation, and three runnable example projects ship under examples/:

  • examples/csv-producer — a custom reader (a directory of CSVs)

  • examples/sqlite-emitter — a custom emitter (SQLite-flavoured SQL)

  • examples/yaml-to-markdown — a single package that ships both a reader and an emitter

Each example is a standalone Python distribution: cd examples/<name> && pip install -e . makes its driver / target immediately usable from the db2sql CLI.

Bug reports

Please report bugs and feature requests at https://github.com/sismicfr/python-db2sql/issues.

Documentation

The full documentation for CLI and API is available on readthedocs.

Build the docs

We use tox to manage our environment and build the documentation:

pip install tox
tox -e docs

Contributing

For guidelines for contributing to db2sql, refer to CONTRIBUTING.rst.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_db2sql-0.1.0.tar.gz (60.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_db2sql-0.1.0-py3-none-any.whl (88.8 kB view details)

Uploaded Python 3

File details

Details for the file python_db2sql-0.1.0.tar.gz.

File metadata

  • Download URL: python_db2sql-0.1.0.tar.gz
  • Upload date:
  • Size: 60.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_db2sql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d8e4bc2ace3e95cd69f338ff0244e85060dddb96b460a38b668309e68c276606
MD5 09fabfc20f03852d5f7d58af244b25b8
BLAKE2b-256 4cc341b40667fe64d58559d48e17134041b35f16a45fa789c181cfb1fb9f47ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_db2sql-0.1.0.tar.gz:

Publisher: release.yml on sismicfr/python-db2sql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_db2sql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_db2sql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 88.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_db2sql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a08a6a173333981169af14693aab1d9e7e587b5dbd302c95383a11068e35a41c
MD5 5c5b5e43a7aac4871c508189f7172a48
BLAKE2b-256 622aaac7c030165c5173cc733e57f9eaea7d494b577c0107f6490f9717e8f4e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_db2sql-0.1.0-py3-none-any.whl:

Publisher: release.yml on sismicfr/python-db2sql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page