Skip to content

Database migration: MySQL to PostgreSQL

Migrate a MySQL database to PostgreSQL with pgloader in Docker, and fix the two authentication errors you are likely to hit.

3 min readUpdated

pgloader copies a whole MySQL database into PostgreSQL in one command: it reads the source schema, creates matching tables, casts the column types and loads the data.

This note runs both databases, their web consoles and a throwaway Debian container with Docker Compose, runs pgloader from that container, and explains the errors that come up most often. Tested with MySQL 8.0.30, PostgreSQL 15.1 and the pgloader 3.x package from Debian bookworm.

Start the databases with Docker Compose

The Compose file below starts five services:

  • database-server-mysql: the source database.
  • database-gui-mysql: phpMyAdmin on port 8081, to inspect the source.
  • database-server-postgresql: the target database.
  • database-gui-postgresql: pgAdmin on port 5050, to inspect the target.
  • debian: an idle Debian container on the same network, where pgloader runs. Because it shares the Compose network, it reaches the databases by their service names.
docker-compose.yml
services:
database-server-mysql:
image: mysql:8.0.30
volumes:
- ./data/database-data-mysql:/var/lib/mysql
- ./conf/init-scripts/database-mysql:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: dbname
MYSQL_USER: username
MYSQL_PASSWORD: secret
ports:
- "3306:3306"
database-gui-mysql:
image: phpmyadmin/phpmyadmin:5.2.0
depends_on:
- database-server-mysql
restart: always
environment:
PMA_HOST: database-server-mysql
PMA_USER: root
PMA_PASSWORD: secret
ports:
- "8081:80"
database-server-postgresql:
image: postgres:15.1
volumes:
- ./data/database-data-postgresql:/var/lib/postgresql/data
- ./conf/init-scripts/database-postgresql:/docker-entrypoint-initdb.d
restart: always
environment:
POSTGRES_DB: dbname
POSTGRES_USER: username
POSTGRES_PASSWORD: secret
ports:
- "5432:5432"
database-gui-postgresql:
image: dpage/pgadmin4:6.18
volumes:
- ./data/database-gui-data-postgresql:/var/lib/pgadmin
depends_on:
- database-server-postgresql
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: dev@aripd.com
PGADMIN_DEFAULT_PASSWORD: secret
ports:
- "5050:80"
debian:
image: debian:bookworm
stdin_open: true # docker run -i
tty: true # docker run -t

SQL files placed in ./conf/init-scripts/database-mysql (for example a dump of the source database) are imported when the MySQL data directory is first created.

Start the stack in the background:

Terminal window
docker compose up -d

Run pgloader

Open a shell in the debian container:

Terminal window
docker compose exec debian bash

Inside the container, install pgloader and run the migration. The first URL is the source, the second the target:

Terminal window
apt update && apt upgrade
apt install pgloader sqlite3
pgloader --verbose --no-ssl-cert-verification \
mysql://username:secret@database-server-mysql:3306/dbname \
"postgresql://username:secret@database-server-postgresql:5432/dbname"

--no-ssl-cert-verification skips verification of the server certificates, which is acceptable for local containers with self-signed certificates. pgloader creates the tables in a PostgreSQL schema named after the MySQL database (dbname here).

Fix common errors

MySQL: unsupported authentication

QMYND:MYSQL-UNSUPPORTED-AUTHENTICATION

MySQL 8 uses the caching_sha2_password authentication plugin by default, and the MySQL driver in pgloader 3.x does not implement it (pgloader issue #782). On MySQL 8.0, start the server with the older plugin so that the users created at initialisation use mysql_native_password:

database-server-mysql:
image: mysql:8.0.30
command: --default-authentication-plugin=mysql_native_password

The option only affects users created after it is set. For an existing user, recreate it or run ALTER USER ... IDENTIFIED WITH mysql_native_password BY '...'. MySQL 8.4 removed --default-authentication-plugin; for 8.4 and later, see the issue for the current workarounds.

PostgreSQL: “10 fell through ECASE expression”

FATAL error: Failed to connect to pgsql at "database-server-postgresql" (port 5432) as user "username": 10 fell through ECASE expression. Wanted one of (0 2 3 4 5 6 7 8).

This error comes from the PostgreSQL side, not from MySQL. Code 10 is the server asking for SCRAM-SHA-256 authentication, the default since PostgreSQL 14, and older pgloader builds do not support it (pgloader issue #1207). The workaround is to store the password as MD5 instead. With the official postgres image, pass the setting on the command line before the data directory is created; the entrypoint then also writes md5 into pg_hba.conf:

database-server-postgresql:
image: postgres:15.1
command: postgres -c password_encryption=md5

On an existing server, set password_encryption = md5 in postgresql.conf, change scram-sha-256 to md5 in pg_hba.conf, restart, and set the user’s password again so that it is stored as MD5.

ERROR: type “longtext” does not exist

PostgreSQL has no LONGTEXT type. Use TEXT, which has no length limit in PostgreSQL. You see this error when MySQL DDL is run against PostgreSQL directly; pgloader casts longtext to text on its own.

References

Related notes

6 min read

SQL joins

INNER, LEFT, RIGHT, FULL OUTER, CROSS and self joins in SQL, with two small example tables, the queries and their results.

  • Databases
5 min read

JDBC

JDBC driver, DataSource and XADataSource class names per database, with GlassFish and Payara connection pool examples.

  • Java & Jakarta EE
  • Databases
8 min read

JPA (Jakarta Persistence)

JPA recipes: java.time mappings, sequence generators, runtime persistence properties, bulk updates, JSON columns and inheritance.

  • Java & Jakarta EE
  • Databases

Type to search the whole site.