Databases
Databases

How to load data from MySQL to Postgres destination

Learn how to use Airbyte to synchronize your MySQL data into Postgres destination within minutes.

TL;DR

This can be done by building a data pipeline manually, usually a Python script (you can leverage a tool as Apache Airflow for this). This process can take more than a full week of development. Or it can be done in minutes on Airbyte in three easy steps:

  1. set up MySQL as a source connector (using Auth, or usually an API key)
  2. set up Postgres destination as a destination connector
  3. define which data you want to transfer and how frequently

You can choose to self-host the pipeline using Airbyte Open Source or have it managed for you with Airbyte Cloud.

This tutorial’s purpose is to show you how.

What is MySQL

MySQL is an SQL (Structured Query Language)-based open-source database management system. An application with many uses, it offers a variety of products, from free MySQL downloads of the most recent iteration to support packages with full service support at the enterprise level. The MySQL server, while most often used as a web database, also supports e-commerce and data warehousing applications and more.

What is Postgres destination

An object-relational database management system, PostgreSQL is able to handle a wide range of workloads, supports multiple standards, and is cross-platform, running on numerous operating systems including Microsoft Windows, Solaris, Linux, and FreeBSD. It is highly extensible, and supports more than 12 procedural languages, Spatial data support, Gin and GIST Indexes, and more. Many web, mobile, and analytics applications use PostgreSQL as the primary data warehouse or data store.

Prerequisites

  1. A MySQL account to transfer your customer data automatically from.
  2. A Postgres destination account.
  3. An active Airbyte Cloud account, or you can also choose to use Airbyte Open Source locally. You can follow the instructions to set up Airbyte on your system using docker-compose.

Airbyte is an open-source data integration platform that consolidates and streamlines the process of extracting and loading data from multiple data sources to data warehouses. It offers pre-built connectors, including MySQL and Postgres destination, for seamless data migration.

When using Airbyte to move data from MySQL to Postgres destination, it extracts data from MySQL using the source connector, converts it into a format Postgres destination can ingest using the provided schema, and then loads it into Postgres destination via the destination connector. This allows businesses to leverage their MySQL data for advanced analytics and insights within Postgres destination, simplifying the ETL process and saving significant time and resources.

Step 1: Set up MySQL as a source connector

1. Open the Airbyte UI and navigate to the "Sources" tab.

2. Click on the "Add Source" button and select "MySQL" from the list of available sources.

3. Enter a name for your MySQL source and click on the "Next" button.

4. Enter the necessary credentials for your MySQL database, including the host, port, username, and password.

5. Select the database you want to connect to from the drop-down menu.

6. Choose the tables you want to replicate data from by selecting them from the list.

7. Click on the "Test" button to ensure that the connection is successful.

8. If the test is successful, click on the "Create" button to save your MySQL source configuration.

9. You can now use your MySQL connector to replicate data from your MySQL database to your destination of choice.

Step 2: Set up Postgres destination as a destination connector

Should you build or buy your data pipelines?

Download our free guide and discover the best approach for your needs, whether it's building your ELT solution in-house or opting for Airbyte Open Source or Airbyte Cloud.

Download now

Step 3: Set up a connection to sync your MySQL data to Postgres destination

Once you've successfully connected MySQL as a data source and Postgres destination as a destination in Airbyte, you can set up a data pipeline between them with the following steps:

  1. Create a new connection: On the Airbyte dashboard, navigate to the 'Connections' tab and click the '+ New Connection' button.
  2. Choose your source: Select MySQL from the dropdown list of your configured sources.
  3. Select your destination: Choose Postgres destination from the dropdown list of your configured destinations.
  4. Configure your sync: Define the frequency of your data syncs based on your business needs. Airbyte allows both manual and automatic scheduling for your data refreshes.
  5. Select the data to sync: Choose the specific MySQL objects you want to import data from towards Postgres destination. You can sync all data or select specific tables and fields.
  6. Select the sync mode for your streams: Choose between full refreshes or incremental syncs (with deduplication if you want), and this for all streams or at the stream level. Incremental is only available for streams that have a primary cursor.
  7. Test your connection: Click the 'Test Connection' button to make sure that your setup works. If the connection test is successful, save your configuration.
  8. Start the sync: If the test passes, click 'Set Up Connection'. Airbyte will start moving data from MySQL to Postgres destination according to your settings.

Remember, Airbyte keeps your data in sync at the frequency you determine, ensuring your Postgres destination data warehouse is always up-to-date with your MySQL data.

