Files
my-vault/01_Projects/Home-Automation/data.md
T

2.9 KiB
Raw Blame History

Yes, pgloader can work with a MySQL dump file, but it is not the most common or optimal way to use pgloader. By default, pgloader is designed to connect directly to the MySQL or MariaDB database and migrate the schema and data to PostgreSQL in one seamless operation. However, it does have support for importing data from SQL dump files.


How to Use pgloader with a MySQL Dump File

1. Prepare the MySQL Dump File

Generate a dump file from MariaDB/MySQL that is compatible with PostgreSQL:

mysqldump --compatible=postgresql --default-character-set=utf8 --no-create-db --skip-lock-tables --extended-insert --quote-names -u hass -p hass > hass.sql
  • --compatible=postgresql: Ensures basic compatibility with PostgreSQL.
  • --skip-lock-tables: Prevents table locking during dump.
  • --extended-insert: Creates multi-row insert statements, which are efficient.
  • --quote-names: Ensures column names are quoted, reducing syntax conflicts.

2. Adjust the Dump File (If Needed)

MySQL dump files may still include syntax incompatible with PostgreSQL, such as:

  • AUTO_INCREMENT → Replace with PostgreSQL SERIAL.

  • **Backticks ()** → Replace with double quotes ("`).

  • Engine and Charset Options:

    ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Remove or ignore these lines.

Tools like sed or manual editing can handle these adjustments.

3. Use pgloader to Import the Dump File

Create a pgloader configuration file to handle the dump file import. Here's an example configuration:

LOAD DATABASE
    FROM FILE 'hass.sql'
    INTO postgresql://hass:hass@localhost/hass

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

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

ALTER SCHEMA 'hass' RENAME TO 'public';
  • FROM FILE: Specifies the path to the MySQL dump file.
  • INTO: Defines the PostgreSQL database connection string.
  • ALTER SCHEMA: Optionally maps schemas.

Run pgloader:

pgloader /path/to/config_file.load

Caveats

  • Dump File Complexity: If the dump file includes MariaDB/MySQL-specific functions or features, these might not be translated properly.

  • Manual Adjustments: Even with --compatible=postgresql, dump files often need manual cleanup.

  • Direct Connection Preferred: When possible, connect pgloader directly to the MariaDB database for a smoother migration:

    pgloader mysql://user:password@host/dbname postgresql://user:password@host/dbname
    

Best Practice

If your dump file requires significant manual adjustment, consider alternatives:

  • Use a direct pgloader connection.
  • Opt for an ETL tool or custom migration script if your schema is complex.

Let me know if youd like help fine-tuning a configuration for pgloader or alternatives! 🚀