DBTrail

Quickstart

From nothing to a heavy report answered in seconds, in three steps you copy and paste. No browser needed.

Three steps, all in a terminal. Copy each block, paste it, press Enter.

You need: a Linux machine with Docker and psql, and a PostgreSQL 15 to 18 where you can log in as a superuser (on RDS and Aurora, the master user).

Install DBTrail

the machine that will run DBTrail
curl -fsSL https://raw.githubusercontent.com/dbtrail/dbtrail/main/install-postgres.sh | sh
cd dbtrail

Connect your PostgreSQL

same terminal

Say where it is. 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

Keep this terminal open: every block below reads these lines.

Turn on logical replication. PostgreSQL restarts, so it stops for a few seconds. The restart needs sudo on its machine.

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

PostgreSQL on this same machine? 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:

sudo systemctl restart postgresql

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

Give DBTrail a login and your tables. This block creates the dbtrail login, lets it read your tables, and marks every table in the public schema for capture.

PostgreSQL 18 with generated columns

This block can make PostgreSQL refuse your application's UPDATEs and DELETEs on such a table. Check first.

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 or Aurora: also run psql -c "GRANT rds_replication TO dbtrail".
  • Partitioned tables: one more block.
  • Tables in another schema: what to change.

Check it.

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.

Connect it to DBTrail.

echo "$DBTRAIL_PW" | ./dbtrail server add --flavor postgres \
  -h "$PGHOST" -u dbtrail --password-stdin \
  -d "$PGDATABASE" --publication bintrail_pub \
  --name "$PGDATABASE" --capture-account

PostgreSQL on this same machine? Write -h host.docker.internal in place of -h "$PGHOST".

DBTrail checks your PostgreSQL, says what to fix if something is missing, and starts recording changes. At the end it prints the psql line for DBTrail's PostgreSQL port, and its password.

server add checks PostgreSQL line by line, starts recording changes, and prints the psql line for DBTrail's port with its password.
You should see

Keep that password: it is shown only once. Lost it? ./dbtrail port new-password makes another.

Make the first copy.

./dbtrail snapshot run "$PGDATABASE"
./dbtrail snapshot schedule "$PGDATABASE" --every 1h

The first line reads every table once and waits until the copy is ready. The second keeps it current from the recorded changes, without reading your tables again.

Run a heavy report

the DBTrail machine, same terminal
unset PGPASSWORD
psql "host=127.0.0.1 port=5433 dbname=$PGDATABASE user=app sslmode=require"

The first line forgets the postgres password, so psql asks for DBTrail's: paste the one from step 2. Then turn on psql's clock, so it prints how long each query takes:

\timing on

Now run a total or a ranking over one of your biggest tables, for example orders by quarter:

SELECT date_trunc('quarter', created_at) AS quarter, count(*), sum(total)
  FROM orders GROUP BY 1 ORDER BY 1;

DBTrail answers it from the copy, without loading your PostgreSQL. On 10 million orders it took about half a second. The same report on PostgreSQL itself took 2.7 seconds.

The quarterly report over 10 million orders, answered by DBTrail in about half a second.

To see who answered, run right after it:

SHOW dbtrail.router_status;

last_answer says copy. The table is wide, so psql may show it page by page: press q to get back to the prompt.

Next

On this page