Sub topic 6
Restoring Plain SQL Dumps with `psql`
Assume you know basic SQL and psql, can use a Linux command line, and understand PostgreSQL roles, databases, and transactions. This chapter focuses on restoring a plain SQL dump into a database.
What a Plain SQL Dump Contains
A plain SQL dump is a text file containing SQL statements such as CREATE TABLE, COPY, INSERT, CREATE INDEX, and ALTER TABLE. pg_dump creates this format by default, or when you specify --format=plain or -Fp.
For example:
pg_dump -U postgres -d mydb -Fp -f mydb.sqlThe dump contains objects from mydb, but it does not create the target database unless you create the dump with --create. A common restore workflow is:
- Create an empty target database.
- Run the dump through
psql. - Check the command result and inspect the restored data.
createdb -U postgres mydb_restored
psql -U postgres -d mydb_restored -v ON_ERROR_STOP=1 -f mydb.sqlThe -f option tells psql to read commands from a file. The ON_ERROR_STOP variable makes psql stop when a SQL command fails. Without it, psql normally reports an error and continues with later statements.
Choosing Error and Transaction Behavior
A restore can run as one transaction or as many transactions. The choice affects what remains after a failure.
| Mode | Command option | Behavior |
|---|---|---|
| Single transaction | `--single-transaction` | All changes commit together or roll back together |
| Normal mode | Omit the option | Earlier successful commands can remain after a later failure |
For a small or medium dump, use one transaction when the dump and destination support it:
psql -U postgres \
-d mydb_restored \
-v ON_ERROR_STOP=1 \
--single-transaction \
-f mydb.sqlThis gives an all-or-nothing restore. However, some statements cannot run inside a transaction, and a large restore may require more temporary resources. If the dump contains such statements, remove --single-transaction and keep ON_ERROR_STOP=1.
Roles, Ownership, and Permissions
A dump can contain statements that assign objects to their original owners or restore privileges. The target cluster must contain those roles. Restore cluster-wide roles separately, or create the needed roles before restoring.
When generating a portable dump for another environment, you can omit ownership and privilege statements:
pg_dump -U postgres \
-d mydb \
--no-owner \
--no-privileges \
--format=plain \
--file=mydb_portable.sqlThe role running psql still needs enough permission to create the objects and load their data. A database owner or suitable administrative role is usually required.
Understanding the Restore Flow
The file is interpreted in order. Tables must exist before their data is loaded, and data must exist before indexes or constraints that depend on it are created.
Checking the Result
Check the command's exit status in a shell script:
psql -U postgres -d mydb_restored \
-v ON_ERROR_STOP=1 \
--single-transaction \
-f mydb.sql
if [ "$?" -ne 0 ]; then
echo "Restore failed" >&2
exit 1
fiThen connect and inspect representative tables:
psql -U postgres -d mydb_restored -c '\dt'
psql -U postgres -d mydb_restored -c 'SELECT count(*) FROM public.orders;'Exercise
Why should a restore command include -v ON_ERROR_STOP=1?
Show answer
It makes psql stop at the first SQL error and return a failure instead of silently continuing through the rest of the dump.