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 databaseshop, seeded fromseed.sql(two tables, a foreign key,NUMERIC,JSONB,TIMESTAMPTZ).import: a one-shot RasterizedDB container that readsshopand builds a new RasterizedDB databaseshopin 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.
psqlto check the result.
What to edit
- Copy
.env.exampleto.envand setRASTERIZEDDB_PG_PASSWORD(the new server's rolerasterized) andSOURCE_PASSWORD(the source's userpostgres). - To import your own PostgreSQL instead of the sample, change the
importservice's--pg-import-fromURI andPGPASSWORD, and remove thesourceservice and itsdepends_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:
| Flag | Default | What it does |
|---|---|---|
--pg-import-from | none | Source connection URI, as in postgresql://user@host:5432/dbname?sslmode=require |
--pg-import-into | none | Name 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, $PGDATABASE | The connection, field by field |
--pg-import-password | $PGPASSWORD | Prefer the environment variable, as here |
--pg-import-sslmode | prefer | disable, prefer, require or verify-full. require encrypts without checking the server; across any untrusted network use verify-full. |
--pg-import-schema | public | The one schema to read |
--pg-import-only-tables, --pg-import-exclude-tables | all tables | Comma-separated table lists |
--pg-import-batch-rows | 5000 | Rows per transaction while loading |
--pg-import-json | auto | json/jsonb columns become JSONB where every document is an object, TEXT elsewhere. text or native force one or the other. |
--pg-import-wide-numeric | fail | A numeric(p, s) finer than 9 decimal places fails the import; text imports it as TEXT |
--pg-import-without-constraints | off | Leave out primary keys, unique constraints and foreign keys |
--pg-import-dry-run | off | Report what would be imported and what would not come across unchanged, creating nothing |
--pg-import-and-serve | off | Start 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
-
Start the source and wait for it to report healthy:
shdocker compose up -d source docker compose ps -
Optional: a dry run, which reports the plan and any type that will change, and creates nothing:
shdocker compose run --rm import \ --pg-import-from 'postgresql://postgres@source:5432/shop?sslmode=disable' \ --pg-import-into shop --pg-import-dry-run -
Import. The server must not be running, because the import writes into its data volume:
shdocker compose stop rasterizeddb docker compose run --rm importIt prints a report of every table and the number of rows it copied, and exits.
-
Start the server:
shdocker compose up -d rasterizeddb
Check that it works
Connect to the new database shop:
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:
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
docker compose down # keeps both volumes
docker compose down -v # deletes the imported data and the source's dataLimits
- One schema per import. Run one import per schema, each into its own database.
- Sequences, views and materialized views are not imported.
timestamp,timestamptzanddateall becomeTIMESTAMP(in UTC; adateat midnight). Types with no counterpart (enums,interval,time, ranges, geometry, and others) becomeTEXTholding PostgreSQL's own rendering. Every array becomesTEXT[]. 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
# 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:# 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-- 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"}');