简体   繁体   English

恢复 sql 时 psql 无效命令 \N

[英]psql invalid command \N while restore sql

I'm trying to restore my dump file, but it caused an error:我正在尝试恢复我的转储文件,但它导致了一个错误:

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

Is there a solution?有解决办法吗? I searched, but I didn't get a clear answer.我搜索了,但我没有得到明确的答案。

Postgres uses "\\N" as substitute symbol for NULL value. Postgres 使用 "\\N" 作为 NULL 值的替代符号。 But all psql commands start with a backslash "" symbol.但是所有 psql 命令都以反斜杠 "" 符号开头。 You can get these messages, when a copy statement fails, but the loading of dump continues.当复制语句失败但转储的加载继续时,您可以获得这些消息。 This message is a false alarm.此消息是误报。 You have to search all lines prior to this error if you want to see the real reason why COPY statement failed.如果您想查看 COPY 语句失败的真正原因,您必须搜索此错误之前的所有行。

Is possible to switch psql to "stop on first error" mode and to find error:可以将 psql 切换到“在第一个错误时停止”模式并查找错误:

psql -v ON_ERROR_STOP=1

I received the same error message when trying to restore from a binary pg_dump.尝试从二进制 pg_dump 恢复时收到相同的错误消息。 I simply used pg_restore to restore my dump and completely avoid the \\N errors, eg我只是使用pg_restore来恢复我的转储并完全避免\\N错误,例如

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

Explanation of switches:开关说明:

-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

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

I have run into this error in the past as well.我过去也遇到过这个错误。 Pavel is correct, it is usually a sign that something in the script created by pg_restore is failing. Pavel 是正确的,这通常表明 pg_restore 创建的脚本中的某些内容失败了。 Because of all the "/N" errors, you aren't seeing the real problem at the very top of the output.由于所有“/N”错误,您在输出的最顶部看不到真正的问题。 I suggest:我建议:

  1. inserting a single, small table (eg, pg_restore --table=orders full_database.dump > orders.dump )插入单个小表(例如, pg_restore --table=orders full_database.dump > orders.dump
  2. if you don't have a small one, then delete a bunch of records out of the restore script - I just made sure the ./ was the last row to be loaded (eg, open orders.dump and delete a bunch of records)如果你没有一个小的,那么从恢复脚本中删除一堆记录 - 我只是确保 ./ 是要加载的最后一行(例如,打开orders.dump并删除一堆记录)
  3. watch the standard output, and once you find the problem, you can always drop the table and reload观察标准输出,一旦发现问题,随时可以丢表重载

In my case, I didn't have the "hstore" extension installed yet, so the script was failing at the very top.就我而言,我还没有安装“hstore”扩展,所以脚本在最顶端失败。 I installed hstore on the destination database, and I was back in business.我在目标数据库上安装了 hstore,然后我又开始营业了。

您可以使用 INSERTS 语句和 --inserts 参数生成转储。

Same thing was happened to me today.今天同样的事情发生在我身上。 I handled issue by dumping with --inserts command.我通过使用 --inserts 命令转储来处理问题。

What I do is:我要做的是:

1) pg_dump with inserts: 1) 带插入的 pg_dump:

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

2) psql (restore your dumped file) 2) psql (恢复你的转储文件)

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

Note-1 ) Make sure that adding outputfile will increase speed of import. Note-1 ) 确保添加 outputfile 会提高导入速度。

Note-2 ) Do not forget to create table with exact same name and columns before importing with psql.注2)在使用psql导入之前,不要忘记创建具有完全相同名称和列的表。

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

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就我而言,我从数据库 A 创建了一个转储
pg_dump -a -t table_name > dump.sql
and was trying to restore it to database B with并试图将其恢复到数据库 B
psql < dump.sql (after updating the proper env vars, of course) psql < dump.sql (当然,在更新适当的环境变量之后)
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.我最终发现转储是特定于模式的,尽管它只是data-only-a选项,因此表结构不是转储的明确一部分)。 That meant that without manually modifying the dump, I couldn't use a dump generated from schema1.table_name to populate schema2.table_name .这意味着如果不手动修改转储,我无法使用从schema1.table_name生成的转储来填充schema2.table_name Manually modifying the dump was easy, the schema is specified in the first 15 lines or so.手动修改转储很容易,模式在前 15 行左右指定。

