Alex Kirpichny

Alexandr Kirpichny

Chief Product Officer, Ispirer Systems

MariaDB is a robust database suitable for traditional web and enterprise apps, e-commerce platforms, and other applications where speed and flexibility are the top priority. However, many companies are looking towards other databases that boast robust data integrity and wide query capabilities. One of the examples is PostgreSQL.

Migrating from MariaDB to PostgreSQL can be a daunting task, whether approached manually or through automated tools. While data migration remains a cornerstone of any such project, it is far from the only consideration. Beyond the data migration, the nuanced complexities of schema conversion also demand meticulous attention.

One notable solution to expedite this intricate process is Ispirer Toolkit, which ensures high-quality migration and shortened migration time. In this article we will analyze the entire process of migrating from MariaDB to PostgreSQL from start to finish, we will look in detail at all the difficulties along the conversion path and their solutions.

What is MariaDB?

MariaDB is a popular open source, relational database management system, a fork of MySQL. It was created in 2009 by MySQL creators after MySQL was acquired by Oracle.

MariaDB is a fast and scalable database that supports a wider range of storage engines compared to MySQL. It is flexible, as it supports external plugins, which makes it a versatile database appropriate for a large number of use cases, including e-commerce, data warehousing, and logging applications.

What is PostgreSQL?

PostgreSQL is considered one of the most powerful object-relational databases on the market. This database is widely known for its reliability, data integrity, powerful query capabilities, and ACID support, making it an excellent choice for those looking for an efficient, flexible, and secure system. PostgreSQL offers true ACID semantics for transactions and has full support for foreign keys, joins, views, triggers, and stored procedures in many different languages.

How to migrate MariaDB to PostgreSQL?

In order to migrate MariaDB to PostgreSQL, you need to follow a number of steps, including Assessment, SQL conversion, Data migration, Testing and Cutover. Let’s review all the steps in more detail.

Step 1. MariaDB Assessment

A preliminary evaluation of the database before migration involves analyzing the original system and identifying possible issues. Typically, a group of experienced professionals would conduct this database evaluation, but we chose to streamline this procedure.

The Ispirer team has created the Assessment Wizard to enhance and expedite the database assessment process. This tool is engineered to conduct a comprehensive examination of any database and provide an estimation for a migration project.

Assessment Wizard connects to a MariaDB database and gathers data about objects, their internal structures, and statements. The tool generates detailed reports that enable the estimation of migration costs and the timeline of a migration project with a fair degree of precision. A sample report from Assessment Wizard can be viewed below:

MariaDB to PostgreSQL Assessment

Step 2. SQL Conversion

SQL code migration is the most labor-intensive stage of the migration process, often taking months to years depending on the code complexity, project scope, and requirements. Automating the migration process can significantly save time and human resources.

Given the diversity of modern relational databases, each has unique features that necessitate thorough examination and additional analysis before migration can commence. These differences in SQL syntax and functionality require meticulous adjustments and substantial effort to reproduce the functionality of the source database in the target system. Migrating from MariaDB to PostgreSQL exemplifies this challenge, as there are numerous differences, including variations in Result Sets, exception handling (including SQLSTATE), global variables, and queries to system objects.

All these difficulties can be eliminated by automating the migration process with Ispirer Toolkit. This tool contains at its core thousands of conversion rules that determine the quality of the conversion. Thanks to them, the quality of automated MariaDB to PostgreSQL conversion remains high. If at the assessment stage it turns out that the database contains a large number of non-automated code structures, then Ispirer Toolkit can be customized to increase the level of automation.

Step 3. MariaDB to PostgreSQL data migration

There are three methods that will help you move your data from MariaDB to Postgres.

  • Method 1: Using Ispirer Toolkit
  • Method 2: Using CSV files
  • Method 3: Using pgloader

Method 1. Using Ispirer Toolkit

Ispirer Toolkit excels in advanced data type mapping and conversion, ensuring accurate and smooth data transfer between source and target databases. It automatically maps data types from the source with their corresponding types in the target database, managing differences in definitions, sizes, and formats.

Moreover, the toolkit allows for configuration of data type mappings and the creation of conversion rules to meet specific needs or preferences. This guarantees that the data maintains its integrity and consistency throughout the migration process.

To enhance performance and reduce downtime during migration, the Ispirer Toolkit uses efficient data transfer methods. It supports various techniques, such as bulk data loading and parallel processing, tailored to the characteristics of the source and target databases. These methods expedite the migration, especially for large data volumes, by maximizing throughput and minimizing latency.

