puddle.town

Upgrading Mastodon to PostgreSQL 17

Upgrading Mastodon to PostgreSQL 17

This post outlines how I upgraded my Mastodon instance from PostgreSQL 16 to 17 on Ubuntu 24.04. I should emphasize up front that no one is suggesting this is necessary or even a good idea at this point. It seems not many have bothered to upgrade yet; Postgres 16 is entirely sufficient. However, I noticed on Ubuntu 24.04, Postgres 17 was installed side-by-side along 16 when it came out two weeks ago. Also, I'm a tech-curious bleeding-edge type of person, and my single-use instance only serves me so there was little risk. If you run a larger, multi-user instance, please do proceed with caution. In particular, you need to make sure you have sufficient RAM and disk space to handle what is essentially a copy of the entire DB.

Warnings out of the way, the process was actually incredibly easy and seamless. I experienced no issues during or after the migration.

The crux of the process lies with the commands pg_dropcluster and pg_upgradecluster. I followed an old guide by @thomas@metalhead.club for inspiration.

Preparation

To be cautious, I powered off my instance and performed an offline backup using Hetzner's console. Your provider likely has the same option, or if you're on bare metal, you probably already have a backup system in place.

After performing the backup and bringing the server back online, I stopped the Mastodon services to ensure the upgrade could proceed uninterrupted, and to prevent writes to the DB during the process:

sudo systemctl stop mastodon-sidekiq
sudo systemctl stop mastodon-streaming
sudo systemctl stop mastodon-web

Migration

It really is as simple as:

sudo pg_dropcluster --stop 17 main
sudo pg_upgradecluster 16 main

The upgrade script will run for a while and do its thing, giving you some friendly output along the way which you may want to save for reference. Once it's done, it's good to take a few minutes to verify that everything is working. Load up Mastodon in a browser, maybe use your favorite mobile app, and generally kick the tires for a bit to make sure things are working as expected. As an extra sanity check, I rebooted my server to make sure the instance came up cleanly.

One thing to note: pg_upgradecluster should automatically copy the contents of /etc/postgresql/16/main/postgresql.conf to /etc/postgresql/17/main/postgresql.conf. It's worth taking a look at the latter to make sure this happened and that the configuration is as expected.

Cleanup

Assuming you've made it this far, you can now cleanup the remnants of the Postgres 16 installation. First drop the old DB:

sudo pg_dropcluster 16 main

Then you can remove the packages specific to Postgres 16:

sudo apt remove postgresql-16 postgresql-client-16

That's it!

#linux #mastodon #postgres #postgresql #sql