简体   繁体   中英

PostgreSQL restore error: "syntax error at or near "x"

I get my PostgreSQL database backup using the following command:

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. Also tried to use many different file format for backup as: dump , tar , etc.


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{...

How can I fix it? I might convert backup file encoding via conv , but as I use Windows, I cannot used it.

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

  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

  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.

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. 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.

The result is a texte file that can be interpreted as a SQL script to reconstitute the database.

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.

So be carefull when doing a dump, use the recommanded encoding even when extracting the data or import in to re create the db.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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