简体   繁体   中英

how to remove back slashes from mysql dump table while importing in postgreSQL

I am facing back slashes() problem during migrating database from MYSQL to postgrSQL. My problem is I have many large mysql tables dump(with sql formate) around 2gb and try to import in postgreSQL database. IN mysql dump file there are many back slashes in many columns so if I am try to import in postgreSQL the it through an exception. I have tried many solutions but did not worked yet. and the mysql dump files are very large so I am unable to set it manually. you can see the mysql code below.

MYSQL CODE:

INSERT INTO Leads 
(921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');

so please suggest any solution. Thanks in advance....!!!!!!

You need to escape these slashes by doubling them up:

INSERT INTO Leads (921347,NULL,'Lee\\'s ...

How you do that depends on the tools at your disposal. If you don't have an editor that can search and replace for you then you could write a php script and process the file using preg_replace.

Personally I would not use sql dump files to migrate data, apart from this sort of problem they are just too slow. Far better to use Navicat\\Toad\\Heidi or the like to do it with. Navicat is best but you have to pay for it while others are free.

if you add E before quoted string it will run, like

INSERT INTO Leads (921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');

will become

INSERT INTO Leads (921347,NULL,e'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');

you can sed all ,' to be ,e' and run insert statements

Eg: I created a file with your statement and sed all occurrences:

cat mysql.sql
INSERT INTO Leads (921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');
INSERT INTO Leads (921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');
sed -i.bak s/,'/,e'/g mysql.sql
cat mysql.sql
INSERT INTO Leads (921347,eNULL,e'Lee\'s Summit',e'MO',e'64081',e'1983-09-24',eNULL,e'75.81.43.181',eNULL,e0,e'cash');
INSERT INTO Leads (921347,eNULL,e'Lee\'s Summit',e'MO',e'64081',e'1983-09-24',eNULL,e'75.81.43.181',eNULL,e0,e'cash');
cat mysql.sql.bak
INSERT INTO Leads (921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');
INSERT INTO Leads (921347,NULL,'Lee\'s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');

So you have .bak of original and mysql.sql converted for postgres...

Update 2: You can use Oracle style to change not all quoted strings, but escaped quotes only:

sed -e "s/\\\\'/''/g" mysql.sql
INSERT INTO Leads (921347,NULL,'Lee''s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');
INSERT INTO Leads (921347,NULL,'Lee''s Summit','MO','64081','1983-09-24',NULL,'75.81.43.181',NULL,0,'cash');

Try:

shell> mysqldump --tab=/tmp yourdatabase

Then for each table use http://www.postgresql.org/docs/8.1/static/sql-copy.html

COPY country FROM '/tmp/yourtable';

Because there is no simple solution doing it for large databases, please read the following wikibook:

Converting MySQL to PostgreSQL http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL

There you find examples how to migrate your dumpfile or a suggestion to use CSV files instead of dump files.

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