我试图恢复我的转储文件,但它导致了一个错误:

psql:psit.sql:27485: invalid command \N

有解决办法吗?我找了,但没有得到明确的答案。


Postgres使用\N作为NULL值的替代符号。但是所有psql命令都以反斜杠\符号开始。当复制语句失败,但转储的加载仍在继续时,您可以获得这些消息。这条消息是假警报。如果希望看到COPY语句失败的真正原因,则必须搜索此错误之前的所有行。

可以切换psql到“第一个错误停止”模式,并找到错误:

psql -v ON_ERROR_STOP=1

我过去也遇到过这种错误。Pavel是正确的,这通常是pg_restore创建的脚本中某些东西失败的标志。由于所有的“/N”错误,您在输出的顶部看不到真正的问题。我建议:

插入单个小表(例如,pg_restore . table) ——表full_database =订单。转储>个订单。转储) 如果你没有一个小的,那么从恢复脚本中删除一堆记录-我只是确保./是最后一行被加载(例如,打开订单。转储和删除一堆记录) 观察标准输出,一旦发现问题,就可以随时放弃 表格和重载

在我的情况下,我还没有安装“hstore”扩展,所以脚本在顶部失败。我在目标数据库上安装了hstore,然后就可以继续工作了。


当我试图从二进制pg_dump恢复时,我收到了相同的错误消息。我简单地使用pg_restore来恢复我的转储,并完全避免\N错误,例如。

pg_restore -c -F t -F your.backup.tar

开关说明:

-f, --file=FILENAME      output file name
-F, --format=c|d|t       backup file format (should be automatic)
-c, --clean              clean (drop) database objects before recreating

In my recent experience, it's possible to get this error when the real problem has nothing to do with escape characters or newlines. In my case, I had created a dump from database A with pg_dump -a -t table_name > dump.sql and was trying to restore it to database B with psql < dump.sql (after updating the proper env vars, of course) What I finally figured out was that the dump, though it was data-only (the -a option, so that the table structure isn't explicitly part of the dump), was schema-specific. That meant that without manually modifying the dump, I couldn't use a dump generated from schema1.table_name to populate schema2.table_name. Manually modifying the dump was easy, the schema is specified in the first 15 lines or so.


可以使用INSERTS语句和——INSERTS参数生成转储。


我知道这是一个旧帖子,但我遇到了另一个解决方案:postgis没有安装在我的新版本上,这导致我在pg_dump上出现同样的错误


安装postgresql-(你的版本)-postgis-scripts


大多数时候,解决方案是安装postgres-contrib包。


对于我在SUSE 12上使用postgreSQL 10,我通过增加磁盘空间解决了无效命令\N错误。磁盘空间不足导致了我的错误。如果查看df -h输出中数据将要进入的文件系统,就可以判断是否耗尽了磁盘空间。如果文件系统/mount的使用率为100%,在执行类似psql -f db. mount的操作后。对于postgres(请参阅https://www.postgresql.org/docs/current/static/app-pg-dumpall.html),您可能需要增加可用的磁盘空间。


今天我也遇到了同样的事。我通过使用——inserts命令转储来处理问题。

我所做的是:

1) pg_dump插入:

pg_dump dbname --username=usernamehere --password --no-owner --no-privileges --data-only --inserts -t 'schema."Table"' > filename.sql

2) PSQL(恢复转储文件)

psql "dbname=dbnamehere options=--search_path=schemaname" --host hostnamehere --username=usernamehere -f filename.sql >& outputfile.txt

注意1)确保添加outputfile可以提高导入的速度。

注2)在用psql导入之前,不要忘记创建具有完全相同名称和列的表。


我有同样的问题,我创建了一个新的数据库,并得到无效的命令\N恢复与psql。 我通过设置与旧数据库相同的表空间来解决这个问题。

例如,旧数据库备份有表空间“pg_default”,我给新数据库定义了相同的表空间,上面的错误已经消失了!


我遵循了所有这些例子,它们都失败了,出现了我们正在谈论的错误:

在Postgres中将一个表从一个数据库复制到另一个数据库

有效的是使用-C的语法,请看这里:

pg_dump -C -t tableName "postgres://$User:$Password@$Host:$Port/$DBName" | psql "postgres://$User:$Password@$Host:$Port/$DBName"

此外,如果两者之间有不同的模式,我发现改变一个dB的模式以匹配其他的表副本是必要的,例如:

DROP SCHEMA public;
ALTER SCHEMA originalDBSchema RENAME TO public;

我的解决办法是:

psql -U your_user your_db < your.file.here.sql  2>&1|more

这样我就可以读取错误消息

我希望这能对大家有所帮助。


对我来说,与源数据库不同的是ENCODING和LOCALE。 一旦我放弃了目标DB并重新创建它,它就可以正常工作。


在我的例子中,问题是在我的目标计算机上缺少磁盘空间。简单地增加本地存储为我解决了它。

希望这能帮助到一些人;)


Adding my resolution, incase it helps anyone. I installed postgis but the error wasn't resolved. The --inserts option was not feasible as I had to copy a big schema having tables with thousands of rows. For the same database I didn't see this issue when pg_dump and psql (restore) were run on mac. But the issue came when pg_dump was run on linux machine, the dump file copied to mac and tried for restore. So I opened the dump file in VSCode. It detected unusual line terminators and gave option to remove them. After doing that the dump file restore ran without the invalid command \N errors.


检查表中的列和备份文件中的列是否合适