NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / DOCKER RECIPES / IMPORT FROM A POSTGRESQL CONTAINER

Import from a PostgreSQL container

Copy a PostgreSQL database, schema and rows, into a new RasterizedDB database, then serve it. Use it to move an application's data across, or to try RasterizedDB on a copy of real data. The recipe brings a seeded PostgreSQL 17 container so it runs as is; point it at your own PostgreSQL to migrate for real.

What you get

  • source: PostgreSQL 17 with a database shop, seeded from seed.sql (two tables, a foreign key, NUMERIC, JSONB, TIMESTAMPTZ).
  • import: a one-shot RasterizedDB container that reads shop and builds a new RasterizedDB database shop in the server's volume, then exits.
  • rasterizeddb: the server, serving the imported database.

The import reads the schema first, then every row of every table, then puts back the primary keys, unique constraints and foreign keys. It is a one-time copy: it does not keep the two in sync, and it will not import into a database that already exists.

Prerequisites

  • Docker with the Compose plugin.
  • psql to check the result.

What to edit

  1. Copy .env.example to .env and set RASTERIZEDDB_PG_PASSWORD (the new server's role rasterized) and SOURCE_PASSWORD (the source's user postgres).
  2. To import your own PostgreSQL instead of the sample, change the import service's --pg-import-from URI and PGPASSWORD, and remove the source service and its depends_on. The source has to be reachable from the container. The URI takes the libpq form, and the import's flags override what it says:
FlagDefaultWhat it does
--pg-import-fromnoneSource connection URI, as in postgresql://user@host:5432/dbname?sslmode=require
--pg-import-intononeName of the new RasterizedDB database. Setting it runs the import.
--pg-import-host, --pg-import-port, --pg-import-user, --pg-import-database$PGHOST/localhost, $PGPORT/5432, $PGUSER/postgres, $PGDATABASEThe connection, field by field
--pg-import-password$PGPASSWORDPrefer the environment variable, as here
--pg-import-sslmodepreferdisable, prefer, require or verify-full. require encrypts without checking the server; across any untrusted network use verify-full.
--pg-import-schemapublicThe one schema to read
--pg-import-only-tables, --pg-import-exclude-tablesall tablesComma-separated table lists
--pg-import-batch-rows5000Rows per transaction while loading
--pg-import-jsonautojson/jsonb columns become JSONB where every document is an object, TEXT elsewhere. text or native force one or the other.
--pg-import-wide-numericfailA numeric(p, s) finer than 9 decimal places fails the import; text imports it as TEXT
--pg-import-without-constraintsoffLeave out primary keys, unique constraints and foreign keys
--pg-import-dry-runoffReport what would be imported and what would not come across unchanged, creating nothing
--pg-import-and-serveoffStart the server after the import instead of exiting

The sample's source has TLS off, as the official postgres image does by default, so the URI says sslmode=disable. That is acceptable only on the private Compose network.

Run it

  1. Start the source and wait for it to report healthy:

    sh
    docker compose up -d source
    docker compose ps
  2. Optional: a dry run, which reports the plan and any type that will change, and creates nothing:

    sh
    docker compose run --rm import \
      --pg-import-from 'postgresql://postgres@source:5432/shop?sslmode=disable' \
      --pg-import-into shop --pg-import-dry-run
  3. Import. The server must not be running, because the import writes into its data volume:

    sh
    docker compose stop rasterizeddb
    docker compose run --rm import

    It prints a report of every table and the number of rows it copied, and exits.

  4. Start the server:

    sh
    docker compose up -d rasterizeddb

Check that it works

Connect to the new database shop:

sh
psql "host=127.0.0.1 port=5432 user=rasterized dbname=shop sslmode=require" \
  -c "SELECT COUNT(*) FROM customers" \
  -c "SELECT o.id, c.name, o.total FROM orders o JOIN customers c ON c.id = o.customer_id ORDER BY o.id"

The first query counts 3 customers. The second returns the three orders, 100 and 101 for Ada Lovelace and 102 for Grace Hopper, with their totals.

Point your application at dbname=shop on the RasterizedDB server, run its test suite, and compare a few row counts with the source before you switch over.

Importing into a running server

A server that is already running can import with a statement instead, without being stopped. Connect to it and run:

sql
IMPORT DATABASE FROM POSTGRES
    HOST 'source' PORT 5432 DATABASE 'shop'
    USER 'postgres' PASSWORD 'the source password' SSLMODE 'disable'
    INTO shop_copy;

The statement takes every option the flags do: SCHEMA '<name>', ONLY TABLES (a, b), EXCLUDE TABLES (c), BATCH <rows>, JSON AS TEXT|NATIVE, WIDE NUMERIC AS FAIL|TEXT, WITHOUT CONSTRAINTS, DRY RUN. The password is replaced by <redacted> in the server's statement statistics.

