DBTrail

Prepare your PostgreSQL

What DBTrail needs on your PostgreSQL, the cases the Quickstart leaves out, and what each check looks for. Nothing is installed in your server.

The Quickstart does this for the tables in public. This page has the same steps, the cases they leave out, and what each check looks for. Nothing is installed in your server.

You need: psql, and a login to your PostgreSQL as a superuser (postgres on most servers; on RDS and Aurora, the master user).

Copy and paste

Say where your PostgreSQL is

the DBTrail machine

Change the first two lines to your server and database. The last line asks for the postgres password once, without showing it.

export PGHOST=db.example.com
export PGDATABASE=shop
export PGUSER=postgres
export DBTRAIL_PW=$(openssl rand -hex 16)
printf 'postgres password: '; read -rs PGPASSWORD; echo; export PGPASSWORD

Run every step of this page in this terminal, and keep it open: server add reads $DBTRAIL_PW later.

Turn on logical replication

your PostgreSQL
psql -c "ALTER SYSTEM SET wal_level = 'logical'" \
     -c "ALTER SYSTEM SET max_slot_wal_keep_size = '10GB'"

PostgreSQL on the same machine as DBTrail? Also run this, so the DBTrail container can reach it (without it, server add stops with connect: connection refused):

psql -c "ALTER SYSTEM SET listen_addresses = '*'"
echo "host all all 172.16.0.0/12 scram-sha-256" | sudo tee -a "$(psql -Atc 'SHOW hba_file')"

Then restart PostgreSQL, on the machine where it runs. A reload is not enough.

sudo systemctl restart postgresql

Set rds.logical_replication = 1 in the instance's parameter group and reboot the instance: Managed PostgreSQL.

Give DBTrail a login and your tables

your PostgreSQL

It creates the dbtrail login, lets it read your tables, sets every table in the public schema to send the whole row with each change, and lists them in a publication, PostgreSQL's list of the tables it sends.

psql -v pw="$DBTRAIL_PW" <<'SQL'
CREATE ROLE dbtrail WITH LOGIN REPLICATION PASSWORD :'pw';
GRANT CONNECT ON DATABASE :"DBNAME" TO dbtrail;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO dbtrail;
DO $$
DECLARE t record; list text;
BEGIN
  FOR t IN SELECT c.oid::regclass AS tbl FROM pg_class c
           JOIN pg_namespace n ON n.oid = c.relnamespace
           WHERE n.nspname = 'public' AND c.relkind = 'r' AND NOT c.relispartition LOOP
    EXECUTE format('ALTER TABLE %s REPLICA IDENTITY FULL', t.tbl);
    list := concat_ws(', ', list, t.tbl::text);
  END LOOP;
  EXECUTE 'CREATE PUBLICATION bintrail_pub FOR TABLE ' || list;
END $$;
SQL
  • RDS and Aurora: also run psql -c "GRANT rds_replication TO dbtrail".
  • Tables in another schema: put its name in place of public, in both places, and also run GRANT USAGE ON SCHEMA <schema> TO dbtrail;.
  • PostgreSQL 18 with generated columns: read this warning first. On 18, the block can stop your application's UPDATEs on such a table.

Partitioned tables, if you have any

your PostgreSQL

The block above leaves partitioned tables out. This one sets the parent and every partition to send the whole row, then adds the table to the publication. Put your table's name in place of public.events.

psql <<'SQL'
DO $$
DECLARE t record;
BEGIN
  FOR t IN SELECT c.oid::regclass AS tbl FROM pg_class c
           JOIN pg_namespace n ON n.oid = c.relnamespace
           WHERE n.nspname = 'public'
             AND (c.relkind = 'p' OR (c.relkind = 'r' AND c.relispartition)) LOOP
    EXECUTE format('ALTER TABLE %s REPLICA IDENTITY FULL', t.tbl);
  END LOOP;
END $$;
SQL
psql -c "ALTER PUBLICATION bintrail_pub ADD TABLE public.events"

Check it

your PostgreSQL
psql <<'SQL'
SHOW wal_level;
SELECT rolname, rolreplication FROM pg_roles WHERE rolname = 'dbtrail';
SELECT DISTINCT c.oid::regclass AS not_ready
FROM pg_publication_tables p
JOIN pg_class c ON c.oid = format('%I.%I', p.schemaname, p.tablename)::regclass
  OR c.oid IN (SELECT relid FROM pg_partition_ancestors(format('%I.%I', p.schemaname, p.tablename)::regclass))
WHERE p.pubname = 'bintrail_pub' AND c.relreplident <> 'f';
SQL