Method 2. Using SCV file

You can export your data from MariaDB as CSV files to upload to PostgreSQL. Here are the steps to export data:

  1.  
  2. select * from customers
  3. INTO OUTFILE 'customers.csv'
  4. FIELDS ENCLOSED BY '"'
  5. TERMINATED BY ','
  6. ESCAPED BY '"'
  7. LINES TERMINATED BY '\n';
  8.  

This command will export data from the customers table as a customers.csv file.

To import the CSV file into a Postgres database, you can use the COPY command as follows:

  1.  
  2. COPY customers(first_name,last_name,contact_number,city)
  3. FROM ‘C:customers.csv’
  4. DELIMITER ‘,’
  5. CSV HEADER;
  6.  

This instruction will duplicate data from the origin file named customers.csv to the destination table called customers, with the array of columns indicated in brackets. You can apply the same procedure to copy data from MariaDB to Postgres for other tables as well. However, if you're dealing with a huge number of tables, the process of migrating data can become time-consuming.

Method 3. Using pgloader

Pgloader is a free software that enables you to transfer data from databases such as MySQL, SQLite, and MS SQL Server to PostgreSQL. MariaDB has extra features, but it still meets the same enterprise needs as MySQL. This means that software that works with MySQL will also work with MariaDB. To get started with MariaDB to PostgreSQL migration, follow these algorithm:

  • First, update & upgrade the apt package in your instance.
  1.  
  2. apt update && apt upgrade -y
  3.  
  • Second, install pgloader for your MariaDB PostgreSQL Integration process.
    1.  
    2. apt install pgloader
    3.  
  • Third, make sure that the tool is installed successfully and check the pgloader version.
    1.  
    2. pgloader --version
    3. </code> </pre>
    4. <li>Fourth, assuming that you have created the desired PostgreSQL database in your instance, run the following command to start the MariaDB to PostgreSQL Integration process. </li>
    5. <pre class="language-text"><code>
    6. pgloader mysql://mysqluser:password@<mysql-server>:<mysql-port>/<source-database> postgresql://<pgsql-role>:password@<pgsql_server>:<postgresql-port>/<target-database>
    7.  

    You might encounter an error if pgloader fails to perform the data type conversion properly when migrating from MariaDB to PostgreSQL. To resolve this, you can run the cast modifier commands first, followed by Step 4. For example,

    1.  
    2. pgloader --cast "type float to real drop typemod" \
    3. --cast "type int to integer drop typemod" \
    4. --cast "type bigint when (= 20 precision) to bigint drop typemod" \
    5.  

    This method of data migration can be labor intensive for databases with large volumes of data. It is rather impractical to use pgloader for a large migration scope. Instead, you can use Ispirer Toolkit. It has a graphical interface, Command Line Mode, and the ability to work with large volumes of databases, which optimizes the migration process and saves your time.

    Step 4. Testing

    The testing stage following a migration project is critical to ensure the integrity and functionality of the migrated system. During this phase, the primary difficulties include verifying data accuracy, ensuring system compatibility, and validating performance under real-world conditions.

    Testing is usually carried out in two steps - functional testing and stress testing. Testing can be carried out by the client's team, which is familiar with the system. However, Ispirer’s QA specialists can conduct testing too, as well as provide a range of additional services.

    Step 5. Cutover

    Cutover usually occurs during downtime. This period is strategically chosen to minimize disruption to business operations, typically during off-peak hours. Although the cutover process itself does not necessitate an extensive amount of downtime, it is crucial to factor in this window when planning database-related tasks. Proper scheduling ensures that all necessary actions, such as final data synchronization, system configuration, and validation checks, are completed within the allotted time. By meticulously planning the cutover phase, businesses can ensure that the migration is completed efficiently, with minimal disruption to users and services.

    Wrapping up

    This article offers a guide on migrating databases from MariaDB to Postgres. It outlines the key subtleties involved in database migration.

    To migrate your database from MariaDB to PostgreSQL without a hitch, utilize Ispirer Toolkit. It supports automatic migration from 30+ databases, including MariaDB and PostgreSQL. You can adjust it to fit your needs and make the process fully automated, regardless of its volume and unique features.

    Experience the toolkit firsthand with a free demo license. Begin the conversion process of your MariaDB database now!