Use Cases to transfer your MySQL data to Postgres destination

Integrating data from MySQL to Postgres destination provides several benefits. Here are a few use cases:

  1. Advanced Analytics: Postgres destination’s powerful data processing capabilities enable you to perform complex queries and data analysis on your MySQL data, extracting insights that wouldn't be possible within MySQL alone.
  2. Data Consolidation: If you're using multiple other sources along with MySQL, syncing to Postgres destination allows you to centralize your data for a holistic view of your operations, and to set up a change data capture process so you never have any discrepancies in your data again.
  3. Historical Data Analysis: MySQL has limits on historical data. Syncing data to Postgres destination allows for long-term data retention and analysis of historical trends over time.
  4. Data Security and Compliance: Postgres destination provides robust data security features. Syncing MySQL data to Postgres destination ensures your data is secured and allows for advanced data governance and compliance management.
  5. Scalability: Postgres destination can handle large volumes of data without affecting performance, providing an ideal solution for growing businesses with expanding MySQL data.
  6. Data Science and Machine Learning: By having MySQL data in Postgres destination, you can apply machine learning models to your data for predictive analytics, customer segmentation, and more.
  7. Reporting and Visualization: While MySQL provides reporting tools, data visualization tools like Tableau, PowerBI, Looker (Google Data Studio) can connect to Postgres destination, providing more advanced business intelligence options. If you have a MySQL table that needs to be converted to a Postgres destination table, Airbyte can do that automatically.

Wrapping Up

To summarize, this tutorial has shown you how to:

  1. Configure a MySQL account as an Airbyte data source connector.
  2. Configure Postgres destination as a data destination connector.
  3. Create an Airbyte data pipeline that will automatically be moving data directly from MySQL to Postgres destination after you set a schedule

With Airbyte, creating data pipelines take minutes, and the data integration possibilities are endless. Airbyte supports the largest catalog of API tools, databases, and files, among other sources. Airbyte's connectors are open-source, so you can add any custom objects to the connector, or even build a new connector from scratch without any local dev environment or any data engineer within 10 minutes with the no-code connector builder.

We look forward to seeing you make use of it! We invite you to join the conversation on our community Slack Channel, or sign up for our newsletter. You should also check out other Airbyte tutorials, and Airbyte’s content hub!

What should you do next?

Hope you enjoyed the reading. Here are the 3 ways we can help you in your data journey:

flag icon
Easily address your data movement needs with Airbyte Cloud
Take the first step towards extensible data movement infrastructure that will give a ton of time back to your data team. 
Get started with Airbyte for free
high five icon
Talk to a data infrastructure expert
Get a free consultation with an Airbyte expert to significantly improve your data movement infrastructure. 
Talk to sales
stars sparkling
Improve your data infrastructure knowledge
Subscribe to our monthly newsletter and get the community’s new enlightening content along with Airbyte’s progress in their mission to solve data integration once and for all.
Subscribe to newsletter

In the evolving information technology landscape, data transfer between database systems has become crucial. This is because of specific use cases, features, and functionalities that different tools have to offer. One common example of data migration is between MySQL and PostgreSQL.

You might want to transition data from MySQL to Postgres because of Postgres’s support for advanced data types like arrays and JSON, a huge library of ready-to-use extensions, or just any other specific feature that is limited in MySQL.

This article will teach you two straightforward methods to load data from MySQL to Postgres.

MySQL Overview

Image Source: MySQL

MySQL is a widely known Relational Database Management System (RDMS). The syntax and features of this database are similar to Oracle and PostgreSQL, as it uses Structured Query Language (SQL) as its primary language. Like other RDMS, MySQL allows you to store and manage data in tables within rows and columns. The structured nature of MySQL is ideal for organizations that require robust data consistency, reliability, and integrity. Some features that make it a popular database include transaction support, outstanding scalability, and strong security measures. 

Some of the key features of MySQL are: 

  • ACID Compliance: ACID stands for Atomicity, Consistency, Isolation, and Durability, a four-layer framework in MySQL that ensures the database transactions are reliable. This dependability makes it a good choice for applications that demand robust data integrity, such as financial and hospitality systems. 
  • Compatibility: MySQL is a platform-independent database that supports almost all operating systems, including Linux, Windows, and MacOS. In addition, it supports multiple programming languages, including Python, PHP, Java, and more, to make databases more accessible to developers. With this compatibility, you can ensure flexibility across different environments using MySQL. 

PostgreSQL Overview

Image Source: PostgreSQL

