@@ -315,9 +315,28 @@ func startupCommand(
315315 parameters * ParameterSet ,
316316) []string {
317317 version := fmt .Sprint (cluster .Spec .PostgresVersion )
318- logDir := parameters . Value ( "log_directory" )
318+ dataDir := DataDirectory ( cluster )
319319 walDir := WALDirectory (cluster , instance )
320320
321+ // TODO: describe this?
322+ mkdirs := make ([]string , 0 , 6 )
323+ mkdir := func (b , p string ) {
324+ if path .IsAbs (p ) {
325+ p = path .Clean (p )
326+ } else {
327+ p = path .Join (dataDir , p )
328+ }
329+
330+ // Create directories unless they are in the empty Postgres data directory.
331+ mkdirs = append (mkdirs ,
332+ `[[ ` + shell .QuoteWord (p )+ ` != "${postgres_data_directory}"* || -f "${postgres_data_directory}/PG_VERSION" ]] &&` ,
333+ `{ (` + shell .MakeDirectories (b , p )+ `) || halt "$(permissions ` + shell .QuoteWord (p )+ ` ||:)"; }` ,
334+ )
335+ }
336+ mkdir (dataMountPath , naming .PGBackRestPGDataLogPath )
337+ mkdir (dataMountPath , naming .PatroniPGDataLogPath )
338+ mkdir (dataDir , parameters .Value ("log_directory" ))
339+
321340 // If the user requests tablespaces, we want to make sure the directories exist with the
322341 // correct owner and permissions.
323342 tablespaceCmd := ""
@@ -445,14 +464,7 @@ chmod +x /tmp/pg_rewind_tde.sh
445464 `else (halt Permissions!); fi ||` ,
446465 `halt "$(permissions "${postgres_data_directory}" ||:)"` ,
447466
448- // Create log directories.
449- `(` + shell .MakeDirectories (dataMountPath , naming .PGBackRestPGDataLogPath ) + `) ||` ,
450- `halt "$(permissions ` + naming .PGBackRestPGDataLogPath + ` ||:)"` ,
451- `(` + shell .MakeDirectories (dataMountPath , naming .PatroniPGDataLogPath ) + `) ||` ,
452- `halt "$(permissions ` + naming .PatroniPGDataLogPath + ` ||:)"` ,
453- `(` + shell .MakeDirectories (DataDirectory (cluster ), logDir ) + `) ||` ,
454- // FIXME: This error prints the wrong directory when logDir is relative (the default).
455- `halt "$(permissions ` + logDir + ` ||:)"` ,
467+ strings .Join (mkdirs , "\n " ),
456468
457469 // Copy replication client certificate files
458470 // from the /pgconf/tls/replication directory to the /tmp/replication directory in order
0 commit comments