简体   繁体   English

PostgreSQL 恢复错误:“x 处或附近的语法错误”

[英]PostgreSQL restore error: "syntax error at or near "x"

I get my PostgreSQL database backup using the following command:我使用以下命令获取 PostgreSQL 数据库备份:

pg_dump --encoding utf8 "host=localhost port=5432 
    dbname=employee user=postgres" > C:\backup\employee.sql

Note: I tried both with and without --encoding parameter.注意:我尝试了使用和不--encoding参数。 Also tried to use many different file format for backup as: dump , tar , etc.还尝试使用许多不同的文件格式进行备份: dumptar等。


Then I try to restore database using the following command:然后我尝试使用以下命令恢复数据库:

psql -e -h 127.0.0.1 -p 5432 -U postgres -d employee -f C:\backup\employee.sql

There are lots of lines on the console during restore process, but at the end of this log, the following error is shown:在还原过程中控制台上有很多行,但在此日志的末尾,显示以下错误:

psql:C:\backup\employee.sql:1: ERROR: syntax error at or near "ÿs" LINE 1: ÿs-ebSG:9loios3"s"des9",e3s{0LrT}0se"me"D13,mispRio""""aild{... psql:C:\backup\employee.sql:1: 错误: "ÿs" 第一行或附近的语法错误: ÿs-ebSG:9loios3"s"des9",e3s{0LrT}"0se"RID" “”生病{...

How can I fix it?我该如何解决? I might convert backup file encoding via conv , but as I use Windows, I cannot used it.我可能会通过conv转换备份文件编码,但由于我使用 Windows,我无法使用它。

Update:更新:

  1. Postgres version I am dumping from: PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by Debian clang version 10.0.1, 64-bit我正在转储的 Postgres 版本: x86_64-pc-linux-gnu 上的 PostgreSQL 12.6,由 Debian clang 版本 10 编译。

  2. Postgres version I am restoring to: PostgreSQL 11.12 (Debian 11.12-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit我要恢复到的 Postgres 版本: x86_64-pc-linux-gnu 上的 PostgreSQL 11.12 (Debian 11.12-1.pgdg90+1),由 gcc (Debian 6.316-364+deb9-705) 编译。

  3. pg_dump version I am using: I have a look at the container to inspec pg_dump version, but cannot get the pg_dump folder.我正在使用的 pg_dump 版本:我查看了容器以检查 pg_dump 版本,但无法获取 pg_dump 文件夹。

This is a classical error due to the fact that PostgerSQL is unable to execute a binary backup, but only a dump which is a copie of the values of the rows of the table.这是一个经典错误,因为 PostgerSQL 无法执行二进制备份,而只能执行作为表行值副本的转储。 So literrals are extracts as it, and number or dates are convert into litterals.因此,文字是提取物,而数字或日期则转换为文字。 Then SQL commands are added to recreate the structure and the INSERTs.然后添加 SQL 命令以重新创建结构和 INSERT。

The result is a texte file that can be interpreted as a SQL script to reconstitute the database.结果是一个文本文件,可以解释为 SQL 脚本来重构数据库。

But this method as a major disadvantage... The dump file must be encoded in a proper coding way to avoid some combination of bytes that can drive the file to be restored in a situation that the reader process interprets thoses bytes as non printed commands like carriage return, line feed...但是这种方法是一个主要缺点......转储文件必须以适当的编码方式进行编码,以避免在读取器进程将这些字节解释为非打印命令的情况下可以驱动文件恢复的某些字节组合,例如回车,换行...

In other databases like SQL Server there is no dump, but only binary backups and the binary pages are only replaced from the backup file to the target data file which avoid all those troubles.在 SQL 服务器等其他数据库中,没有转储,但只有二进制备份,二进制页面仅从备份文件替换到目标数据文件,避免了所有这些麻烦。

So be carefull when doing a dump, use the recommanded encoding even when extracting the data or import in to re create the db.所以在进行转储时要小心,即使在提取数据或导入以重新创建数据库时也要使用推荐的编码。

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

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