brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: .sha3sum in the sqlite3 shell compares the content of two SQLite files

Sqlite
~/tips/tip-sqlite-sha3sum-compare-content

sqlite3 app.db ".sha3sum"

Hashes the rows of a SQLite file so two copies can be compared regardless of file layout.

Two SQLite files with the same data can have different bytes on disk, for example after a VACUUM, so sha256sum on the files is no use. The shell can hash the content instead.

sqlite3 app.db ".sha3sum"
sqlite3 restored.db ".sha3sum"

Each prints one SHA3 hash of all rows in all tables. When both lines match, the data is the same, no matter how the pages are laid out. I think it is a good way to check that a restored backup or a copy made with .backup really holds what the original had.

Some options are worth knowing. --schema also hashes the table definitions, so a missing index shows up too. Any other argument is a LIKE pattern for table names, so .sha3sum users hashes only that table, and .sha3sum 'log%' all tables that start with log. .help sha3sum lists the rest.

By default the schema is not part of the hash, so two files can match while one has an extra index or a different column type. Add --schema when that matters. The file has to be quiet while you hash it, because rows that change during the run give a hash that matches nothing. And it reads every row, so on a file of several gigabytes it takes a while.

Further reading: Command Line Shell For SQLite

More Sqlite tips SELECT unixepoch('2026-01-01 00:00:00'); SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state sqlite3 -bail app.db < migrate.sql CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); –> users(id) CREATE INDEX users_active_email ON users(email) WHERE active = 1; All Sqlite tips (27) All topics

Link