简体   繁体   English

将MySQL转储导入Postgres并转义\\

[英]import MySQL dump into Postgres and escape \

I received a MYSQL dump and needs to push it into the Postgres table. 我收到一个MYSQL转储,需要将其推入Postgres表中。

Inside this poi_dump.sql dump, there are many rows of insert. 在poi_dump.sql转储中,有很多插入行。 The example as below: 例子如下:

Insert into table values (1,'hello','\rthis is a beautiful Alan\'s cottage', '\nbyebye')
Insert into table values (1,'hello','\rthis is a big\"Work', '\nbyebye')

I am using PSQL command: 我正在使用PSQL命令:

psql analytics < poi_dump.sql

However, the postgres does not recognize all the \\ used to escape in a ' ' field. 但是,postgres无法识别在''字段中用于转义的所有\\。

Errors From Postgres:
Query buffer reset (cleared).
invalid command \nOn
invalid command \n
invalid command \'
invalid command \'
invalid command \'
invalid command \n
invalid command \"Without
invalid command \nPort-a-loo
Query buffer reset (cleared).
invalid command \n

How can i make postgres to accept those \\r ,\\n , 's inside a quote field? 我怎样才能使postgres在报价字段内接受那些\\ r,\\ n,?

PostgreSQL doesn't recognize backslash in string by default. PostgreSQL默认不识别字符串中的反斜杠。 But this behave can be simply changed: 但这行为可以简单地更改:

ides_jmmaj_prac=# select 'hello\nworld';
┌──────────────┐
│   ?column?   │
╞══════════════╡
│ hello\nworld │
└──────────────┘
(1 row)

Time: 0,496 ms
ides_jmmaj_prac=# set standard_conforming_strings TO off;
SET
Time: 0,251 ms
ides_jmmaj_prac=# select 'hello\nworld';
WARNING:  nonstandard use of escape in a string literal
ŘÁDKA 1: select 'hello\nworld';
                ^
DOPORUČENÍ:  Use the escape string syntax for escapes, e.g., E'\r\n'.
┌──────────┐
│ ?column? │
╞══════════╡
│ hello   ↵│
│ world    │
└──────────┘
(1 row)

Time: 0,496 ms

Disable warnings by: 通过以下方式禁用警告:

set escape_string_warning to off;

Probably \\r should be removed. 大概\\r应该被删除。 PostgreSQL using Unix end of lines - only \\n (I didn't test it). PostgreSQL使用Unix的结尾-仅\\n (我没有测试过)。

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

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