Migrate data from PostgreSQL to KakaoCloud MySQL
This guide explains how to migrate data from PostgreSQL to KakaoCloud MySQL.
- Estimated Time: 30 minutes
- Recommended OS: macOS, Ubuntu
- Prerequisites
About scenario
In this scenario, we’ll walk through how to migrate data from PostgreSQL to KakaoCloud MySQL. Key steps include:
- Setting up the migration environment: Configure security groups and create a bastion host to prepare for data migration.
- Creating a MySQL database: Provision a managed MySQL instance in KakaoCloud console.
- Transforming PostgreSQL data: Generate a PostgreSQL dump using
pg_dumpand convert it to MySQL format using the pg2mysql converter tool. - Importing and verifying MySQL data: Import the transformed data into the MySQL instance and confirm data integrity.

pg2mysql Converter Tool is an open-source software based on PHP. For more details, please refer to the official webpage.
Before you start
1. Network environment setup
Before setting up data migration and replication on KakaoCloud, configure the network environment by creating a VPC and public subnet based on the following values:
-
Go to KakaoCloud console > Networking > VPC.
-
Select the [+ Create VPC] button and configure the VPC and subnet as shown below:
Category Item Setting/Input Value VPC Information VPC Name tutorial VPC IP CIDR Block 10.0.0.0/16 Availability Zone Number of Availability Zones 1 First AZ kr-central-2-a Subnet Settings Number of Public Subnets per AZ 1 kr-central-2-a Public subnet IPv4 CIDR block: 10.0.0.0/20 -
Verify the topology displayed at the bottom and select [Create] if it is correct.
- The status of the subnet will change in the following order:
Pending Create>Pending Update>Active. Ensure the status isActivebefore proceeding.
- The status of the subnet will change in the following order:
2. Configure security group
Configuring security groups is essential for securing data transfer and allowing required traffic.
Security Group: tutorial-bastion-sg
-
Go to KakaoCloud console > VPC > Security Group and create a security group based on the table below:
Name Description (optional) tutorial-bastion-sg Security policy for the bastion host -
Select [+ Add] to set the following inbound rules, and then select [Apply].
Inbound Policy Item Setting bastion inbound policy 1 Protocol TCPSource IP {Your Public IP}/32Port Number 5432 Policy description (optional) bastion inbound policy 1 bastion inbound policy 2 Protocol TCPSource IP {Your Public IP}/32Port Number 22 Policy description (optional) bastion inbound policy 2
3. Set up bastion host
The bastion host serves as a gateway for securely accessing cloud instances remotely for data migration and MySQL access.
-
Go to KakaoCloud console > Beyond Compute Service > Virtual Machine.
-
Refer to the table below to create a VM instance that will serve as the bastion host.
Category Item Setting/Input Value Notes Basic Info Name tutorial-migration Count 1 Image Ubuntu 20.04 Instance Type m2a.large Volume Root Volume 20 GB Key Pair {PRIVATE_KEY}Store the key file securely when it is first created. A lost key cannot be recovered and must be reissued. Network VPC tutorial Subnet main (10.0.0.0/20) Security Group tutorial-bastion-sg
4. Create MySQL database instance
Create a MySQL instance to serve as the target for data migration. KakaoCloud’s MySQL is a fully managed database service that provides secure access within a logically isolated network.
-
Go to KakaoCloud console > Data Store > MySQL.
-
Under the Instance Group tab, select [Create Instance Group] and create an instance group using the following values:
Category Item Setting/Input Value Notes Basic Settings Instance Group Name tutorial-mysql Description Optional MySQL Settings Engine Version Use specified version MySQL User Name admin MySQL Password root1234 Instance Availability Single (Primary Instance) Instance Type m2a.large Storage Storage Type/Size 100 Log Storage Log Storage Type/Size 100 Network VPC tutorial Subnet main (10.0.0.0/20) Automatic Backup Automatic Backup Option Disabled
Migration steps
Step 1. Configure instance for migration
-
In Virtual Machine > Instance tab, select the pre-created bastion host
tutorial-migrationand select [Assign Public IP] to assign a public IP.- The assigned Public IP can be checked under the Network tab.
-
Open a terminal on your local machine and use
cdto navigate to the folder containing the downloaded key file.- If you downloaded the key file when you created it, it is normally saved in your Downloads folder (
cd ~/Downloads).
- If you downloaded the key file when you created it, it is normally saved in your Downloads folder (
-
Run the following commands to connect to the bastion host over SSH:
chmod 400 {PRIVATE_KEY}.pem # Grant read permission
ssh -i {PRIVATE_KEY}.pem ubuntu@{MIGRATION_PUBLIC_IP}Item Description {PRIVATE_KEY}Name of the key file {MIGRATION_PUBLIC_IP}Public IP address of the tutorial-migrationinstance -
Set up the migration instance:
Configure migration instancesudo apt-get update -y
sudo apt-get install php unzip postgresql mysql-client -y
wget https://www.easyredmine.com/ER/media/knowledge-base/pg2mysql-1.9.zip
unzip pg2mysql-1.9.zip
Step 2. Creating a PostgreSQL data dump
-
First, prepare an example table and data in PostgreSQL. Connect to PostgreSQL:
sudo -u postgres psqlRun the following commands in PostgreSQL to prepare the example table and data. Set a password for the
postgresaccount, then exit with\q.Create example tableCREATE TABLE public.users (
id INT NOT NULL,
name VARCHAR(50),
email VARCHAR(50)
);
INSERT INTO public.users (id, name, email) VALUES
(1, 'Alice', 'alice@example.com'),
(2, 'Bob', 'bob@example.com'),
(3, 'Charlie', 'charlie@example.com');
ALTER TABLE public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
-- Set password
\password postgres
-- Exit
\q -
Create a dump of the example table:
Create dump filecd pg2mysql-1.9/
pg_dump -U postgres -h 127.0.0.1 -p 5432 -n public \
--format=p > PG_DUMP_FILE.sql -
Convert the PostgreSQL dump to MySQL format:
Convert to MySQL formatphp pg2mysql_cli.php PG_DUMP_FILE.sql MYSQL_DUMP_FILE.sql innodb -
Edit the converted MySQL file as shown below.
Example: MySQL dump file before editing# Converted with pg2mysql-1.9
# Converted on Thu, 25 Jul 2024 05:11:47 +0000
# Lightbox Technologies Inc. http://www.lightbox.ca
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-- SET time_zone="+00:00"; -- Remove if required by your environment.Example: MySQL dump file after editing# Converted with pg2mysql-1.9
# Converted on Thu, 25 Jul 2024 05:11:47 +0000
# Lightbox Technologies Inc. http://www.lightbox.ca
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-- Create database
CREATE DATABASE psql_to_mysql;
-- Select database
USE psql_to_mysql;
CREATE TABLE users (
id int(11) NOT NULL,
name varchar(50),
email varchar(50)
) ENGINE=innodb;
INSERT INTO users (id, name, email) VALUES
('1', 'Alice', 'alice@example.com'),
('2', 'Bob', 'bob@example.com'),
('3', 'Charlie', 'charlie@example.com');
ALTER TABLE users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);infoThe converted SQL file contains MySQL table definitions. KakaoCloud MySQL does not have a default database, so first create one using
CREATE DATABASE.
Step 3. Import data into KakaoCloud MySQL instance
Import the MySQL dump file into the KakaoCloud MySQL instance.
mysql -u {USER} -h {MYSQL_ENDPOINT} -p < MYSQL_DUMP_FILE.sql
Step 4. Verify data consistency
Verify the data consistency of the imported database in KakaoCloud MySQL.
mysql -u {USER} -h {MYSQL_ENDPOINT} -p
mysql> use {DATABASE_NAME};
Database changed
mysql> select * from {TABLE_NAME};