Gustaf, Thanks for the advice.
What I did was to upgrade the database to 9.4.0 then try to run the 5.8.1 upgrade scripts.
This does not work, even after using Naviserver. I still get the dollar quoting error message. I used install-ns to install naviserver.
Installing data model for Kernel 5.8.1...
Loading data model /var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql...
COMMIT
COMMIT
COMMIT
COMMIT
Failed to install Kernel, version 5.8.1. The following error was generated:
psql:/var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql:16: ERROR: unterminated dollar-quoted string at or near "$$ DECLARE BEGIN insert into acs_logs (log_id, log_level, log_key, message) values (nextval('t_acs_log_id_seq'), 'notice', notice__log_key, notice__message);" at character 121 psql:/var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql:18: ERROR: syntax error at or near "return" at character 5 psql:/var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql:19: WARNING: there is no transaction in progress psql:/var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql:20: ERROR: unterminated dollar-quoted string at or near "$$ LANGUAGE plpgsql;" at character 1 psql:/var/lib/aolserver/taxonomy/packages/acs-kernel/sql/postgresql/upgrade/upgrade-5.8.0d2-5.8.0d3.sql:35: ERROR: unterminated dollar-quoted string at or near "$$ DECLARE BEGIN insert into acs_logs (log_id, log_level, log_key, message) values