PostgreSQL, also known as Postgres, is one of the traditional Object-RDBMS. From its creation in 1986 at the University of California, the development community of this database has been actively working on its technology enhancements. Postgres is an advanced enterprise-level database that supports both structured and semi-structured data. For semi-structured data, it has native support for JavaScript Object Notation (JSON) and Binary JSON (JSONB) data types, and for structured data, it conforms with SQL standards. 

Key features of PostgreSQL:

  • Open-source: Postgres also has an open-source environment. It has a robust development community supported by a large ecosystem of contributors and documentation. In addition, its open-source nature allows you to work in collaborative environments, which encourages developers to root for its improvement. 
  • Extensions: Postgres offers many built-in extensions that are pre-built templates or add-on modules used for enhanced functionality of database solutions. These extensions include: pgcrypto (gives you the ability to write cryptographic functions within the database) and PostGIS (allows you to handle spatial data). 

Check out our article on PostgreSQL vs MySQL to quench your thirst for learning and gain deeper insights into database management.

Methods to Migrate Data From MySQL to PostgreSQL

  • Method 1: Connecting MySQL to PostgreSQL using Airbyte.
  • Method 2: Migrating data from MySQL to PostgreSQL manually. 

Method 1: Connecting MySQL to PostgreSQL Using Airbyte

Airbyte is like a data engineer's secret weapon! With its powerful capabilities, you can  set up various Data Integrations including MySQL to BigQuery and MySQL to Redshift, among many other connections. It's the perfect tool to supercharge your data engineering projects and make them shine!

With over 350+ pre-built connectors and a customizable pipeline configuration, it streamlines the complex tasks required to build specific data pipelines for migrating data from MySQL to Postgres. Here’s a detailed guide on migrating data between both tools: 

Step 1: Configure MySQL As a Source

  • Create an Airbyte account or log in to an existing one. 
  • On the home page, click the Sources option from the left navigation. 
  • Look for the search field in the Sources page and type MySQL. Click on the MySQL connector card that appears. 
  • You’ll be directed to the Create a source page of MySQL. There, fill in details in text fields including Host, Port, Database, Username, Password, and SSL modes
  • Then, in the Update Method section. Select between: Read Changes using Binary Log (CDC) and Scan Changes with User-Defined Cursor. The first method is recommended method by Airbyte that Incrementally reads new inserts, updates, and deletes using the MySQL binary log. The other method incrementally detects new inserts and updates using the cursor column chosen when configuring a connection. 
  • Configure other optional fields as per your requirement and click Set up source

Step 2: Configure Postgres As a Destination

  • After configuring MySQL, click on the Destinations section from the left navigation bar. 
  • On the Destinations page, type in Postgres in the search bar. 
  • Click on the Postgres connector card.
  • You’ll be directed to Create a destination page. Fill in the details such as Host, Port, DB Name, Default Schema, User, SSL modes, and SSH Tunnel Method
  • Optionally, configure optional fields and click Set up destination

Step 3: Create A Connection Between MySQL And Postgres 

  • Now that you have configured the source and destination click on Connections from the left navigation bar.

 

  • On the Connections page, click Create a connection button and select MySQL as a source and Postgres as a destination to establish a connection.
  • Provide a unique Connection name for the connection and select Replication frequency. You can choose to tweak the Streams section and select sync mode.
  • Click on Set up connection > Sync now to run synchronization. 

You have successfully installed a connection between MySQL and PostgreSQL using Airbyte. 

Method 2: Migrating Data From MySQL to PostgreSQL Manually

In this method, you will parse SQL files from MySQL to PostgreSQL and manually move data from one database to another. Here is a detailed guide on how to export MySQL data to Postgres: 

Prerequisites

Step 1: Create PostgreSQL Schema

Before moving forward with the migration process, a clear understanding of the changes required to work with Postgres is needed. In this step, you must create a new schema in the PostgreSQL database and match the structure of the MySQL database, Since each database uses its own data types, storage formats, and SQL syntax, this step is very crucial. You can choose to do schema conversion manually by tweaking data types, as both databases have partially similar structures. However, you can also use libraries such as DBConvert to do this task for you. 

Step 2: Export Data From MySQL

Open the terminal and navigate to the directory where MySQL is installed. Type in the following command:

mysql -u username -p

Replace username with MySQL username.

Now, use the mysqldump command to export the MySQL database into a file. Type this command to perform this task:

mysqldump -u username -p your_database > mysql_data.sql

