Be aware of any operation regarding database could cause data lost. BACKUP FIRST!
In every major release of Postgres, there are always bunch of breaking changes introduced, regarding it's store engine, so it's impossible to upgrade Postgres by just changing the tag of postgres image from like :16 to :17.
A tool comes with postgres image called pg_upgrade is made to help you deal with the breaking changes.
The blow demonstrates upgrading Postgres service in docker compose from 16 to 17
Do a manual backup like the way we introduced in Backup and Restore.
Import your data by pg_restore
If you prefer to use a standalone Postgres, you can simply add DATABASE_URL to your .env file and remove the default one in docker-compose.yml.