@@ -66,6 +66,18 @@ start_postgresql() {
6666 echo " log_directory = 'log'" >> " $PGDATA /postgresql.conf"
6767 echo " log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'" >> " $PGDATA /postgresql.conf"
6868
69+ # Enable WAL archiving for pgBackRest (CRITICAL for backup functionality)
70+ echo " wal_level = replica" >> " $PGDATA /postgresql.conf"
71+ echo " archive_mode = on" >> " $PGDATA /postgresql.conf"
72+ echo " archive_command = 'pgbackrest --stanza=${PGBACKREST_STANZA:- main} archive-push %p'" >> " $PGDATA /postgresql.conf"
73+ echo " max_wal_senders = 3" >> " $PGDATA /postgresql.conf"
74+ echo " wal_keep_size = 1GB" >> " $PGDATA /postgresql.conf"
75+
76+ # Additional settings for better backup performance
77+ echo " checkpoint_completion_target = 0.9" >> " $PGDATA /postgresql.conf"
78+ echo " wal_buffers = 16MB" >> " $PGDATA /postgresql.conf"
79+ echo " checkpoint_timeout = 15min" >> " $PGDATA /postgresql.conf"
80+
6981 # Set up pg_hba.conf for authentication
7082 {
7183 echo " local all all trust"
@@ -130,6 +142,30 @@ EOSQL
130142 return 1
131143 fi
132144
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+
149+ # Stop current PostgreSQL instance
150+ kill $POSTGRES_PID 2> /dev/null || true
151+ wait $POSTGRES_PID 2> /dev/null || true
152+
153+ # Start PostgreSQL again
154+ su-exec postgres postgres &
155+ POSTGRES_PID=$!
156+
157+ # Wait for PostgreSQL to be ready again
158+ if ! wait_for_postgres 120; then
159+ log " ERROR" " PostgreSQL failed to restart with archive configuration"
160+ kill $POSTGRES_PID 2> /dev/null || true
161+ return 1
162+ fi
163+
164+ # Mark archive as configured
165+ touch " $PGDATA /.archive_configured"
166+ log " INFO" " PostgreSQL restarted successfully with archive configuration"
167+ fi
168+
133169 log " INFO" " PostgreSQL is ready"
134170 return 0
135171}
0 commit comments