Stop and clean up

sh
docker compose down        # keeps both volumes
docker compose down -v     # deletes the imported data and the source's data

Limits

  • One schema per import. Run one import per schema, each into its own database.
  • Sequences, views and materialized views are not imported.
  • timestamp, timestamptz and date all become TIMESTAMP (in UTC; a date at midnight). Types with no counterpart (enums, interval, time, ranges, geometry, and others) become TEXT holding PostgreSQL's own rendering. Every array becomes TEXT[]. The dry run lists each such change.
  • A value that would not be stored exactly fails the import, naming the table, row and column, rather than being changed silently.
  • Full details are on Import from PostgreSQL.

The files

compose.yaml · Docker ComposeDOWNLOAD ↓
# Moving a PostgreSQL database into RasterizedDB. `source` is a PostgreSQL 17
# container seeded from seed.sql; `import` copies its schema and rows into a
# new RasterizedDB database and exits; `rasterizeddb` then serves the result.
# Copy .env.example to .env first. Steps are in README.md.
name: rasterizeddb-import

services:
  source:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: ${SOURCE_PASSWORD:?set SOURCE_PASSWORD in .env}
      POSTGRES_DB: shop
    volumes:
      - source-data:/var/lib/postgresql/data
      - ./seed.sql:/docker-entrypoint-initdb.d/seed.sql:ro
    healthcheck:
      # Over TCP, so it passes only once the real server is up (the image's
      # first-run setup listens on a local socket only).
      test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "postgres", "-d", "shop"]
      interval: 5s
      timeout: 5s
      retries: 24

  # One-shot: docker compose run --rm import
  # It uses the server's data volume, so the server must be stopped while it
  # runs (a running server holds the directory's lock and the import refuses).
  import:
    image: ${RASTERIZEDDB_IMAGE:-milendenev/rasterizeddb:1.1.0}
    profiles: ["import"]
    environment:
      RASTERIZEDDB_PG_PASSWORD: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
      # The source's password, read by the import (kept out of the URI and
      # the command line).
      PGPASSWORD: ${SOURCE_PASSWORD:?set SOURCE_PASSWORD in .env}
    command:
      - --pg-import-from
      - postgresql://postgres@source:5432/shop?sslmode=disable
      - --pg-import-into
      - shop
    volumes:
      - data:/var/lib/rasterizeddb
    depends_on:
      source:
        condition: service_healthy

  rasterizeddb:
    image: ${RASTERIZEDDB_IMAGE:-milendenev/rasterizeddb:1.1.0}
    environment:
      RASTERIZEDDB_PG_PASSWORD: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
    command: ["--log-level", "warn"]
    ports:
      - "127.0.0.1:5432:5432"
      - "127.0.0.1:61170:61170"
    volumes:
      - data:/var/lib/rasterizeddb
    restart: unless-stopped
    stop_grace_period: 60s
    healthcheck:
      test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/5432"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s

volumes:
  data:
  source-data:
.env.example · Environment fileDOWNLOAD ↓
# Copy to .env next to compose.yaml and change both passwords.

# The password of the RasterizedDB role `rasterized`, used on its first start.
RASTERIZEDDB_PG_PASSWORD=change-me-to-a-long-random-password

# The password of the PostgreSQL user `postgres` in the source container.
SOURCE_PASSWORD=change-me-too

# The image and tag. Pin a full version.
RASTERIZEDDB_IMAGE=milendenev/rasterizeddb:1.1.0
seed.sql · SQLDOWNLOAD ↓
-- Sample data for the PostgreSQL source. The postgres image runs this once,
-- when its volume is first initialised. Replace it with your own dump, or
-- delete the mount in compose.yaml and point the import at your database.

CREATE TABLE customers (
    id         BIGINT PRIMARY KEY,
    email      TEXT NOT NULL UNIQUE,
    name       TEXT NOT NULL,
    vip        BOOLEAN NOT NULL DEFAULT false,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE orders (
    id          BIGINT PRIMARY KEY,
    customer_id BIGINT NOT NULL REFERENCES customers (id),
    total       NUMERIC(10, 2) NOT NULL,
    details     JSONB NOT NULL
);

INSERT INTO customers (id, email, name, vip) VALUES
    (1, 'ada@example.com',   'Ada Lovelace',  true),
    (2, 'alan@example.com',  'Alan Turing',   false),
    (3, 'grace@example.com', 'Grace Hopper',  true);

INSERT INTO orders (id, customer_id, total, details) VALUES
    (100, 1, 19.99, '{"items": 2, "channel": "web"}'),
    (101, 1,  5.00, '{"items": 1, "channel": "store"}'),
    (102, 3, 42.50, '{"items": 3, "channel": "web"}');
✓ page rendered in 132 µs axum + tera + htmx · no other javascript