You want three answers: logical, then dbtrail | t, then (0 rows). A table listed under not_ready is not captured until you run ALTER TABLE <table> REPLICA IDENTITY FULL; on it.

Every check DBTrail runs, with the statement that fixes each, on a machine with Docker:

docker run --rm --network host ghcr.io/dbtrail/bintrail-pg:latest doctor \
  --query-dsn "postgres://dbtrail:$DBTRAIL_PW@$PGHOST:${PGPORT:-5432}/$PGDATABASE" \
  --slot "bintrail_$PGDATABASE" --publication bintrail_pub

It ends with Failed: 0 when all is well. What each line means: below.

Done. Next, connect it to DBTrail with ./dbtrail server add: Quickstart, step 2.

In detail

Nothing is installed in your PostgreSQL server

DBTrail reads your changes as an ordinary logical-replication client, with PostgreSQL's built-in pgoutput. It installs no extension, plugin or trigger, and never writes to your server.

What DBTrail needs

  • PostgreSQL 15 to 18. 13 and 14 may work, best effort.
  • wal_level = logical. It takes a restart.
  • A login with REPLICATION, and SELECT on your tables for the first copy.
  • REPLICA IDENTITY FULL on every table you capture, and on each partition of a partitioned one.
  • A publication over those tables. You create it; DBTrail checks it and never creates it.
  • Primary keys that are not DEFERRABLE.
  • Recommended: max_slot_wal_keep_size set (for example '10GB').

The replication slot is created for you.

Every step in full

The examples use a database named shop with its tables in public, where PostgreSQL puts a table created without a schema name. \dt *.* in psql lists your tables with their schema.

1. Logical replication

ALTER SYSTEM SET wal_level = 'logical';, then a restart: a reload is not enough. On RDS, Aurora and Cloud SQL it is a parameter of the instance instead: Managed PostgreSQL. The slot uses one of max_replication_slots and one of max_wal_senders; the defaults (10) are fine.

2. DBTrail's login

  • It needs REPLICATION, not superuser. It only reads.
  • The first copy reads every table, so the login needs SELECT on them. Without it the server is still added and its changes are recorded, but the copy is refused, with the GRANT to run (a login that cannot read a table).
  • Tables created later: the GRANT SELECT covers the tables that exist when it runs. For the next ones, run once, as the role that creates them: ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO dbtrail;
  • Another schema: also GRANT USAGE ON SCHEMA <schema> TO dbtrail;.
  • RDS and Aurora: GRANT rds_replication TO dbtrail;, or capture as the master user, which already has it.

3. The whole row in every change

By default PostgreSQL logs only the primary key of the earlier row of an UPDATE or DELETE. DBTrail captures a table only at REPLICA IDENTITY FULL, which logs the whole row. It costs more WAL, and it needs no primary key.

ALTER TABLE public.orders REPLICA IDENTITY FULL;
  • A partitioned table: set it on the parent and on each partition. The statement on the parent does not reach the partitions (the block).
  • A table created later in a FOR ALL TABLES publication (or a FOR TABLES IN SCHEMA one) starts without it. Run the ALTER TABLE right after the CREATE TABLE, before its first rows.
  • A table with no replica identity at all (NOTHING, or DEFAULT with no primary key): PostgreSQL itself refuses UPDATE and DELETE on it while it is in a publication. Set FULL.
  • A primary key declared DEFERRABLE: the table is not captured. Add the key again without it (ALTER TABLE <t> DROP CONSTRAINT <key>, ADD PRIMARY KEY (...)), or take the table out of the publication.
PostgreSQL 18 and generated columns

Read this before you set REPLICA IDENTITY FULL on PostgreSQL 18

A table at REPLICA IDENTITY FULL, in a publication, with a generated column the publication does not publish, refuses every UPDATE and DELETE, from your application too (Replica identity must not contain unpublished generated columns). INSERT still works. It happens as soon as both settings are in place, whether DBTrail runs or not. PostgreSQL 17 and older have no such rule.

Find your generated columns first:

SELECT c.relnamespace::regnamespace AS schema, c.relname AS "table", a.attname AS "column",
       CASE a.attgenerated WHEN 's' THEN 'STORED' ELSE 'VIRTUAL' END AS kind
FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid
WHERE a.attgenerated <> '' AND a.attnum > 0 AND NOT a.attisdropped AND c.relkind IN ('r', 'p');
  • STORED: make the publication publish it first, then set FULL. Create it WITH (publish_generated_columns = stored) (step 4), or, for one that exists: ALTER PUBLICATION bintrail_pub SET (publish_generated_columns = stored);
  • VIRTUAL (what GENERATED ALWAYS AS (...) creates on 18 when neither word is given): PostgreSQL cannot publish it, so DBTrail cannot capture that table. Do not set FULL on it, and leave it out of the publication (a FOR ALL TABLES publication cannot leave a table out: list the tables instead). Or replace the column with a STORED one.
  • A table in another publication too is refused if that one does not publish the column.
  • Your application is already refused? This lets it write again at once (DBTrail then does not capture the table): ALTER TABLE public.orders REPLICA IDENTITY DEFAULT;

DBTrail's checks find both cases and stop with the statements to run.

A table that is not ready

A table below FULL, with a replica identity that leaves a primary-key column out, or with a DEFERRABLE primary key, does not stop capture of the others:

  • Its changes are not captured. The other tables go on.
  • DBTrail says so: the checks fail with the statement to run, so server add and Start wait until it is fixed; Status lists the table in red; status shows capture as DEGRADED, with the reason.
  • After the fix, capture of the table starts again by itself, with no restart. The changes made before the fix are missing from its copy until the table's next full read, and from its history for good.
  • Until that full read, its copy stays as its last read left it. Fresh mode and the PostgreSQL port do not take it as current (with forwarding on, the port sends statements on it to your PostgreSQL), a read of one of its rows at a past moment is refused, and verify reports it as not compared.
  • A copy read from S3, or with --baseline-carry-forward-unchanged off, cannot keep that table as it was: the scheduled update is refused for every table (refused-gap, naming the table) until a full read.

4. The publication

A publication is PostgreSQL's list of the tables it sends. You create it: DBTrail checks that it exists and covers your tables, and never creates it.

-- Specific tables (recommended: you control exactly what is captured):
CREATE PUBLICATION bintrail_pub FOR TABLE public.orders, public.customers;

-- On PostgreSQL 18, when a captured table has a STORED generated column
-- (see the note in step 3):
CREATE PUBLICATION bintrail_pub FOR TABLE public.orders, public.customers
  WITH (publish_generated_columns = stored);

-- …or everything (requires superuser):
CREATE PUBLICATION bintrail_pub FOR ALL TABLES;
  • Leave publish unset, so it sends every operation. A publication that leaves out an insert, update or delete, or has a row filter (WHERE (...)) or a column list, is refused: those changes would be lost.
  • Without truncate, a TRUNCATE is not recorded and the rows it removed stay in the copy. DBTrail warns about it. The fix keeps the publication's tables and other options: ALTER PUBLICATION bintrail_pub SET (publish = 'insert, update, delete, truncate');
  • To add a table later, set it to FULL first, then ALTER PUBLICATION bintrail_pub ADD TABLE public.items;. In the other order, its changes in between are not captured.

5. The replication slot

DBTrail creates it on its first run (bintrail_ and the database name) and reuses it after a restart.

  • A slot keeps WAL on your PostgreSQL until DBTrail has read it. While capture is stopped it grows, and with no limit it can fill the disk.
  • max_slot_wal_keep_size caps it ('10GB' in the steps above). Past the cap the slot is lost: capture does not resume, and a server has to start over with a full read. What DBTrail recorded up to then stays usable.
  • Removing a server leaves its slot on your PostgreSQL: drop it yourself. A capture run with bintrail-pg alone is removed with bintrail-pg reset (Reference).

What the checks look for

The doctor command in Check it runs these. Capture prints the same warnings when it starts. βœ“ passed, ! a warning (capture works), βœ— a failure, with the statement to run under it.

  • wal_level: fails unless it is logical.
  • max_slot_wal_keep_size: warns while it is unlimited (-1, the default on RDS and Aurora too).
  • Replication slot health: passes while reserved; warns at extended or unreserved, when the slot holds WAL near the limit; fails at lost.
  • REPLICA IDENTITY FULL: fails for each table, or partition, below FULL.
  • Replica identity holds the primary key, Primary keys are not deferrable: fail for such a table (step 3).
  • Publication publishes TRUNCATE: warns (step 4).
  • Read access to the published tables: warns for a table the login cannot read, with the GRANT to run.
  • No UNLOGGED tables: warns. PostgreSQL writes no change log for them, so they are never captured. ALTER TABLE <t> SET LOGGED if you need them.
  • FK cascade-child coverage: warns when a published table has an ON DELETE CASCADE or SET NULL child that is not published. Add the child, at REPLICA IDENTITY FULL.

What to do about any other message: Troubleshooting.

On this page