Replace username with MySQL username, your_database with the name of MySQL database, and mysql_data.sql with the file name in which you want to store exported data.

Note: You can also choose to export specific tables and objects from the MySQL database using the mysqldump command.  

After you get the file, type in mysql exit and close the terminal.

Step 3: Import Data into PostgreSQL

Open the terminal and go to the Postgres directory. Run the following code to access the database:

psql -U username -d your_database

From the above code, replace username with your Postgres username and your_database with the database name you want to access.

Now you are connected, you must import the data of the MySQL database to Postgres. Run the following code to achieve this task:

psql -U username -d your_database -f mysql_data.sql

The above command will connect to a specific database (your_database) as a specified user (username) and execute the SQL commands contained in the mysql_data file while importing the data.

Note: The schema in your Postgres database should be compatible with data in mysql_data.sql file. Therefore, make sure Step 1 is carried out carefully.

That concludes it. If you have followed every step of the manual method, you can migrate data from MySQL to Postgres without difficulty.

Limitations of Using a Manual Method

  • Time Consumption and Expertise: Manually connecting MySQL to Postgres can be complicated and time-consuming, particularly for huge databases. It requires a deep level of expertise and a thorough understanding of both database's work in terms of data types, SQL syntax, and storage formats. 
  • Error-prone: The manual method requires converting schema and custom coding for moving data from MySQL to Postgres. Tweaking data types, SQL syntax, and writing codes increases the risk of human error, which can lead to data loss and integrity issues.
  • Lacks Automation: Compared to specialized migration tools like Airbyte, the manual method lacks automation capabilities. This can be human resource intensive as you have to execute and monitor each step carefully.

Conclusion 

You now know two easy methods for synchronizing data between MySQL and Postgres. The first method uses Airbyte to automate the connection between both tools. You have to configure MySQL as a source and Postgres as a destination and create a connection between them. Just like that, the migration will be successful within a few clicks. The other method involves a manual intervention to complete the same task. You have to create and verify the schema structure as well as export MySQL data into a .sql file before uploading it to the Postgres table. This process is time-consuming and has a lot of limitations mentioned above.

Integrating data from MySQL to Postgres brings a whole bunch of benefits to the table. And when you utilise one of the powerful data migration tools like Airbyte to the mix, it's like adding a whole new set of feathers to its cap! Airbyte's rich functionalities, like 350+ pre-built connectors, robust orchestration capabilities, and an easy-to-use interface, make data integration easier.

What should you do next?

Hope you enjoyed the reading. Here are the 3 ways we can help you in your data journey:

flag icon
Easily address your data movement needs with Airbyte Cloud
Take the first step towards extensible data movement infrastructure that will give a ton of time back to your data team. 
Get started with Airbyte for free
high five icon
Talk to a data infrastructure expert
Get a free consultation with an Airbyte expert to significantly improve your data movement infrastructure. 
Talk to sales
stars sparkling
Improve your data infrastructure knowledge
Subscribe to our monthly newsletter and get the community’s new enlightening content along with Airbyte’s progress in their mission to solve data integration once and for all.
Subscribe to newsletter

Frequently Asked Questions

What data can you extract from MySQL?

MySQL provides access to a wide range of data types, including:

1. Numeric data types: These include integers, decimals, and floating-point numbers.

2. String data types: These include character strings, binary strings, and text strings.

3. Date and time data types: These include date, time, datetime, and timestamp.

4. Boolean data types: These include true/false or yes/no values.

5. Spatial data types: These include points, lines, polygons, and other geometric shapes.

6. Large object data types: These include binary large objects (BLOBs) and character large objects (CLOBs).

7. Collection data types: These include arrays, sets, and maps.

8. User-defined data types: These are custom data types created by the user.

Overall, MySQL's API provides access to a wide range of data types, making it a versatile tool for managing and manipulating data in a variety of applications.

What data can you transfer to Postgres destination?

You can transfer a wide variety of data to Postgres destination. This usually includes structured, semi-structured, and unstructured data like transaction records, log files, JSON data, CSV files, and more, allowing robust, scalable data integration and analysis.

What are top ETL tools to transfer data from MySQL to Postgres destination?

The most prominent ETL tools to transfer data from MySQL to Postgres destination include:

  • Airbyte
  • Fivetran
  • Stitch
  • Matillion
  • Talend Data Integration

These tools help in extracting data from MySQL and various sources (APIs, databases, and more), transforming it efficiently, and loading it into Postgres destination and other databases, data warehouses and data lakes, enhancing data management capabilities.