Skip to content
shellhacks.dev
Databases

Dump in custom format

Dump in the custom format with pg_dump -Fc. It is compressed and lets pg_restore list the contents and restore one table, which a plain SQL dump cannot do.

2 min read
Summarize in AI

A plain SQL dump restores all or nothing, and the file for a large database is bigger than it needs to be.

Shell
createdb shop
psql shop -q -c 'CREATE TABLE orders (id int PRIMARY KEY, note text)' -c 'CREATE TABLE users (id int)' -c "INSERT INTO orders SELECT g, repeat('x', 100) FROM generate_series(1, 20000) g"
pg_dump shop > shop.sql
pg_dump -Fc shop > shop.dump
echo "plain: $(wc -c < shop.sql) bytes, custom: $(wc -c < shop.dump) bytes"
head -c 5 shop.dump; echo
pg_restore -l shop.dump | grep -E 'TABLE( DATA)? public'
dropdb shop
Text
plain:  2130406 bytes, custom:    58425 bytes
PGDMP
217; 1259 16496 TABLE public orders postgres
218; 1259 16503 TABLE public users postgres
3458; 0 16496 TABLE DATA public orders postgres
3459; 0 16503 TABLE DATA public users postgres

-Fc writes PostgreSQL's custom archive format, which is compressed with zlib by default and has a table of contents. pg_restore -l prints that list, and pg_restore -t orders -d target shop.dump restores a single table, or -j 4 restores in parallel, as the pg_restore reference describes. The file is binary, beginning with the letters PGDMP, so it is not meant to be read or edited as text. Here it is a small fraction of the size of the plain dump.

Only pg_restore can read it, and it needs a version no older than the one that wrote the file. A plain dump is easier to diff and to load with psql, and the directory format, -Fd, is the one that allows a parallel dump with -j. The size depends on the data; random content hardly compresses.

Share

More snippets

Databases

Describe a table

Show a table's columns, indexes and foreign keys with the psql meta-command backslash d, and add a plus sign for storage and comments.

2 min read
Databases

Run a SQL statement from the shell

Run a statement from the shell with psql -c. One -c string with several statements is a single transaction, so an error undoes all of them, unlike repeated -c.

2 min read
Databases

Run SQL from a file

Run SQL from a file with psql -f. An error in the middle does not stop the file and the exit status stays 0, unless ON_ERROR_STOP is set to 1.

2 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.