-
Notifications
You must be signed in to change notification settings - Fork 608
Add PostgreSQL upgrade guide #8894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
041050b
Add PostgreSQL upgrade guide
hanzei cb9932e
manual cleanup
hanzei d470a77
Scope Docker postgres upgrade guide to mattermost/docker
hanzei 3d56d01
Reference POSTGRES_IMAGE_TAG in Docker upgrade steps
hanzei fe82d1a
Fix Docker restart command and upgrade notice severity
hanzei 2bbb2e8
Merge branch 'master' into worktree-lexical-churning-giraffe
hanzei 0dfba3c
Merge branch 'master' into worktree-lexical-churning-giraffe
hanzei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
source/administration-guide/upgrade/upgrading-postgres.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| Upgrade PostgreSQL | ||
| ================== | ||
|
|
||
| .. include:: ../../_static/badges/all-commercial.rst | ||
| :start-after: :nosearch: | ||
|
|
||
| Mattermost follows the `PostgreSQL community versioning policy <https://www.postgresql.org/support/versioning/>`_, which provides 5 years of support per major version. When a PostgreSQL version reaches end-of-life, Mattermost drops support for it in a subsequent release. See the :doc:`software and hardware requirements </deployment-guide/software-hardware-requirements>` documentation for the currently supported PostgreSQL versions. | ||
|
|
||
| When upgrading PostgreSQL, refer to the `official PostgreSQL upgrade documentation <https://www.postgresql.org/docs/current/upgrading.html>`_ for comprehensive guidance. This page covers the steps specific to Mattermost deployments. | ||
|
|
||
| Before you begin | ||
| ---------------- | ||
|
|
||
| 1. **Back up your database.** Always take a full database backup before upgrading. See the :doc:`backup and disaster recovery </deployment-guide/backup-disaster-recovery>` documentation. | ||
|
|
||
| 2. **Check supported versions.** Confirm the target PostgreSQL version is supported by your Mattermost release. See :doc:`software and hardware requirements </deployment-guide/software-hardware-requirements>`. | ||
|
|
||
| 3. **Stop Mattermost.** Shut down the Mattermost server before starting the database upgrade to prevent data writes during the process. | ||
|
|
||
|
|
||
| Upgrade a bare-metal PostgreSQL server | ||
| --------------------------------------- | ||
|
|
||
| There are two main approaches for upgrading PostgreSQL on a bare-metal or virtual machine: | ||
|
|
||
| - **pg_upgrade** (in-place): Faster; upgrades the data directory without a full dump/restore cycle. Recommended for large databases. | ||
| - **pg_dump / pg_restore** (logical): Simpler and safer for cross-machine migrations or when in-place upgrade is not possible. | ||
|
|
||
| Using pg_upgrade | ||
| ~~~~~~~~~~~~~~~~ | ||
|
|
||
| ``pg_upgrade`` allows you to upgrade between major PostgreSQL versions without a full export. Both the old and new PostgreSQL versions must be installed side-by-side. | ||
|
|
||
| 1. Install the new PostgreSQL version alongside the existing one using your package manager. | ||
|
|
||
| 2. Stop the existing PostgreSQL service: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo systemctl stop postgresql | ||
|
|
||
| 3. Run ``pg_upgrade`` as the ``postgres`` user, specifying the binary and data directories for both versions. Replace ``<old_version>`` and ``<new_version>`` with the appropriate version numbers (e.g. ``15`` and ``16``): | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo -u postgres /usr/lib/postgresql/<new_version>/bin/pg_upgrade \ | ||
| --old-datadir /var/lib/postgresql/<old_version>/main \ | ||
| --new-datadir /var/lib/postgresql/<new_version>/main \ | ||
| --old-bindir /usr/lib/postgresql/<old_version>/bin \ | ||
| --new-bindir /usr/lib/postgresql/<new_version>/bin | ||
|
|
||
| 4. Update your system to start the new PostgreSQL version by default, then start the service: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo systemctl start postgresql | ||
|
|
||
| For full ``pg_upgrade`` reference, see the `official pg_upgrade documentation <https://www.postgresql.org/docs/current/pgupgrade.html>`_. | ||
|
|
||
| Using pg_dump and pg_restore | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| This approach exports the entire database, installs the new PostgreSQL version, and restores the data. It is simpler but requires downtime proportional to database size. | ||
|
|
||
| 1. Export the Mattermost database (replace ``mattermost`` with your database name and ``mmuser`` with your database user): | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| pg_dump -U mmuser -Fc mattermost > mattermost_backup.dump | ||
|
|
||
| 2. Install the new PostgreSQL version and create the database user and database: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo -u postgres psql -c "CREATE USER mmuser WITH PASSWORD 'your_password';" | ||
| sudo -u postgres psql -c "CREATE DATABASE mattermost OWNER mmuser;" | ||
|
|
||
| 3. Restore the database into the new PostgreSQL instance: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| pg_restore -U mmuser -d mattermost mattermost_backup.dump | ||
|
|
||
| Upgrade PostgreSQL in Docker | ||
| ----------------------------- | ||
|
|
||
| .. note:: | ||
|
|
||
| The steps below are written for the official `Mattermost Docker deployment <https://github.com/mattermost/docker>`_. If you are using a custom Docker setup, adapt the container and volume names accordingly. | ||
|
|
||
| When running PostgreSQL in Docker, ``pg_dump``/``pg_restore`` is the recommended upgrade approach. In-place ``pg_upgrade`` is complex in containers because it requires both old and new binaries in the same container. | ||
|
|
||
| 1. Stop the Mattermost container: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| docker stop mattermost | ||
|
|
||
| 2. Dump the database from the running PostgreSQL container (replace ``mattermost``, ``mmuser``, and ``db`` with your database name, user, and container name): | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| docker exec postgres pg_dump -U mmuser -Fc mattermost > mattermost_backup.dump | ||
|
|
||
| 3. Stop the existing PostgreSQL container: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| docker stop db | ||
|
|
||
| 4. Update the PostgreSQL image tag in your ``docker-compose.yml`` (or ``docker run`` command) to the new version. For example: | ||
|
|
||
| .. code-block:: yaml | ||
|
|
||
| image: postgres:16 | ||
|
|
||
| 5. Start the new PostgreSQL container. The existing data volume is mounted but PostgreSQL will not be able to read data directory files from a different major version, so the volume should be empty or new: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| docker start db | ||
|
|
||
| .. note:: | ||
|
|
||
| If your volume already contains data from the old major version, PostgreSQL will refuse to start. In that case, create a new named volume for the new container, then restore from the dump in the next step. | ||
|
|
||
| 6. Recreate the database user and database in the new container, then restore: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| docker exec -i postgres psql -U postgres -c "CREATE USER mmuser WITH PASSWORD 'your_password';" | ||
| docker exec -i postgres psql -U postgres -c "CREATE DATABASE mattermost OWNER mmuser;" | ||
| docker exec -i postgres pg_restore -U mmuser -d mattermost < mattermost_backup.dump | ||
|
|
||
| After the upgrade | ||
| ------------------ | ||
|
|
||
| After upgrading PostgreSQL, run ``ANALYZE VERBOSE`` on the Mattermost database. This re-populates the ``pg_statistics`` table used by PostgreSQL to generate optimal query plans. Skipping this step can result in degraded database performance. | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo -u postgres psql -d mattermost -c "ANALYZE VERBOSE;" | ||
|
|
||
| Once complete, restart Mattermost: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| sudo systemctl start mattermost | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.