Repository: cloudnative-pg/cloudnative-pg

Issue:
[Feature]: allow to customize the postgres executable name
### 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?

I want to be able to change the PostgreSQL executable name used by the instance manager. This is useful for testing and debugging by intercepting the PostgreSQL execution.

### Describe the solution you'd like

Following what we have done with [in-place major upgrades](https://github.com/cloudnative-pg/cloudnative-pg/commit/2e129276ada268d3bd1b5fa3f37af7152afb78ba), add an environment variable to customize the PostgreSQL executable name used by the instance manager.

### Describe alternatives you've considered

N/A

### Additional context

N/A

### Backport?

No

### Are you willing to actively contribute to this feature?

Yes

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

Discussion:

Solution patch:
diff --git a/internal/cmd/manager/instance/run/lifecycle/reaper.go b/internal/cmd/manager/instance/run/lifecycle/reaper.go
index 55d17106..7a4f47b4 100644
--- a/internal/cmd/manager/instance/run/lifecycle/reaper.go
+++ b/internal/cmd/manager/instance/run/lifecycle/reaper.go
@@ -102,7 +102,7 @@ func (z *PostgresOrphansReaper) handleSignal(contextLogger log.Logger) error {
 	pidFile := path.Join(z.instance.PgData, postgres.PostgresqlPidFile)
 	_, postMasterPid, _ := z.instance.GetPostmasterPidFromFile(pidFile)
 	for _, p := range processes {
-		if p.PPid() == 1 && p.Executable() == "postgres" {
+		if p.PPid() == 1 && p.Executable() == postgres.GetPostgresExecutableName() {
 			pid := p.Pid()
 			if pid == postMasterPid {
 				continue
diff --git a/pkg/management/postgres/instance.go b/pkg/management/postgres/instance.go
index 8ea2efc4..6aa7f4b9 100644
--- a/pkg/management/postgres/instance.go
+++ b/pkg/management/postgres/instance.go
@@ -59,7 +59,6 @@ import (
 )
 
 const (
-	postgresName      = "postgres"
 	pgCtlName         = "pg_ctl"
 	pgRewindName      = "pg_rewind"
 	pgBaseBackupName  = "pg_basebackup"
@@ -73,6 +72,15 @@ const (
 	pgPingNoAttempt  = 3 // connection not attempted (bad params)
 )
 
+// GetPostgresExecutableName returns the name of the PostgreSQL executable
+func GetPostgresExecutableName() string {
+	if name := os.Getenv("POSTGRES_NAME"); name != "" {
+		return name
+	}
+
+	return "postgres"
+}
+
 // shutdownMode represent a way to request the postmaster shutdown
 type shutdownMode string
 
@@ -670,7 +678,7 @@ func (instance *Instance) Reload(ctx context.Context) error {
 // Run this instance returning an OS process needed
 // to control the instance execution
 func (instance *Instance) Run() (*execlog.StreamingCmd, error) {
-	process, err := instance.CheckForExistingPostmaster(postgresName)
+	process, err := instance.CheckForExistingPostmaster(GetPostgresExecutableName())
 	if err != nil {
 		return nil, err
 	}
@@ -703,11 +711,11 @@ func (instance *Instance) Run() (*execlog.StreamingCmd, error) {
 		return nil, err
 	}
 
-	postgresCmd := exec.Command(postgresName, options...) // #nosec
+	postgresCmd := exec.Command(GetPostgresExecutableName(), options...) // #nosec
 	postgresCmd.Env = instance.Env
 	compatibility.AddInstanceRunCommands(postgresCmd)
 
-	streamingCmd, err := execlog.RunStreamingNoWait(postgresCmd, postgresName)
+	streamingCmd, err := execlog.RunStreamingNoWait(postgresCmd, GetPostgresExecutableName())
 	if err != nil {
 		return nil, err
 	}



Test patch:
diff --git a/pkg/management/postgres/pidfile_test.go b/pkg/management/postgres/pidfile_test.go
index 271b00b2..460a61b1 100644
--- a/pkg/management/postgres/pidfile_test.go
+++ b/pkg/management/postgres/pidfile_test.go
@@ -52,7 +52,7 @@ var _ = Describe("the detection of a postmaster process using the pid file", fun
 		instance := NewInstance()
 		instance.PgData = pgdata
 		instance.SocketDirectory = socketDir
-		process, err := instance.CheckForExistingPostmaster(postgresName)
+		process, err := instance.CheckForExistingPostmaster(GetPostgresExecutableName())
 		Expect(err).ShouldNot(HaveOccurred())
 		Expect(process).To(BeNil())
 	})
@@ -70,7 +70,7 @@ var _ = Describe("the detection of a postmaster process using the pid file", fun
 		err = os.WriteFile(filepath.Join(socketDir, ".s.PGSQL.5432.lock"), []byte("1234"), 0o400)
 		Expect(err).ShouldNot(HaveOccurred())
 
-		process, err := instance.CheckForExistingPostmaster(postgresName)
+		process, err := instance.CheckForExistingPostmaster(GetPostgresExecutableName())
 		Expect(err).ShouldNot(HaveOccurred())
 		Expect(process).To(BeNil())