@@ -142,28 +142,51 @@ EOSQL
142142 return 1
143143 fi
144144
145- # If this is a fresh initialization, restart PostgreSQL to apply archive settings
146- if [ ! -f " $PGDATA /.archive_configured" ]; then
147- log " INFO" " Restarting PostgreSQL to apply archive configuration..."
148-
145+ # Verify archive_mode is properly configured
146+ log " INFO" " Verifying archive_mode configuration..."
147+
148+ # Check if archive_mode is enabled
149+ archive_mode_status=$( su-exec postgres psql -d postgres -t -c " SHOW archive_mode;" 2> /dev/null | tr -d ' [:space:]' || echo " " )
150+
151+ if [ " $archive_mode_status " != " on" ]; then
152+ log " WARN" " Archive mode is not enabled (current: $archive_mode_status ), restarting PostgreSQL..."
153+
149154 # Stop current PostgreSQL instance
150155 kill $POSTGRES_PID 2> /dev/null || true
151156 wait $POSTGRES_PID 2> /dev/null || true
152-
157+
158+ # Ensure archive settings are in postgresql.conf
159+ if ! grep -q " archive_mode = on" " $PGDATA /postgresql.conf" ; then
160+ echo " archive_mode = on" >> " $PGDATA /postgresql.conf"
161+ fi
162+ if ! grep -q " wal_level = replica" " $PGDATA /postgresql.conf" ; then
163+ echo " wal_level = replica" >> " $PGDATA /postgresql.conf"
164+ fi
165+ if ! grep -q " archive_command" " $PGDATA /postgresql.conf" ; then
166+ echo " archive_command = 'pgbackrest --stanza=${PGBACKREST_STANZA:- main} archive-push %p'" >> " $PGDATA /postgresql.conf"
167+ fi
168+
153169 # Start PostgreSQL again
154170 su-exec postgres postgres &
155171 POSTGRES_PID=$!
156-
172+
157173 # Wait for PostgreSQL to be ready again
158174 if ! wait_for_postgres 120; then
159175 log " ERROR" " PostgreSQL failed to restart with archive configuration"
160176 kill $POSTGRES_PID 2> /dev/null || true
161177 return 1
162178 fi
163-
164- # Mark archive as configured
165- touch " $PGDATA /.archive_configured"
166- log " INFO" " PostgreSQL restarted successfully with archive configuration"
179+
180+ # Verify archive_mode is now enabled
181+ archive_mode_status=$( su-exec postgres psql -d postgres -t -c " SHOW archive_mode;" 2> /dev/null | tr -d ' [:space:]' || echo " " )
182+ if [ " $archive_mode_status " = " on" ]; then
183+ log " INFO" " Archive mode successfully enabled"
184+ else
185+ log " ERROR" " Failed to enable archive mode (current: $archive_mode_status )"
186+ return 1
187+ fi
188+ else
189+ log " INFO" " Archive mode is already enabled"
167190 fi
168191
169192 log " INFO" " PostgreSQL is ready"
0 commit comments