Upgrading postgresql can't be done in-place, you have to setup a new DB with the expected version and restore your content in this DB.
I had a few containers running older versions of postgres lying around (13 and 14), and I wanted to upgrade them to postgresql 15. Here's the script I came up with.
{{<noteclass="warning"title="⚠️ Warning">}}
When handling production data, don't blindly run this as a script. Run each step manually and double-check that you aren't destroying anything.
{{</note>}}
{{<noteclass="info">}}
This post is inspired by [Thomas Bandt's](https://thomasbandt.com/postgres-docker-major-version-upgrade), with some tweaks in the process, and less writing.
{{</note>}}
```bash
app_name="app"
db_user="user"
db_name="db"
# Stop the running app(s)
# to prevent modifications during the backup & restore
docker compose stop $app_name
# Dump DB & roles
# I'm using custom scripts for this part in order to run
If your database is not in a container, then [`pg_upgrade`](https://www.postgresql.org/docs/current/pgupgrade.html) may be a better option: faster, less hassle and doesn't require manually dumping and restoring.
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.