How to self-host MariaDB in PostgreSQL

Galaxy Glossary

How do I self-host MariaDB with Docker and connect it to PostgreSQL?

Host your own MariaDB server on local or cloud infrastructure, giving you full control over configuration, security, and cost.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why self-host MariaDB instead of using a cloud service?

Self-hosting avoids recurring fees, keeps data on your infrastructure, and lets you fine-tune performance and security to match internal policies.

What prerequisites should be in place?

Have Docker (or Podman) installed, open TCP port 3306, and reserve at least 2 GB RAM plus persistent storage for /var/lib/mysql.

How do I launch MariaDB with Docker?

Run one command: docker run --name mariadb -e MARIADB_ROOT_PASSWORD=StrongPass123 -e MARIADB_DATABASE=shop -e MARIADB_USER=app -e MARIADB_PASSWORD=AppPass123 -p 3306:3306 -v /srv/mariadb/data:/var/lib/mysql -d mariadb:10.11. It pulls the image, boots the server, and mounts data on the host.

Which parameters matter most?

MARIADB_ROOT_PASSWORD sets the super-user password. MARIADB_DATABASE, MARIADB_USER, and MARIADB_PASSWORD create an application database and user on first start. -v binds host storage for durability.

How do I initialise an ecommerce schema?

Connect with docker exec -it mariadb mysql -uroot -p and run the DDL in the next code block to create Customers, Orders, Products, and OrderItems.

How can PostgreSQL query the self-hosted MariaDB?

Install mysql_fdw in PostgreSQL, create a foreign server, and import the schema so you can SELECT from MariaDB tables inside PostgreSQL.

What best practices improve reliability?

Place data on redundant storage, schedule dumps with mariabackup, and pin to a specific MariaDB image tag for predictable upgrades.

Which common mistakes should I avoid?

Do not expose port 3306 to the internet without a firewall, and never forget to back up the host volume before upgrading the image.

Why How to self-host MariaDB in PostgreSQL is important

How to self-host MariaDB in PostgreSQL Example Usage


-- Insert a customer and an order inside MariaDB
INSERT INTO Customers (name, email) VALUES ('Jane Doe','jane@example.com');

INSERT INTO Orders (customer_id, order_date, total_amount)
VALUES (LAST_INSERT_ID(), CURRENT_DATE, 199.99);

-- PostgreSQL querying the remote MariaDB order through mysql_fdw
SELECT o.id, c.name, o.total_amount
FROM orders_fdw o
JOIN customers_fdw c ON c.id = o.customer_id;

How to self-host MariaDB in PostgreSQL Syntax


# Quick single-node Docker run

docker run \
  --name mariadb \
  -e MARIADB_ROOT_PASSWORD=<root_pw> \
  -e MARIADB_DATABASE=shop \
  -e MARIADB_USER=app \
  -e MARIADB_PASSWORD=<app_pw> \
  -p 3306:3306 \
  -v /srv/mariadb/data:/var/lib/mysql \
  -d mariadb:10.11

# docker-compose.yml alternative

version: "3.9"
services:
  mariadb:
    image: mariadb:10.11
    container_name: mariadb
    restart: unless-stopped
    ports:
      - "3306:3306"
    environment:
      MARIADB_ROOT_PASSWORD: StrongPass123
      MARIADB_DATABASE: shop
      MARIADB_USER: app
      MARIADB_PASSWORD: AppPass123
    volumes:
      - mariadb_data:/var/lib/mysql
volumes:
  mariadb_data:

# Sample ecommerce schema inside MariaDB

CREATE TABLE Customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    email VARCHAR(150) UNIQUE,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE Products (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(120),
    price DECIMAL(10,2),
    stock INT
);

CREATE TABLE Orders (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT,
    order_date DATE,
    total_amount DECIMAL(12,2),
    FOREIGN KEY (customer_id) REFERENCES Customers(id)
);

CREATE TABLE OrderItems (
    id INT PRIMARY KEY AUTO_INCREMENT,
    order_id INT,
    product_id INT,
    quantity INT,
    FOREIGN KEY (order_id) REFERENCES Orders(id),
    FOREIGN KEY (product_id) REFERENCES Products(id)
);

Common Mistakes

Frequently Asked Questions (FAQs)

Is Docker the only way to self-host MariaDB?

No. You can install native packages with apt or yum, but Docker simplifies upgrades and isolation.

How do I back up a self-hosted MariaDB?

Use mariabackup --backup for hot backups or mysqldump --single-transaction for logical dumps. Store backups off-site.

Can I run replication in a self-hosted setup?

Yes. Start a secondary container, configure server_id values, and run the CHANGE MASTER TO command to enable replication.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.