Upgrade problem with postgres database "setup storage error: storage: migrate up error: Dirty database version 1. Fix and force version."

I am trying to upgrade from a fairly old version (lora-app-server 3.3.1 → chirpstack-application-server 3.17.1) and when the upgrade finishes, the application server log throws the error "setup storage error: storage: migrate up error: Dirty database version 1. Fix and force version. "
I have two db schemas (as and ns), in the postgre log (v9.6) you see the following:

ERROR: relation “code_migration” does not exist
STATEMENT: lock table code_migration
ERROR: relation “code_migration” does not exist
STATEMENT: lock table code_migration
ERROR: relation “application” already exists
STATEMENT: create table application (
app_eui bytea primary key,
name character varying (100) not null
);

create table node (
dev_eui bytea primary key,
app_eui bytea references application on delete cascade not null,
app_key bytea not null,
used_dev_nonces bytea
);

create index node_app_eui on node (app_eui);

ERROR: relation “code_migration” does not exist
STATEMENT: lock table code_migration

Please help

upgrade PostgreSQL too.

Thanks for answering @eugenev, my postgresql has version 9.6 + 181 + deb9u3. According to the documentation, it is worth 9.5+

i have the same problem exactly. Im running PostgreSQL 13.5. im triying to upgrade Loraserver application server 3.3.1 to 3.17.3.
¿How can I use the new version of chirpstack without losing my data on the LoraServer 3.3.1 version?
@brocaar ¿can you help us?
Thank you