简体   繁体   中英

Convert mysql binary to postgresql bytea

What is the easiest way to export data from mysql and import it in postgresql?

I am having trouble with the MySQL binary fields conversion.

The equivalent of binary type in MySQL is bytea in PostgreSQL.


You can use pgloader (simplest way)

After installing pgloader, create simple script test.load

load database  
from mysql://username:password@host/database_name
into postgresql://postgres:postgres@localhost/database_name

WITH include drop, create tables, create indexes, reset sequences

  SET maintenance_work_mem to '128MB',
      work_mem to '12MB'

 CAST type binary TO bytea drop typemod  using byte-vector-to-bytea;

Run it in your terminal:

pgloader test.load

Another way is using mysqldump

1. Dump it with hex-blob option

mysqldump -u username -p -h host --skip-quote-names --hex-blob --skip-triggers \
--compact --no-create-info your_db your_table > prepg.dump

2. Do sed so it can be inserted to you bytea type column

sed "s/0x\([0-9A-F]*\)/decode('\1','hex')/g" prepg.dump > pg.dump

3. Load into your PostgreSQL table

\i '/path_to_file/pg.dump'

Reference

Generate your mysql script of your database using your editor and then try one of the sql converter. Personally I tried this converter before and it works well: http://www.sqlines.com/online

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