Use mysql_fdw to connect and query MySQL Enterprise Edition tables directly from PostgreSQL.
PostgreSQL can query MySQL Enterprise Edition through the mysql_fdw
foreign data wrapper. The extension lets you treat a remote MySQL database as local tables, enabling cross-database joins, analytics, and migrations without exporting CSV files.
mysql_fdw
extension?Install the wrapper package on the PostgreSQL host, then run:
CREATE EXTENSION IF NOT EXISTS mysql_fdw;
The command adds the necessary C library and SQL functions to your cluster.
CREATE SERVER mysql_ee_srv
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host 'mysql-ee.prod', port '3306');
This statement registers the remote MySQL Enterprise Edition instance so PostgreSQL knows where to forward queries.
CREATE USER MAPPING FOR app_readonly
SERVER mysql_ee_srv
OPTIONS (username 'report_user', password '••••••');
Use distinct read and write accounts, store passwords in pgpass
, and restrict roles with ALTER ROLE
.
IMPORT FOREIGN SCHEMA ecommerce
FROM SERVER mysql_ee_srv
INTO public
OPTIONS (charset 'utf8mb4');
PostgreSQL creates matching foreign tables—customers
, orders
, products
, and orderitems
—that point to MySQL Enterprise datasets.
SELECT c.name, SUM(oi.quantity * p.price) AS lifetime_value
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN orderitems oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY c.name
ORDER BY lifetime_value DESC
LIMIT 10;
The planner pushes filters and aggregates down to MySQL when possible, reducing network traffic.
Add relevant indexes in MySQL, set use_remote_estimate
to true
for large tables, and limit exposed columns. Prefer SSL connections and rotate passwords regularly.
Do not forget to match character sets; mismatches cause garbled text. Avoid importing massive tables without WHERE filters—use LIMIT
or partitions.
Install mysql_fdw
, create a server, map users, import schemas, then query as if the MySQL Enterprise Edition tables were native PostgreSQL objects—enjoy seamless cross-database analytics.
mysql_fdw
supported on Amazon RDS PostgreSQL?No. RDS blocks custom C extensions. Use an EC2-hosted PostgreSQL instance or AWS Aurora MySQL cross-region replication instead.
Yes. Grant INSERT/UPDATE/DELETE to the MySQL account and run standard DML against the foreign table. Ensure transactional expectations are clear—FDWs do not offer two-phase commit.
Run IMPORT FOREIGN SCHEMA
again or create new FOREIGN TABLE
definitions. Existing foreign tables do not auto-update when columns change.