perl -pi.BAK -aF'/(\h*[#=]\h*)/,$_,4' -le '
$F[4] = "archive";
$_ = join $,, @F[2..$#F] if /^\h*#\h*wal_level\h*=/;
' /var/lib/pgsql/data/postgresql.conf
The line is split into fields `@F` and the delimiters are included as well. The parameter 4 in the field split option `-F` will constrain the number of fields to that. The fields from 3rd onwards are joined together using empty space, which is the default value of the `OFS = $,`