大多数情况下,解决方案是安装postgres-contrib包。

For me using postgreSQL 10 on SUSE 12, I resolved the invalid command \\N error by increasing disk space.对于我在 SUSE 12 上使用 postgreSQL 10,我通过增加磁盘空间解决了invalid command \\N错误。 Lack of disk space was causing the error for me.磁盘空间不足导致了我的错误。 You can tell if you are out of disk space if you look at the file system your data is going to in the df -h output.如果在df -h输出中查看数据将要到达的文件系统,则可以判断是否磁盘空间不足。 If file system/mount is at 100% used, after doing something like psql -f db.out postgres (see https://www.postgresql.org/docs/current/static/app-pg-dumpall.html ) you likely need to increase the disk space available.如果文件系统/挂载使用率为 100%,则在执行psql -f db.out postgres (请参阅https://www.postgresql.org/docs/current/static/app-pg-dumpall.html ),您可能需要增加可用的磁盘空间。

I had the same problem, I created a new database and got invalid command \\N on restore with psql.我遇到了同样的问题,我创建了一个新数据库,并在使用 psql 还原时得到了invalid command \\N I solved it by setting the same tablespace with the old database.我通过使用旧数据库设置相同的表空间来解决它。

For example, old database backup had tablespace "pg_default", I defined the same tablespace to the new database, and the above error has gone!例如,旧数据库备份有表空间“pg_default”,我为新数据库定义了相同的表空间,并且上述错误消失了!

I followed all these example's and they all failed with the error we are talking about:我遵循了所有这些示例,但它们都因我们正在谈论的错误而失败:

Copy a table from one database to another in Postgres 在 Postgres 中将表从一个数据库复制到另一个数据库

What worked was the syntax with -C , see here:有效的是-C的语法,请参见此处:

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

Also if there are differing Schema's between the two, I find altering one dB's schema to match the others is necessary for Table copies to work, eg:此外,如果两者之间存在不同的架构,我发现改变一个 dB 的架构以匹配其他架构对于表副本的工作是必要的,例如:

DROP SCHEMA public;
ALTER SCHEMA originalDBSchema RENAME TO public;

My solution was this:我的解决方案是这样的:

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

this way I could read the error message这样我就可以阅读错误信息

I hope this helps anybody.我希望这对任何人都有帮助。

For me it was the ENCODING and LOCALE that differ from the source database.对我来说,是 ENCODING 和 LOCALE 与源数据库不同。 Once I dropped the target DB and recreated it it was working fine.一旦我删除了目标数据库并重新创建它,它就可以正常工作。

In my case the problem was a lack of disk space on my target machine.就我而言,问题是我的目标机器上缺少磁盘空间。 Simply increasing the local storage fixed it for me.简单地增加本地存储为我修复了它。

Hope this helps someone ;)希望这有助于某人;)

Adding my resolution, incase it helps anyone.添加我的决议,以防它帮助任何人。 I installed postgis but the error wasn't resolved.我安装了 postgis,但错误没有解决。 The --inserts option was not feasible as I had to copy a big schema having tables with thousands of rows. --inserts 选项不可行,因为我必须复制一个包含数千行表的大模式。 For the same database I didn't see this issue when pg_dump and psql (restore) were run on mac.对于同一个数据库,当 pg_dump 和 psql(恢复)在 mac 上运行时,我没有看到这个问题。 But the issue came when pg_dump was run on linux machine, the dump file copied to mac and tried for restore.但是当 pg_dump 在 linux 机器上运行时,问题出现了,转储文件复制到 mac 并尝试恢复。 So I opened the dump file in VSCode.所以我在 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.之后,转储文件恢复运行没有无效命令 \N 错误。

The @farshid-ashuri's answer works fine for me. @farshid-ashuri 的答案对我来说很好。

I'm using Postgres 14 on Almalinux and messages like its appear.我在 Almalinux 上使用 Postgres 14 并出现类似的消息。 After installing the postgresql14-contrib package, I restored a full dump and no more messages.安装 postgresql14-contrib package 后,我恢复了完整的转储,没有更多消息。

*Sorry by my poor english. *对不起,我的英语很差。

check that the columns in the table and the columns in the backup file suitable检查表中的列和备份文件中的列是否合适

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM