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: .dump with a table name exports one SQLite table as SQL

Sqlite
~/tips/tip-sqlite-dump-one-table

sqlite3 app.db ".dump users" > users.sql

Writes the CREATE and INSERT statements for just one table to move it to another file.

.dump on its own writes the whole file as SQL. Often you only need one table, for example to copy it into another database file.

sqlite3 app.db ".dump users" > users.sql
sqlite3 other.db < users.sql

The output holds the CREATE TABLE for that table and an INSERT for every row, inside a transaction. The argument is a LIKE pattern, so .dump 'log%' takes every table whose name starts with log, which is handy for a set of related tables.

Because it is plain SQL, you can read it, grep it or edit it before loading. That’s why I prefer it over a binary copy when moving a table between apps, or when I want to keep a small reference table in git.

It also writes PRAGMA foreign_keys=OFF; at the top, so the import doesn’t trip over foreign keys that point to tables you did not include. That also means nothing checks them during the load. The target must not have a table with that name yet, or the CREATE TABLE fails and the shell goes on with the inserts into the existing table. And the pattern matches object names: an index called idx_email or a trigger on the table is left out, so the index does not come along. Recreate those by hand, or copy the schema with .schema users.

Further reading: Command Line Shell For SQLite (.dump)

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