CYBERTEC PostgreSQL Logo

Exploration: Migration to CNPG

08.2025
Category: 

Introduction

In our CNPG series, we have explained how to create a PostgreSQL cluster and how to customize it. However, most of the time, we don't have the luxury to start to use a database from scratch. We might have already started a project without high availability and we might be looking for an easy-to-manage environment on cloud native environment. In this case, CNPG is the answer, but how do we migrate our existing cluster to CNPG? In today's article, we will explain that.

There are a couple ways to migrate an existing cluster to CNPG; pg_basebackup, import, and logical replication. We will examine pg_basebackup and import methods today. Depending on the use case or the requirement, we might need to use a different method.

Requirements and Limitations for pg_basebackup Method

pg_basebackup, as usual, does let us create a physical copy from a source cluster. Because we are creating a physical copy of the existing cluster, the limitations are a bit more strict than for the import method. The requirements for pg_basebackup are as follows;

  • Source and target hardware architecture must be the same.
  • Source and target major PostgreSQL version must be the same.
  • On the source, we need an appropriate user with REPLICATION privilege.
  • The source must accept connections from the target cluster using replication user.
  • On the source, we need enough max_wal_sender for pg_basebackup and for streaming WAL segments.

As of now, CNPG version v1.27 has only one limitation for pg_basebackup, snapshot copy. In order to start creating the physical copy pg_basebackup takes a snapshot of the source cluster. Concurrently, it also streams WAL segments to the target cluster from the beginning of the process until the end of the backup. Once this process is completed, the cluster diverges and starts on a new timeline from the source. That is why all write operations on the source must be stopped before starting the migration process.

Note: this limitation only applies when the target instance is not specified as replica.

Checking The Source Cluster

Let's check the existing single instace cluster:

It is a pretty simple source setup but it is enough for our examination.

Migration to CNPG

For migration we need a manifest like the following:

In this manifest, we tell CNPG operator that we will bootstrap an instance using pg_basebackup and as a source we will use source - cluster. On the definition of source - cluster externalcluster we specify source instance connection parameters.

Apply the manifest:

Check Target Cluster

Let's connect to the target cluster and verify our migration:

Requirements and Limitations for import Method

The import method basically uses the pg_dump and pg_restore, which is why we need a downtime for migration, as is done on pg_basebackup. Yet, because it is a logical backup, it supports migration between different versions, so while migrating to CNPG we can also upgrade our databases. As aforementioned in the pg_basebackup section it has less requirements, but still the same limitation applies to the import method; before starting the migration process we need to stop write operation on the source instance.

  • On the source, we need an appropriate migration user with READ privilege on the relevant database for all objects.
  • If one or multiple roles are going to be imported, the source migration user needs privileges over catalog objects.
  • The source must accept connections from the target cluster using migration user.

Checking The Source Cluster

For this method, we use the same source cluster. However, our source database is to_be_cloned.

Migration to CNPG

For migration with import we use the following manifest:

This manifest is slightly different than the one we use in the pg_basebackup method. On bootstrap section we use initdb instead of pg_basebackup. Afterwards, we tell the cluster to import data using an external source that we define. The other difference is the target database name. In this example, we import the data into the to_be_cloned_target database. That means our configuration can be changed under the boundaries of initdb.

Apply the manifest:

For more detail, you can run kubectl logs before the import job is completed:

 

Check Target Cluster

Let's connect to the target cluster and verify our migration:

As we expected, our import is successfully completed and the target database is named to_be_cloned_target.

Conclusion

CNPG allows us to migrate our existing database cluster into Kubernetes environment safely with pg_basebackup and import methods. Depending on the use case and requirement, the method will vary. pg_basebackup creates a physical copy of the source cluster, but needs hardware and version compatibility, while import offers a bit more flexibility and allows major version upgrades. With either approach, CNPG handles the orchestration, making it easier to move existing workloads into a resilient, cloud-native PostgreSQL environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2025
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram