Install mysql_fdw so PostgreSQL can query MariaDB tables through foreign data wrappers.
Running analytics in PostgreSQL while transactional data lives in MariaDB is common. The mysql_fdw extension lets you query MariaDB tables from Postgres without ETL, keeping data fresh and reducing duplication.
You need PostgreSQL 13+, superuser rights, network access to the MariaDB server, and the system packages postgresql-server-dev and libmysqlclient-dev so that mysql_fdw can compile.
On Debian-based Linux run: sudo apt install postgresql-13-mysql-fdw
. For source install, clone the GitHub repo, run make && sudo make install
, then restart PostgreSQL so the shared library loads.
Connect to the target database and run CREATE EXTENSION IF NOT EXISTS mysql_fdw;
. The command registers the foreign-data wrapper and makes the server and user-mapping commands available.
Define the remote host: CREATE SERVER mariadb_srv FOREIGN DATA WRAPPER mysql_fdw OPTIONS(host 'maria.prod', port '3306');
Then map credentials: CREATE USER MAPPING FOR CURRENT_USER SERVER mariadb_srv OPTIONS(username 'report', password '********');
Run IMPORT FOREIGN SCHEMA ecommerce FROM SERVER mariadb_srv INTO public;
. Postgres creates foreign tables Customers, Orders, Products, and OrderItems that point to the original MariaDB objects.
Use normal SQL: SELECT c.name, o.total_amount FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.order_date > CURRENT_DATE - INTERVAL '30 days';
. Postgres plans the join; mysql_fdw pushes filters to MariaDB when possible.
Index join columns in MariaDB, set OPTIONS(use_remote_estimate 'true')
on the server for better planner stats, and restrict imported columns to those you need to cut network overhead.
Do not store sensitive passwords in plain text; use CREATE USER MAPPING
with a dedicated least-privilege MariaDB account. Avoid wide SELECT *
queries; fetch only required columns to keep latency low.
Yes. Use the IMPORT FOREIGN SCHEMA ... LIMIT TO (table1, table2) clause or create specific foreign tables with CREATE FOREIGN TABLE
.
It uses libmysqlclient under the hood, so parameterized queries are supported and help prevent SQL injection.
Create indexes on filter and join columns in MariaDB, enable use_remote_estimate, and fetch only the columns you need.