Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

psql prints invalid command \N many times during a restore

Errorinvalid command \N

Short answer

The \N lines are data rows, not commands. A COPY ... FROM STDIN statement earlier in the dump failed, so psql read its rows as if they were commands. Scroll up to the first real error, fix it (missing role, missing database, wrong tool), and rerun with ON_ERROR_STOP set.

  • Applies to PostgreSQL 16, PostgreSQL 17, PostgreSQL 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Rerun the restore so that it stops at the first failure. The error that ends the run is the real problem; the \N lines were only the fallout.

    createdb -T template0 mydb
    psql -X --set ON_ERROR_STOP=on mydb < dump.sql
  2. 2Match what you see, then jump to its fix

    1. 1ERROR: role "someone" does not exist, shortly before the first invalid command lineA role named in the dump does not existFix
    2. 2The restore starts and the first CREATE or COPY statement fails.The target database was not created from template0, or does not existFix
    3. 3The file is a non-text dump (not readable SQL), and psql fails on the first lines.The file is not a plain-SQL dumpFix

What does the error look like?

  • invalid command \N

Cause 1 of 3

A role named in the dump does not exist

1Check

A plain dump records who owns each object. The manual says every user who owns objects or holds permissions in the dumped database must exist before the restore, or the objects are not recreated with the original ownership.

psql -X -c '\du'

2Fix

Create the missing roles first with CREATE ROLE, then restore again into a fresh database.

dropdb mydb
createdb -T template0 mydb
psql -X --set ON_ERROR_STOP=on mydb < dump.sql

3Verify

The restore runs to the end without stopping, and the exit status is 0.

echo $?

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

The target database was not created from template0, or does not exist

1Check

The dump does not create the database. The manual says to create it from template0 before running psql, so objects and extensions are not mixed with whatever template1 contains.

psql -X -l

2Fix

Create an empty database from template0 and restore into it.

createdb -T template0 mydb

3Verify

psql connects to the new database and the restore passes the first COPY.

psql -X -d mydb -c '\dt'

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

The file is not a plain-SQL dump

1Check

Non-text dumps are restored with pg_restore, not psql. Plain files open with readable SQL; custom-format files do not.

head -c 200 dump.sql

2Fix

Restore it with pg_restore into an empty database.

pg_restore -d mydb dump.sql

3Verify

pg_restore finishes without errors.

psql -X -d mydb -c '\dt'

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Always restore with ON_ERROR_STOP set, so the first error stops the run.
  • Use -1 (--single-transaction) for small dumps so a failed restore leaves nothing half loaded.
  • Write down which roles own objects in the database before you dump it, so you can recreate them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.