cloudnative-pg icon indicating copy to clipboard operation
cloudnative-pg copied to clipboard

[Feature]: Allow using existing pgdata when found and skip initdb

Open Crow-Control opened this issue 2 years ago • 13 comments

Is there an existing issue already for this feature request/idea?

  • [X] I have searched for an existing issue, and could not find anything. I believe this is a new feature request to be evaluated.

What problem is this feature going to solve? Why should it be added?

There are a LOT of niche usecases where people have to recreate the cluster object and just want to reuse their existing PV.

Even when recreated from scratch as a 100% duplicate of the previous cluster object, this would not work and throw the following error: PGDATA already exists

Me, and many other users, are slowly starting to get more annoyed by the enforcement of a database wipe when existing PGDATA is detected.

It's understandable that Cloudnative-PG views a new cluster as a new cluster, but when deployed cluster objects are literally identical, it should not act like they are not.

A redeployment of the mycluster object, pointed to PVCs of a previous instance of mycluster should at least be able to reuse that data

Describe the solution you'd like

Easiest solution, is to allow overriding the PGDATA check and accept to use existing PGDATA. It should be optional and disabled by default, documented as an advanced option etc.

It should not be the default, but it should be an available option for niche usecases.

Proposed key: initdb.useExisting: true

Default set to: false

Describe alternatives you've considered

For all the many cases running into this, there isn't really a clean different solution. As all of those cases are slightly different

Additional context

No response

Backport?

Yes

Are you willing to actively contribute to this feature?

Yes

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Crow-Control avatar Apr 01 '24 22:04 Crow-Control

This issue has been very difficult to resolve when trying to work with cnpg cluster during development, such as when doing local development with minikube where I may start and stop teardown/destroy the cluster including cnpg cluster a few times a day. The only reliable solution I've found so far to get past "PGDATA already exists" is to stop everything including minikube and delete local data/volumes. It'd be great to have this feature. Thanks!

hestonravenhood avatar Apr 04 '24 12:04 hestonravenhood

This issue has been very difficult to resolve when trying to work with cnpg cluster during development, such as when doing local development with minikube where I may start and stop services including cnpg cluster a few times an hour. The only reliable solution I've found so far to get past "PGDATA already exists" is to stop everything including minikube and delete local data/volumes. It'd be great to have this feature. Thanks!

start en stop isn't the issue, the problem is recreating the cluster object. If you need to stop something, you should be using the hibernation system, this stops all pods.

THe issue here, is only a thing when recreating the cluster object.

Crow-Control avatar Apr 06 '24 11:04 Crow-Control

This issue has been very difficult to resolve when trying to work with cnpg cluster during development, such as when doing local development with minikube where I may start and stop services including cnpg cluster a few times an hour. The only reliable solution I've found so far to get past "PGDATA already exists" is to stop everything including minikube and delete local data/volumes. It'd be great to have this feature. Thanks!

start en stop isn't the issue, the problem is recreating the cluster object. If you need to stop something, you should be using the hibernation system, this stops all pods.

THe issue here, is only a thing when recreating the cluster object.

Good point, I should have written that it was when doing teardown/recreating the cluster (I wasn't really doing much start/stop at that point).

I was also running two cnpg clusters on my single minikube node, so perhaps they didn't like having to share the same node. Once I progressed with testing locally and moved to a remote cluster instead of minikube, I came across this section in docs about scheduling and pod affinity, then node affinity, and found that by editing my PVs for both clusters to have affinity towards one node each, it seemed to further reduce occurrence of PGData already exists, and of multi-attach error.

hestonravenhood avatar Apr 07 '24 21:04 hestonravenhood

do we have any progress status for this issue regarding the reusing of PGDATA when redeploying ?

josephlim75 avatar Jul 21 '24 20:07 josephlim75

I have the same use case as my clusters get deleted regularly and without enough notice to peform a a snapshot or other type of backup. Having the ability to simply continue using an existing pgdata directory on new cluster creation would be ideal.

I am also happy to contribute this as the code doesn't seem too complicated and with the proposed solution by @PrivatePuffin we would be maintaining backwards compatibility for everyone else.

atanaspam avatar Mar 27 '25 13:03 atanaspam

I have the same use case as my clusters get deleted regularly and without enough notice to peform a a snapshot or other type of backup. Having the ability to simply continue using an existing pgdata directory on new cluster creation would be ideal.

I am also happy to contribute this as the code doesn't seem too complicated and with the proposed solution by @PrivatePuffin we would be maintaining backwards compatibility for everyone else.

If you could that would be great and would, at least, not negatively affect any other users not wanting this feature :)

Crow-Control avatar Mar 27 '25 14:03 Crow-Control

Also running into this issue - would be great to have the ability to restore after a totally new cluster!

boyswan avatar Apr 01 '25 12:04 boyswan

This is critical for my usecase, as cluster resources are all "throwaway" in my setup except for this very specific Cluster CRD from CNPG.

Due to a routine procedure that recreated the resource I got into a bit of a mess. My storageclass is set to Retain PVs on delete to avoid this kind of behavior. The logic that came out of the box of renaming the pgdata folder was very interesting, and useful to recover, but I would've preferred to not have this ordeal happen 😅 .

I ended up starting a temporary postgres with docker using the renamed pgdata, dumping from it and importing it into a modified Cluster that had a bootstrap section with the import section removed. Doing this with GitOps was also mostly a pain / near impossible, so I did it manually*. I also saw some people simply using cp to replace all the pgdata files with the hibernated db, maybe that would've been easier.

I guess there could be a lot of complexity depending on how the cnpg-managed secret resource with the user passwords is generated, potentially mismatching what is on the secret? (this may not be a problem or already solved)

Sign me up for an automatic pgdata reuse feature 💯 it's fine if it's behind a config.

** realizing now that the import section should probably be removed after the successful import, it serves no purpose for later and here it was very annoying.

p4block avatar May 20 '25 21:05 p4block

It may or may not have happened again, so this time I employed the procedure I saw elsewhere

  • copy/have working db elsewhere else than the pv (move the renamed pgdata, zfs snapshot or other mechanism)
  • delete content of PV
  • apply Cluster with no initdb import (or restart cluster, many things work, just start fresh)
  • hibernate freshly created cluster
  • replace pgdata with backup
  • de-hibernate cluster

Cluster works and the new generated secret still provides valid credentials for the postgres with the old pgdata, I guess there's pw replacement mechanism on startup

p4block avatar May 21 '25 22:05 p4block

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] avatar Jul 21 '25 02:07 github-actions[bot]

I wrote a workaround there: https://github.com/cloudnative-pg/cloudnative-pg/issues/5778#issuecomment-3136138144

titilambert avatar Jul 30 '25 12:07 titilambert

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] avatar Oct 04 '25 01:10 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Oct 22 '25 02:10 github-actions[bot]