Migrate from Crunchy to Percona Operator for PostgreSQL using a standby cluster¶
A standby cluster is a PostgreSQL cluster that doesn’t have a writable primary. Instead, it applies changes from backups or from streaming replication from another database that acts as the source.
Using a standby cluster when migrating your PostgreSQL workloads, you maintain a live, synchronized copy of your original Crunchy cluster in Percona Operator for PostgreSQL. This approach minimizes downtime and gives you a way to validate the new cluster before switching over.
In this migration guide, we deploy Percona Operator for PostgreSQL as a standby cluster for a Crunchy PostgreSQL cluster by using a combination of backup and streaming replication. This approach ensures a fast initial data sync and keeps your data up to date with minimal downtime during cutover. For more information about standby cluster types, see standby cluster types.
Then we cut over the Crunchy PostgreSQL cluster and promote Percona Operator for PostgreSQL.
This migration provides minimal downtime that happens only during the cutover.
Versions used in this guide¶
This guide is tested with the following combinations of Crunchy PostgreSQL Operator and Percona Operator for PostgreSQL versions:
| Crunchy PostgreSQL Operator | Percona Operator for PostgreSQL | PostgreSQL | Standby cluster | Backup and restore | Reusing data volumes |
|---|---|---|---|---|---|
| 5.8.x | 3.0.0 | 18 | |||
| 6.0.x | 3.1.0 | 18 |
Other combinations aren’t tested.
The PostgresCluster examples in this guide use the postgres-operator.crunchydata.com/v1beta1 API version. Crunchy PostgreSQL Operator 6.0 also serves this version, so the examples work for both Crunchy versions. Crunchy 6.0 adds the v1 version, and kubectl returns objects in v1 by default.
For the differences between the Crunchy and Percona Custom Resources, see Crunchy and Percona Custom Resource differences.
The examples use S3-compatible storage for backups. If you use another remote storage, configure access to it for both clusters.
Before you migrate¶
Complete the Pre-migration compatibility checks. Physical methods require a matching PostgreSQL major version and CPU architecture.
Prerequisites¶
Ensure you have the following:
- A Kubernetes or OpenShift cluster with permissions to create namespaces, CRDs, Roles, and Deployments.
- Helm 3.x.
kubectloroccommand line tools- yq YAML processor.
- A remote backup storage reachable from Pods in both the Crunchy and Percona namespaces.
Before you start¶
-
Create the namespaces for Crunchy Operator for PostgreSQL and Percona Operator for PostgreSQL and export them as the environment variables. If you already run Crunchy Operator for PostgreSQL, export its namespace as the environment variable.
export CRUNCHY_NS=postgres-operator export NAMESPACE=pgo -
Clone the repositories to edit the manifests during the migration setup:
- For Crunchy PostgreSQL Operator:
git clone https://github.com/CrunchyData/postgres-operator.git- For Percona Operator for PostgreSQL:
git clone -b v3.1.0 https://github.com/percona/percona-postgresql-operator
Deploy Crunchy Operator for PostgreSQL¶
Skip this step if you already run Crunchy Operator for PostgreSQL.
If you don’t, install the Operator Deployment following the official Crunchy documentation .
Adapt the configuration for Crunchy PostgreSQL cluster¶
Whether you are deploying a new Crunchy PostgreSQL cluster or already have one running, make sure that:
pgBackRestis configured to send backups to a remote storage that will be accessible by the Percona standby cluster.- The Percona cluster can reach the Crunchy primary Service for streaming replication. If both clusters run in the same Kubernetes cluster, the
<clusterName>-haService is reachable from other namespaces and needs no changes. If they run in different Kubernetes clusters, expose the Service outside the cluster, for example withspec.service.type: LoadBalancerin thePostgresClusterCustom Resource.
To meet these requirements, do the following:
-
Create a Secret containing the access credentials for your backup storage. This example configuration uses AWS S3 as the backup storage and the
repo1as thepgBackRestrepository for storing backups there:storage-access.yamlapiVersion: v1 kind: Secret metadata: name: crunchy-pgbackrest-secret stringData: s3.conf: | [global] repo1-s3-key=<your-access-key> repo1-s3-key-secret=<your-access-secret>For more configuration examples for other storages, see Configure backup storage
-
Apply the file to create the Secret:
kubectl apply -f storage-access.yaml -n $CRUNCHY_NS -
Edit the
PostgresClusterCustom Resource configuration to include the following configuration:For
pgBackRestconfiguration:- The name of the Secret
- The name of the bucket
- The endpoint on the storage
- The shared path for both Crunchy and Percona standby clusters
Here’s the example configuration for Crunchy PostgreSQL cluster showing these important settings:
postgres.yamlapiVersion: postgres-operator.crunchydata.com/v1beta1 kind: PostgresCluster metadata: name: crunchy-source spec: postgresVersion: 18 instances: - name: instance1 replicas: 3 dataVolumeClaimSpec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi backups: pgbackrest: configuration: - secret: name: crunchy-pgbackrest-secret manual: repoName: repo1 options: - --type=full global: repo1-path: /crunchy-to-percona/repo1 repo1-s3-uri-style: path repo1-s3-verify-tls: "n" # Skip if you use AWS S3 repos: - name: repo1 s3: bucket: <YOUR_BUCKET_HERE> endpoint: s3.amazonaws.com region: <YOUR_REGION_HERE> proxy: pgBouncer: replicas: 1 -
Apply the configuration to create the new or update the existing Crunchy PostgreSQL cluster:
kubectl apply -f postgres.yaml -n $CRUNCHY_NS -
Wait until all PostgreSQL instances of the cluster are ready. Set the number to the
replicasvalue of theinstance1instance set (3in this example):kubectl wait postgrescluster.postgres-operator.crunchydata.com/crunchy-source \ -n "${CRUNCHY_NS}" \ --for=jsonpath='{.status.instances[?(@.name=="instance1")].readyReplicas}'=3 \ --timeout=600sExpected output
postgrescluster.postgres-operator.crunchydata.com/crunchy-source condition metThis command waits on the cluster status, so you can run it right after
kubectl apply. Akubectl wait podcommand fails withno matching resources foundif the Pods don’t exist yet.
Make a full backup on the Crunchy PostgreSQL cluster before the migration¶
Make a full backup. This ensures the standby cluster has a recent restore point and needs to replay a small amount of WAL files to synchronize.
-
Start a full backup of your Crunchy PostgreSQL cluster. The annotation value identifies this backup request, so save it in a variable:
BACKUP_ID=$(date +%s) kubectl annotate postgrescluster.postgres-operator.crunchydata.com crunchy-source \ --namespace "${CRUNCHY_NS}" \ --overwrite \ postgres-operator.crunchydata.com/pgbackrest-backup="${BACKUP_ID}"The
--overwriteflag lets you re-run the command if the cluster already has this annotation from an earlier backup. -
Wait until the Crunchy Operator picks up this backup request:
kubectl wait postgrescluster.postgres-operator.crunchydata.com/crunchy-source \ -n "${CRUNCHY_NS}" \ --for=jsonpath='{.status.pgbackrest.manualBackup.id}'="${BACKUP_ID}" \ --timeout=120s -
Wait for the backup to finish:
kubectl wait postgrescluster.postgres-operator.crunchydata.com/crunchy-source \ -n "${CRUNCHY_NS}" \ --for=jsonpath='{.status.pgbackrest.manualBackup.finished}'=true \ --timeout=600sOn a newly created Crunchy cluster, the Crunchy Operator first completes its automatic initial backup and starts your backup only after that. The timeout covers both backups. For a large cluster, increase it.
Expected output
postgrescluster.postgres-operator.crunchydata.com/crunchy-source condition met -
Check that the backup succeeded:
kubectl get postgrescluster.postgres-operator.crunchydata.com crunchy-source \ -n "${CRUNCHY_NS}" \ -o jsonpath='{.status.pgbackrest.manualBackup.succeeded}{"\n"}'The output is
1for a successful backup.If the backup fails, the Crunchy Operator retries it. During the retries,
finishedstaysfalseand the previous wait times out, whilestatus.pgbackrest.manualBackup.failedcounts the failed attempts. Check it with:kubectl get postgrescluster.postgres-operator.crunchydata.com crunchy-source \ -n "${CRUNCHY_NS}" \ -o jsonpath='{.status.pgbackrest.manualBackup}{"\n"}'To find the backup Job and check its logs, run:
kubectl get job -n "${CRUNCHY_NS}" \ -l postgres-operator.crunchydata.com/cluster=crunchy-source,postgres-operator.crunchydata.com/pgbackrest-backup=manualAfter you fix the cause, start the backup again with a new
BACKUP_IDvalue.
Export data for the standby cluster¶
If the Percona cluster runs in a different namespace than Crunchy, copy the Crunchy TLS secrets into the Percona namespace for mutual TLS on the replication connection:
-
Retrieve the Secrets:
kubectl get secrets -n $CRUNCHY_NSExpected output
crunchy-pgbackrest-secret Opaque 1 7h29m crunchy-source-cluster-cert Opaque 3 7h29m crunchy-source-instance1-khbq-certs Opaque 6 7h29m crunchy-source-instance1-ndrp-certs Opaque 6 7h29m crunchy-source-instance1-xlh6-certs Opaque 6 7h29m crunchy-source-pgbackrest Opaque 3 7h29m crunchy-source-pgbouncer Opaque 6 7h29m crunchy-source-pguser-crunchy-source Opaque 12 7h29m crunchy-source-replication-cert Opaque 3 7h29m pgo-root-cacert Opaque 2 7h29mThe Secrets we are interested in are
crunchy-source-cluster-certandcrunchy-source-replication-cert -
Export Secrets to a file:
kubectl get secret crunchy-source-cluster-cert -o json -n "$CRUNCHY_NS" \ | yq -o yaml ' { "apiVersion": .apiVersion, "kind": .kind, "data": .data, "metadata": {"name": .metadata.name}, "type": .type } ' > ~/crunchy-source-cluster-cert.yaml kubectl get secret crunchy-source-replication-cert -o json -n "$CRUNCHY_NS" \ | yq -o yaml ' { "apiVersion": .apiVersion, "kind": .kind, "data": .data, "metadata": {"name": .metadata.name}, "type": .type } ' > ~/crunchy-source-replication-cert.yaml
Deploy Percona Operator for PostgreSQL and PostgreSQL cluster in standby mode¶
-
Deploy Percona Operator for PostgreSQL:
kubectl apply --server-side -f https://raw.githubusercontent.com/percona/percona-postgresql-operator/v3.1.0/deploy/bundle.yaml -n ${ -
Import the TLS Secrets using the configuration files you created earlier:
kubectl apply -f ~/crunchy-source-cluster-cert.yaml -n ${ kubectl apply -f ~/crunchy-source-replication-cert.yaml -n $NAMESPACE -
Create the shared Secret for the backup storage. You can reuse the same Secret configuration file from the Adapt the configuration for Crunchy PostgreSQL cluster step:
echo "apiVersion: v1 kind: Secret metadata: name: percona-pgbackrest-secret stringData: s3.conf: | [global] repo1-s3-key=<your-access-key> repo1-s3-key-secret=<your-access-secret> " | kubectl apply -n $NAMESPACE -f - -
Edit the
deploy/cr.yamland specify the following configuration:-
For the standby cluster:
- Set
standby.enabledtotrue. - Specify the repository name to restore from in the
standby.repoNameoption. - Specify the name of the service of the Crunchy PostgreSQL to stream data from. The Service name follows the pattern
<service-name>.<namespace>.svc.cluster.local. - Specify the port of this service.
Replace example names such as
crunchy-source-ha,crunchy-source-pgbackrest, andcluster1-pgbouncerif you use different cluster names. The service namecrunchy-source-hain the following example is derived from the sourcePostgresClusternamecrunchy-source. Replace<CRUNCHY_NS>with the namespace of the Crunchy cluster.spec: standby: enabled: true host: crunchy-source-ha.<CRUNCHY_NS>.svc.cluster.local port: 5432 repoName: repo1 - Set
-
For the mutual TLS communication, reference the TLS Secrets you created for the
secrets.customTLSSecretandsecrets.customReplicationTLSSecretoptionssecrets: customTLSSecret: name: crunchy-source-cluster-cert customReplicationTLSSecret: name: crunchy-source-replication-certpgBouncer doesn’t work while the cluster uses the Crunchy certificates
The Crunchy server certificate is issued only for the
crunchy-source-primaryandcrunchy-source-replicashost names. With this certificate, Percona pgBouncer servicecluster1-pgbouncercan’t connect to PostgreSQL, and clients that connect tocluster1-primarywithsslmode=verify-fullfail on the host name check. The Operator also doesn’t renew these certificates. They expire one year after Crunchy issued them.Keep these certificates only for the migration. After the cutover, switch the cluster to Operator-managed certificates.
-
Configure the backup storage to exactly match the configuration on the Crunchy PostgreSQL cluster:
backups: pgbackrest: .... configuration: - secret: name: percona-pgbackrest-secret global: # Must match repo1-path in the Crunchy source cluster exactly. repo1-path: /crunchy-to-percona/repo1 repo1-s3-uri-style: path repo1-s3-verify-tls: "n" # Skip for AWS S3 repos: - name: repo1 s3: bucket: <YOUR-BUCKET-HERE> endpoint: s3.amazonaws.com region: <YOUR-REGION-HERE> #The rest of your configuration- If the Crunchy cluster uses tablespace volumes (
spec.instances[].tablespaceVolumes), add atablespaceVolumesentry with the same name for each tablespace to the Percona instance, for exampletablespaceVolumes: [{name: ts1, dataVolumeClaimSpec: {accessModes: [ReadWriteOnce], resources: {requests: {storage: 5Gi}}}}]. Without it, the standby can’t create the tablespace directories and doesn’t start.
-
-
Create the Percona PostgreSQL cluster
kubectl apply -f deploy/cr.yaml -n $NAMESPACEThe Percona Operator deploys the cluster and:
- Restores the base backup from the S3 bucket.
- Replays WAL from S3 until it catches up with the live Crunchy cluster.
- Switches to streaming replication from
crunchy-source-ha.
-
Wait for the cluster to report the
Readystatus:kubectl wait perconapgcluster/cluster1 \ -n $NAMESPACE \ --for=jsonpath='{.status.state}'=ready \ --timeout=600sExpected output
perconapgcluster.pgv2.percona.com/cluster1 condition met -
Verify the data is replicated on the standby.
- Export the standby leader Pod as an environment variable. The standby leader replicates from Crunchy, and the other Pods replicate from it. The Operator labels the standby leader with
role=primary:
STANDBY_POD=$(kubectl get pod -n $NAMESPACE \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/role=primary \ -o jsonpath='{.items[0].metadata.name}')- Connect to the pod and verify the cluster is replicating:
kubectl -n "${NAMESPACE}" exec "${STANDBY_POD}" -c database -- \ psql -t -c "SELECT pg_is_in_recovery(), pg_last_wal_replay_lsn();"You should see
tforpg_is_in_recovery()and a non-null LSN.Sample output
t | 0/14000000 - Export the standby leader Pod as an environment variable. The standby leader replicates from Crunchy, and the other Pods replicate from it. The Operator labels the standby leader with
Verify streaming replication lag before cutover¶
On the Crunchy primary Pod, confirm the Percona standby is connected and lag is acceptable:
-
Identify the primary:
CRUNCHY_PRIMARY=$(kubectl get pod \ -l postgres-operator.crunchydata.com/cluster=crunchy-source,postgres-operator.crunchydata.com/role=master \ -n "${CRUNCHY_NS}" \ -o jsonpath='{.items[0].metadata.name}') -
Exec to the primary and verify the replication lag:
kubectl -n "${CRUNCHY_NS}" exec "${CRUNCHY_PRIMARY}" -c database -- \ psql -c " SELECT application_name, client_addr, state, pg_wal_lsn_diff(sent_lsn, replay_lsn) AS byte_lag, write_lag, flush_lag, replay_lag FROM pg_stat_replication; "The output also lists the Crunchy replicas. The Percona standby leader is the row whose
application_namestarts withcluster1-instance1.Sample output
application_name | client_addr | state | byte_lag | write_lag | flush_lag | replay_lag ----------------------------+-------------+-----------+----------+-----------------+-----------------+----------------- crunchy-source-instance1-n642-0 | 10.76.0.6 | streaming | 0 | 00:00:00.004238 | 00:00:00.00733 | 00:00:00.009044 crunchy-source-instance1-shr4-0 | 10.76.2.9 | streaming | 0 | 00:00:00.011289 | 00:00:00.013495 | 00:00:00.014209 cluster1-instance1-lfnc-0 | 10.76.2.11 | streaming | 0 | 00:00:00.001273 | 00:00:00.002045 | 00:00:00.002287 (3 rows)
Proceed only when write_lag and replay_lag are null or within your policy (for example a few seconds).
Cut over the Crunchy PostgreSQL cluster¶
Now you are ready for the cutover. Note that this step causes downtime as both clusters will be in standby mode and will not accept writes.
-
Patch the Crunchy PostgreSQL cluster to the standby mode. Patroni shuts down the primary cleanly, which archives its last WAL segment to the shared repository. The
repoNameoption makes Crunchy a standby that follows this repository, which is what Rollback relies on:kubectl patch postgrescluster crunchy-source \ -n $CRUNCHY_NS \ --type=merge \ -p '{"spec": {"standby": {"enabled": true, "repoName": "repo1"}}}' -
Wait until the former primary reports recovery. Right after the patch, the check can still return
for a connection error for a few seconds while Patroni restarts PostgreSQL:until [ "$(kubectl -n "${CRUNCHY_NS}" exec "${CRUNCHY_PRIMARY}" -c database -- \ psql -tAc 'SELECT pg_is_in_recovery();' 2>/dev/null)" = "t" ]; do echo "Waiting for ${CRUNCHY_PRIMARY} to enter recovery..." sleep 2 done echo "${CRUNCHY_PRIMARY} is in recovery"
(Optional) Shut down the Crunchy cluster¶
After the Percona standby has replayed WAL, shut down Crunchy to avoid split-brain:
kubectl patch postgrescluster crunchy-source \
-n $CRUNCHY_NS \
--type=merge \
-p '{"spec": {"shutdown": true}}'
Promote Percona PostgreSQL cluster¶
Before promoting the Percona PostgreSQL cluster, make sure it has finished replaying all WAL (Write-Ahead Log) data.
After the Crunchy cluster is shut down, no new WAL files will be generated. The Percona standby will keep replaying existing WAL files from S3 object storage, and its Log Sequence Number (LSN) will advance each time new WAL is applied.
Once the LSN value stops changing and stays the same across two consecutive checks, it means all available WAL has been replayed and it’s safe to promote the standby cluster to be the new primary.
-
Verify the Percona standby cluster has stopped replaying WAL files. The Operator can restart the standby Pods after you created the cluster, so look up the standby leader again first:
STANDBY_POD=$(kubectl get pod -n $NAMESPACE \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/role=primary \ -o jsonpath='{.items[0].metadata.name}')Then check the replay position:
kubectl -n $NAMESPACE exec "${STANDBY_POD}" -c database -- \ psql -t -c "SELECT pg_last_wal_replay_lsn();"Repeat this command several times until the LSN value stops advancing.
-
Promote the Percona PostgreSQL cluster to be the new primary by patching the cluster:
kubectl patch perconapgcluster cluster1 \ -n $NAMESPACE \ --type=merge \ -p '{"spec": {"standby": {"enabled": false}}}' -
Wait until the cluster leaves recovery mode and accepts writes. Don’t rely on the cluster
readystatus for this: it staysreadyduring the promotion, sokubectl waitfor this status returns immediately.- Identify the primary Pod:
PERCONA_PRIMARY=$(kubectl get pod -n $NAMESPACE \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/role=primary \ -o jsonpath='{.items[0].metadata.name}')- Check the recovery mode until the Pod reports
f, which means the instance is read-write:
until [ "$(kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -tAc 'SELECT pg_is_in_recovery();' 2>/dev/null)" = "f" ]; do echo "Waiting for ${PERCONA_PRIMARY} to accept writes..." sleep 2 done echo "${PERCONA_PRIMARY} accepts writes"The promotion usually takes a few seconds.
-
After promoting the Percona cluster, it is important to confirm that the
pgBackReststanza has been created. The stanza is required for managing backups and enabling restores. Without it, regular backup operations and recovery from backup will not be possible. Waiting forstanzaCreatedto becometrueensures that backup tooling has been initialized properly before proceeding.kubectl wait perconapgcluster/cluster1 \ -n $NAMESPACE \ --for=jsonpath='{.status.pgbackrest.repos[0].stanzaCreated}'=true \ --timeout=300sExpected output
perconapgcluster.pgv2.percona.com/cluster1 condition met
Take a post-migration backup¶
After the promotion, cluster1 still archives WAL to the repository path it shares with Crunchy, and the Operator has already taken an incremental backup there on top of Crunchy’s full backup. Move cluster1 to its own repository path and take a full backup there. This gives the Percona cluster backups that don’t depend on the Crunchy archive.
This step ends the rollback window
After you change the repository path, Crunchy no longer receives the WAL from cluster1. If you roll back after this point, Crunchy doesn’t get the writes made on cluster1. Do this step only when you no longer need Rollback. Until then, cluster1 can keep using the shared path.
Do this step before you point applications to cluster1. The WAL that cluster1 generates between the path change and the stanza creation isn’t stored in either repository.
-
Change the repository path. Use a path that no other cluster uses:
kubectl patch perconapgcluster cluster1 \ -n $NAMESPACE \ --type=merge \ -p '{"spec":{"backups":{"pgbackrest":{"global":{"repo1-path":"/percona/cluster1/repo1"}}}}}' -
Wait until the
pgBackRestconfiguration on the primary Pod shows the new path:until kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ grep -qE 'repo1-path *= */percona/cluster1/repo1$' /etc/pgbackrest/conf.d/pgbackrest_instance.conf 2>/dev/null; do echo "Waiting for the new repository path..." sleep 5 done -
Create the
pgBackReststanza in the new path. The Operator doesn’t do it when only the path changes:kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ pgbackrest --stanza=db stanza-createUntil the stanza exists, WAL archiving to the new path fails. In Operator versions 3.0.0 and 3.1.0,
pg_stat_archiverdoesn’t show these failures, so don’t use it to check archiving. -
Wait until the repository host Pod also has the new path. Backups run from this Pod, and it gets the new configuration up to a minute later than the primary:
until kubectl -n $NAMESPACE exec cluster1-repo-host-0 -c pgbackrest -- \ grep -qE 'repo1-path *= */percona/cluster1/repo1$' /etc/pgbackrest/conf.d/pgbackrest_repo.conf 2>/dev/null; do echo "Waiting for the repository host..." sleep 5 done
Then take a full backup in the new path:
-
Create the
post-migration-backup.yamlconfiguration file. Set--type=fullinoptions: without it,pgBackRestmakes an incremental backup on top of the last full backup in the repository.post-migration-backup.yamlapiVersion: pgv2.percona.com/v2 kind: PerconaPGBackup metadata: name: post-migration-backup spec: pgCluster: cluster1 repoName: repo1 options: - --type=full -
Apply the configuration to start the backup:
kubectl apply -f post-migration-backup.yaml -n $NAMESPACE -
Wait for the backup job to complete:
kubectl wait perconapgbackup/post-migration-backup \ -n $NAMESPACE \ --for=jsonpath='{.status.state}'=Succeeded \ --timeout=600sExpected output
perconapgbackup.pgv2.percona.com/post-migration-backup condition met -
Check that the backup is a full backup:
kubectl get perconapgbackup post-migration-backup \ -n $NAMESPACE \ -o jsonpath='{.status.backupType}{"\n"}'The output is
full.
Switch to Operator-managed TLS certificates¶
The cluster still uses the TLS certificates imported from Crunchy. They don’t cover the cluster1 host names, so cluster1-pgbouncer can’t connect to PostgreSQL, and the Operator doesn’t renew them. Replace them with certificates that the Operator issues and renews.
Like the repository path change, this step ends the rollback window: replication between the clusters relies on the Crunchy certificates.
-
Remove the references to the Crunchy certificates from the cluster configuration:
kubectl patch perconapgcluster cluster1 \ -n $NAMESPACE \ --type=json \ -p='[ {"op":"remove","path":"/spec/secrets/customTLSSecret"}, {"op":"remove","path":"/spec/secrets/customReplicationTLSSecret"} ]'The Operator creates the
cluster1-cluster-certandcluster1-replication-certSecrets, restarts the pgBouncer Pods and then restarts the PostgreSQL Pods one by one. -
Watch the PostgreSQL Pods:
kubectl get pod -n $NAMESPACE -w \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/data=postgresThe restart can stop after the first replica: the restarted replica trusts only the new certificate authority, so it can’t connect to the primary that still uses the Crunchy certificate. The replica stays not ready, and its
databasecontainer log showscertificate verify failed. If this happens, finish the switch manually:-
Delete the other replica Pod that still runs with the old certificates, so that it restarts with the new ones:
kubectl delete pod <old-replica-pod> -n $NAMESPACE -
The replicas with the new certificates can’t stream from the primary, so they replay WAL only from the backup repository and lag behind. Patroni refuses a switchover to a lagging replica. Make the primary archive its current WAL file, then check that the lag of the replica you switch to is
0:kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -c "SELECT pg_switch_wal();" kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ patronictl listRepeat
patronictl listuntil the replay lag of the replica (the lastLagcolumn) shows0. The firstLagcolumn can keep growing: the replica doesn’t stream from the primary, so its received position doesn’t move. -
Make the replica that runs with the new certificates the primary. Writes stop for about 10 seconds during the switchover:
kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ patronictl switchover --candidate <new-replica-pod> --forceCheck the output for
Successfully switched over. The command exits with code0even when the switchover fails, for example withSwitchover failed, details: 503. In this case, repeat the previous step and try again.The Operator then restarts the former primary with the new certificates.
-
-
After all PostgreSQL Pods are restarted and ready, check that the cluster reports the
readystate:kubectl get perconapgcluster cluster1 -n $NAMESPACE -
Check that clients can connect with full certificate verification. Save the certificate authority of the cluster to a file:
kubectl get secret cluster1-cluster-cert -n $NAMESPACE \ -o jsonpath='{.data.ca\.crt}' | base64 --decode > cluster1-ca.crtUse this file as
sslrootcertwithsslmode=verify-fullwhen you connect tocluster1-primaryorcluster1-pgbouncer. Applications that trusted the Crunchy certificate authority must use this one instead.
Update the application connection string¶
After you migrated to Percona Operator for PostgreSQL, update your application connection string to use the new Percona cluster. If you must send traffic to cluster1 before you switch the TLS certificates, use the cluster1-primary Service with sslmode=require instead of pgBouncer.
-
Get the
pgBouncerservice exposed by the Percona Operator:kubectl get service -n $NAMESPACE cluster1-pgbouncer
The output will show the cluster1-pgbouncer service, along with its
ClusterIP and port.
-
Update your application’s connection string to use the
pgBouncerservice name (from the previous step) as the PostgreSQL host. For example:Host: cluster1-pgbouncer.$NAMESPACE.svc.cluster.local Port: 5432
Replace ${NAMESPACE} with your cluster’s namespace. Update your application configuration to point to this service, ensuring it now connects to your Percona Operator for PostgreSQL deployment.
Rollback¶
The standby path is the most rollback-friendly of the migration options: Crunchy and Percona share the same pgBackRest repository until you move cluster1 to its own repository path.
Before you move cluster1 to its own repository¶
You can return write traffic to Crunchy while cluster1 still archives WAL to the shared repository and uses the Crunchy certificates. In other words, before you complete Take a post-migration backup and Switch to Operator-managed TLS certificates. The Crunchy PostgresCluster must still exist.
What happens to the writes made on cluster1 after the promotion:
- If the WAL of
cluster1reaches the shared repository before Crunchy is promoted, Crunchy replays it, and these writes are on Crunchy after the rollback. This is the normal case when you follow the steps below and WAL archiving works. Don’t copy these writes to Crunchy manually: you would duplicate them. - If some of this WAL is missing from the repository, for example, because archiving failed or
cluster1stopped without a clean shutdown, the writes in it are lost. Crunchy can then reuse sequence values thatcluster1already used, andcluster1ends up on a timeline that diverged from Crunchy. In this case, deletecluster1and create it again as a standby.
To roll back, do the following:
-
Stop application writes to
cluster1. -
Record the position of the last write on
cluster1and the WAL file that contains it, and makecluster1archive this file. You need these values to check that Crunchy gets every write. Run this and the following steps in the same terminal session, so that theFINAL_LSNandFINAL_WALvariables are kept, or note the values from the output:PERCONA_PRIMARY=$(kubectl get pod -n "${NAMESPACE}" \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/role=primary \ -o jsonpath='{.items[0].metadata.name}') FINAL_LSN=$(kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -tAc "SELECT pg_current_wal_lsn();") FINAL_WAL=$(kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -tAc "SELECT pg_walfile_name(pg_current_wal_lsn());") kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -tAc "SELECT pg_switch_wal();" >/dev/null echo "Final LSN: ${FINAL_LSN}, WAL file: ${FINAL_WAL}" -
Put
cluster1back into standby so it stops acting as the primary (Patroni will follow the Custom Resource). Thestandby.host,standby.port, andstandby.repoNameyou configured earlier should still be present; merge only toggles standby on:kubectl patch perconapgcluster cluster1 \ -n $NAMESPACE \ --type=merge \ -p '{"spec": {"standby": {"enabled": true, "repoName": "repo1"}}}' -
Wait until every
cluster1Pod is in recovery. A single check right after the patch isn’t enough: the primary keeps returningf, and then a connection error, for a few seconds while Patroni demotes it.for pod in $(kubectl get pod -n "${NAMESPACE}" -o name \ -l postgres-operator.crunchydata.com/cluster=cluster1,postgres-operator.crunchydata.com/data=postgres); do until [ "$(kubectl -n "${NAMESPACE}" exec "${pod}" -c database -- \ psql -tAc 'SELECT pg_is_in_recovery();' 2>/dev/null)" = "t" ]; do echo "Waiting for ${pod} to enter recovery..." sleep 2 done echo "${pod} is in recovery" doneDon’t promote Crunchy while cluster1 is still a primary
If you take Crunchy out of standby while
cluster1still accepts writes, both clusters accept writes and archive WAL to the samepgBackReststanza on different timelines. Neither Operator reports an error. Whencluster1later follows Crunchy again,pg_rewinddiscards the writes made oncluster1in the meantime. -
Check that the WAL file with the last write is in the shared repository. The following command tries to fetch the file from the repository for about 6 minutes:
ARCHIVED=no for i in $(seq 1 60); do if kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ pgbackrest --stanza=db archive-get "${FINAL_WAL}" /tmp/rollback-wal-check >/dev/null 2>&1; then ARCHIVED=yes; echo "${FINAL_WAL} is in the repository"; break fi echo "Waiting for ${FINAL_WAL} to be archived..." sleep 5 done kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- rm -f /tmp/rollback-wal-check [ "${ARCHIVED}" = yes ] || echo "ERROR: ${FINAL_WAL} is not in the repository. Don't continue."Continue only if the output ends with
is in the repository. If it ends with the error, WAL archiving oncluster1failed, and the writes in this file and the files before it would be lost after the rollback. Don’t rely onpg_stat_archiverto check this: in Operator versions 3.0.0 and 3.1.0 it reports WAL files as archived even when the upload failed.Fixing the cause, for example wrong storage credentials, isn’t enough: PostgreSQL doesn’t retry WAL files that the failed archive command reported as archived. After you fix the cause, push the missing WAL files from
cluster1to the repository manually. The files are still in thepg_waldirectory of the Pod. Replacepg18with your PostgreSQL major version:kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- bash -c ' cd /pgdata/pg18/pg_wal for f in $(ls | grep -E "^'"${FINAL_WAL:0:8}"'[0-9A-F]{16}$" | sort); do [[ "$f" > "'"${FINAL_WAL}"'" ]] && break pgbackrest --stanza=db --no-archive-async archive-push "$PWD/$f" done'This command pushes every WAL file of the current timeline up to
FINAL_WAL. Pushing a file that is already in the repository with the same content succeeds, so the command is safe to run. Then repeat this step. -
If you applied (Optional) Shut down the Crunchy cluster, clear shutdown:
kubectl patch postgrescluster crunchy-source \ -n "${CRUNCHY_NS}" \ --type=merge \ -p '{"spec": {"shutdown": false}}' -
Wait until every Crunchy Pod has replayed WAL up to the last write on
cluster1. Crunchy is still a standby and replays this WAL from the shared repository. If you promote it before it reaches this position, the remaining writes are lost.If you shut down Crunchy, it starts the former primary first and the other instances later. Wait until all instances are ready. Set the number to the
replicasvalue of theinstance1instance set (3in this example):kubectl wait postgrescluster.postgres-operator.crunchydata.com/crunchy-source \ -n "${CRUNCHY_NS}" \ --for=jsonpath='{.status.instances[?(@.name=="instance1")].readyReplicas}'=3 \ --timeout=600sThen check the replay position on every Crunchy Pod:
for pod in $(kubectl get pod -n "${CRUNCHY_NS}" -o name \ -l postgres-operator.crunchydata.com/cluster=crunchy-source,postgres-operator.crunchydata.com/data=postgres); do until [ "$(kubectl -n "${CRUNCHY_NS}" exec "${pod}" -c database -- \ psql -tAc "SELECT pg_last_wal_replay_lsn() >= '${FINAL_LSN}'::pg_lsn;" 2>/dev/null)" = "t" ]; do echo "Waiting for ${pod} to replay up to ${FINAL_LSN}..." sleep 2 done echo "${pod} has replayed up to ${FINAL_LSN}" done -
Take Crunchy out of standby so Patroni can promote it back to primary:
kubectl patch postgrescluster crunchy-source \ -n "${CRUNCHY_NS}" \ --type=merge \ -p '{"spec": {"standby": {"enabled": false}}}' -
Confirm roles: Crunchy should accept writes and
cluster1should be in recovery until replication catches up. Therole=masterlabel appears on the promoted Pod only after the promotion, so wait for it:until CRUNCHY_PRIMARY=$(kubectl get pod \ -l postgres-operator.crunchydata.com/cluster=crunchy-source,postgres-operator.crunchydata.com/role=master \ -n "${CRUNCHY_NS}" \ -o jsonpath='{.items[0].metadata.name}' 2>/dev/null) && [ -n "${CRUNCHY_PRIMARY}" ]; do echo "Waiting for the Crunchy primary Pod..." sleep 2 done until [ "$(kubectl -n "${CRUNCHY_NS}" exec "${CRUNCHY_PRIMARY}" -c database -- \ psql -tAc 'SELECT pg_is_in_recovery();' 2>/dev/null)" = "f" ]; do echo "Waiting for ${CRUNCHY_PRIMARY} to accept writes..." sleep 2 done echo "${CRUNCHY_PRIMARY} accepts writes"Check that
cluster1follows Crunchy:kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \ psql -t -c "SELECT pg_is_in_recovery();"You should see
ton Percona while it follows Crunchy. -
Restore the password of the Crunchy pgBouncer user. After the promotion, the Percona Operator set a new password for the
_crunchypgbouncerrole, and the rollback carried this password back to Crunchy. Until you restore it, the Crunchy pgBouncer fails to authenticate. Set the role password back to the value from the Crunchy pgBouncer Secret:VERIFIER=$(kubectl get secret crunchy-source-pgbouncer -n "${CRUNCHY_NS}" \ -o jsonpath='{.data.pgbouncer-verifier}' | base64 --decode) kubectl -n "${CRUNCHY_NS}" exec "${CRUNCHY_PRIMARY}" -c database -- \ psql -c "ALTER ROLE \"_crunchypgbouncer\" PASSWORD '${VERIFIER}';"pgBouncer keeps rejecting connections for about 30 seconds after the change with
server login has been failing. This is pgBouncer’s retry delay. Retry the connection after that. -
Point applications back at the Crunchy
pgBouncerService:kubectl get service -n "${CRUNCHY_NS}" \ -l postgres-operator.crunchydata.com/cluster=crunchy-source,postgres-operator.crunchydata.com/role=pgbouncer
If you migrate to Percona again after a rollback
Step 10 sets the Crunchy password for _crunchypgbouncer, and cluster1 replicates it. After you promote cluster1 again, the Operator doesn’t reset this password, so cluster1-pgbouncer fails to authenticate. After the promotion, set the password from the Percona pgBouncer Secret on the cluster1 primary:
VERIFIER=$(kubectl get secret cluster1-pgbouncer -n "${NAMESPACE}" \
-o jsonpath='{.data.pgbouncer-verifier}' | base64 --decode)
kubectl -n $NAMESPACE exec "${PERCONA_PRIMARY}" -c database -- \
psql -c "ALTER ROLE \"_crunchypgbouncer\" PASSWORD '${VERIFIER}';"
After you move cluster1 to its own repository¶
After cluster1 archives WAL to its own repository path, Crunchy no longer receives its writes. You should treat Crunchy as a historical reference, not a live paired primary.
Rolling back from here is the same idea as a fresh restore: deploy or restore a Crunchy cluster from the repository state you need, or follow Migrate from Crunchy using backup and restore in reverse order for your environment. Plan the rollback before cutover if you require a guaranteed fast path.
Troubleshooting¶
Percona standby cannot resolve or reach the Crunchy primary¶
Confirm the PostgreSQL HA Service for crunchy-source resolves from the Percona database Pod. Replace crunchy-source-ha if your Service name differs.
kubectl -n "${NAMESPACE}" exec "${STANDBY_POD}" -c database -- \
bash -c "getent hosts crunchy-source-ha.${CRUNCHY_NS}.svc.cluster.local"
If DNS fails, check Service names and namespaces. If DNS succeeds but TCP fails, check NetworkPolicies, service mesh rules, and firewalls between namespaces.
Standby fails with Read-only file system for /tablespaces¶
The standby leader is in CrashLoopBackOff, and its log shows unable to create path '/tablespaces': [30] Read-only file system or pg_basebackup: error: could not create directory "/tablespaces": Read-only file system, followed by Failed to bootstrap cluster. The Crunchy cluster uses tablespace volumes, and the Percona instance has no tablespaceVolumes entry with the same name. Add the entries to the cluster configuration.
Replication authentication errors¶
The Percona standby connects to Crunchy as the PostgreSQL role _crunchyrepl using the replication TLS material. Ensure crunchy-source-replication-cert exists in $NAMESPACE and matches what Crunchy generated (re-export from "${CRUNCHY_NS}" if you rotated certificates).
kubectl get secret crunchy-source-replication-cert -n "${NAMESPACE}"
Verify the PerconaPGCluster still references it under secrets.customReplicationTLSSecret and secrets.customTLSSecret as in Deploy Percona Operator for PostgreSQL and PostgreSQL cluster in standby mode.
pgBackRest restore or WAL archive problems¶
Symptoms include failed restore Jobs, missing archive.info, or the standby never leaving base backup replay.
-
Confirm
repo1-pathis identical on Crunchy and oncluster1(for example/crunchy-to-percona/repo1in this guide). This applies until you movecluster1to its own repository path. After the move, the paths differ on purpose.kubectl get postgrescluster crunchy-source -n "${CRUNCHY_NS}" \ -o jsonpath='{.spec.backups.pgbackrest.global.repo1-path}{"\n"}' kubectl get perconapgcluster cluster1 -n $NAMESPACE \ -o jsonpath='{.spec.backups.pgbackrest.global.repo1-path}{"\n"}' -
Confirm both clusters use credentials that can read and write the same bucket (equivalent Secrets). See Configure backup storage for TLS flags such as
repo1-s3-verify-tlsand path-style settings. -
From a throwaway Pod in
$NAMESPACE, verify object storage reachability with your endpoint, bucket, and keys (adapt for your provider):kubectl run s3-check \ --image=perconalab/awscli \ --restart=Never \ -n $NAMESPACE \ -- bash -c " AWS_ACCESS_KEY_ID='<your-access-key>' \ AWS_SECRET_ACCESS_KEY='<your-secret-key>' \ AWS_DEFAULT_REGION='<your-region>' \ aws --endpoint-url 'https://<your-s3-endpoint>' \ --no-verify-ssl \ s3 ls s3://<your-bucket> "The command finishes quickly, so read its output from the Pod logs, and then delete the Pod:
kubectl wait pod/s3-check -n $NAMESPACE \ --for=jsonpath='{.status.phase}'=Succeeded --timeout=120s kubectl logs s3-check -n $NAMESPACE kubectl delete pod s3-check -n $NAMESPACE
Timeline history file missing after promotion (Crunchy archive)¶
Some Crunchy PGO configurations using async archive mode can hit a missing timeline history file on the archive after promotion (see Crunchy PGO issue #4472 ). If pgBackRest or replicas complain about *.history on the archive, push the file once from the new primary:
kubectl -n "${NAMESPACE}" exec "${PERCONA_PRIMARY}" -c database -- \
bash -c "
pgbackrest --stanza=db --no-archive-async \
archive-push \"\${PGDATA}/pg_wal/00000002.history\" || true
"
Adjust the filename to match the timeline file reported in your logs.
Created: May 22, 2026