# Discourse PostgreSQL 15 升级

**URL:** <https://www.isharkfly.com/t/discourse-postgresql-15/16899>\
**Category:** Discourse\
**Tags:** discourse\
**Created:** [February 10, 2025, 2:42am UTC](https://www.isharkfly.com/t/discourse-postgresql-15/16899 "2025-02-10T02:42:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [February 10, 2025, 2:42am UTC](https://www.isharkfly.com/t/discourse-postgresql-15/16899/1 "2025-02-10T02:42:54Z")

</div>

> ⚠ **WARNING!** 如果你的数据库很大的话，你需要很多的磁盘空间才能完成升级操作（至少需要 2 倍空闲的当前数据库空间），并且在升级的时候需要小心，同时先完成备份后再升级。

Discourse 的官方已经完成了对 Discourse 容器的修改，当 Discourse 的管理员对容器进行重构的话，最新的 Discourse 版本将会自动升级数据库到 PostgreSQL 15。虽然这次升级是从 PostgreSQL 13 到 PostgreSQL 15，如果再 2020 年的时候，你的 Discourse 按照实例没有升级到 PostgreSQL 13（[PostgreSQL 13 update - Announcements - Discourse Meta](https://meta.discourse.org/t/postgresql-13-update/172563) ），你可以跳过这个升级过程，直接升级到 PostgreSQL 15。

如果在本次升级之前，你都没有升级过 PostgreSQL 数据库，那么你可以把 Discourse 的配置文件从 `templates/postgres.12.template.yml` 修改为 `templates/postgres.template.yml`，上面的配置文件是在 `app.yml` 中定义的。

针对任何数据库或系统的升级，我们 **强烈建议** 对需要升级的实例进行备份。

多备份，多备份不是坏事。

## 升级

### [官方支持的 Discourse 安装指南](https://meta.discourse.org/t/142537?silent=true) (单容器安装)

在你下一次对你的 Discourse 重构的时候，你将会在重构中看到下面的提示：

```auto
-------------------------------------------------------------------------------------
UPGRADE OF POSTGRES COMPLETE

Old 13 database is stored at /shared/postgres_data_old

To complete the upgrade, rebuild again using:

./launcher rebuild app
-------------------------------------------------------------------------------------

```

当看到上面的消息，说明这次升级非常顺利，所有的数据库升级都已经完成了，你只需要重构 Discourse 安装实例即可。

我们也才完成了升级：[2025年2月的 Discourse 版本重构后会升级 PostgreSQL 到15](https://www.isharkfly.com/t/2025-2-discourse-postgresql-15/16897)

整体的升级过程还是非常丝滑，没有什么太大的问题。

### 数据中心安装

如果你的 Discourse 安装是通过数据中心容器方式进行安装的，这种方式的安装是通过 Discourse 官方提供的 discourse\_docker 仓库进行安装的话，你需要以安装的方式关闭 PostgreSQL 服务。

当前，我们会有后台运行的查询来对数据库每隔几分钟就会进行下查询。因此关闭 Web 容器将会有助于数据库容器的正常关闭。

```auto
./launcher stop web_only
./launcher stop data
./launcher rebuild data
./launcher rebuild data
./launcher rebuild web_only

```

在对数据容器进行重构之前，你可以查看下 PostgreSQL 日志，以确定数据库容器被正确的关闭。

运行 `tail -f shared/standalone/log/var-log/postgres/current` 命令，如果能看到下面的输出，则说明数据库容器被正确的关闭：

```auto
2025-01-24 09:19:06.437 UTC [37] LOG: received smart shutdown request
2025-01-24 09:19:06.444 UTC [37] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
2025-01-24 09:19:06.446 UTC [49] LOG: shutting down
2025-01-24 09:19:06.468 UTC [37] LOG: database system is shut down

```

### 进行手动更新 / 磁盘空间检查

> ⚠⚠⚠  
> 在对这些进行操作之前，请务必备份 POSTGRES\_DATA 目录  
> ⚠⚠⚠

如果当前的磁盘空间有限，但你也没有办法获得更多的磁盘空间的话，可以尝试下下面的方法：following:

```bash
./launcher stop app #(or both web_only and data if that is your case)
mkdir -p /var/discourse/shared/standalone/postgres_data_new
docker run --rm \
	--entrypoint=/bin/bash \
	-v /var/discourse/shared/standalone/postgres_data:/var/lib/postgresql/13/data \
	-v /var/discourse/shared/standalone/postgres_data_new:/var/lib/postgresql/15/data \
	tianon/postgres-upgrade:13-to-15 \
	-c "apt-get update && apt-get install -y postgresql-13-pgvector postgresql-15-pgvector &&
	docker-upgrade"
mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old
mv /var/discourse/shared/standalone/postgres_data_new /var/discourse/shared/standalone/postgres_data
docker run --rm -v /var/discourse/shared/standalone:/shared \
local_discourse/app chown -R postgres:postgres /shared/postgres_data #(or local_discourse/data)
./launcher rebuild app #(or first data and then web_only if that is your case)

```

在官方的数据库升级测试情况下，至少需要有超过当前数据库使用空间 1 倍以上的空闲空间才可以。

建议是需要保留至少 2 倍以上的空闲空间。

如果你使用的不是默认的路径，你可以尝试下面的命令来替换下 `docker` 命令：

```bash
# change 'en_US.UTF-8' to your locale
docker run --rm \
	--entrypoint=/bin/bash \
	-e LANG='en_US.UTF-8' \
	-v /var/discourse/shared/standalone/postgres_data:/var/lib/postgresql/13/data \
	-v /var/discourse/shared/standalone/postgres_data_new:/var/lib/postgresql/15/data \
	tianon/postgres-upgrade:13-to-15 \
	-c 'sed -i "s/^# $LANG/$LANG/" /etc/locale.gen && locale-gen &&
	apt-get update && apt-get install -y postgresql-13-pgvector postgresql-15-pgvector &&
	docker-upgrade'

```

## 延后更新

如果你希望延后 PostgreSQL 的更新的话，你可以在升级之前修改 app.yml 配置文件中的配置，把 `"templates/postgres.template.yml"` 修改为 `"templates/postgres.13.template.yml"`。

但是 Discourse 的官方并不推荐这样去做，这是因为在随后可能一些系统的管理员就忘记了本次升级了。

## 升级后的可选操作

### 优化 PostgreSQL 统计信息

在升级完成后，新的 PostgreSQL 没有提供数据表的统计信息，你可以使用使用下面的命令来生成：

```auto
docker exec -u postgres app \
/usr/lib/postgresql/15/bin/vacuumdb -d discourse --analyze-in-stages

```

### 清理老的数据

在老的标准 Discourse 安装，你可以使用下面的命令来删除老的 PG13 数据格式：

```auto
cd /var/discourse
./launcher cleanup

```

如果你使用的是独立的数据容器，你需要使用下面的命令来删除不需要的备份：

```auto
rm -fr /var/discourse/shared/data/postgres_data_old/

```

## FAQ（问答）

### The source cluster was not shut down cleanly

如果在升级的时候，收到了上面的信息的话，你可以尝试使用下面的简单的办法来进行修复。

使用命令 `./launcher start app` 来重启老的容器，等待几分钟等容器的正常运行。

运行命令`./launcher stop app` 来关闭容器。

使用下面的命令来查看日志，确定容器被正确的关闭：

```auto
tail -f shared/standalone/log/var-log/postgres/current
2025-01-24 09:19:06.437 UTC [37] LOG: received smart shutdown request
2025-01-24 09:19:06.444 UTC [37] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
2025-01-24 09:19:06.446 UTC [49] LOG: shutting down
2025-01-24 09:19:06.468 UTC [37] LOG: database system is shut down

```

如果在上面的日志中，没有看到数据库被正确的关闭和退出的话，可以使用命令 `./launcher enter app` 进入容器，然后运行下面的命令来关闭部分。

```auto
export SVWAIT=300
sv stop nginx
sv stop unicorn
sv stop postgres
exit

```

当服务关闭后再次查看日志信息。

如果日志信息和上面显示的日志信息相同，显示为数据库被正确的关闭和退出，然后你就可以使用 `./launcher rebuild app`命令进行重构。

### lc\_collate values for database “postgres” do not match

上面的这个错误信息是因为针对数据库，使用了不是默认的字符集或信息。

上面的配置信息表明，你需要下面的 3 个配置变量才能保证正常的升级。

找到 app.yml 配置文件中的 `env: ` 部分，确定下面的内容被正确的配置。

```yaml
  LC_ALL: en_US.UTF-8
  LANG: en_US.UTF-8
  LANGUAGE: en_US.UTF-8

```

修改 `en_US.UTF-8` 为你需要的字符集。

### Every rebuild does the upgrade again aka upgrade loop

当上面的错误信息存在的话，通常查看你的日志会看到下面的内容：

```auto
mv: cannot move '/shared/postgres_data' to '/shared/postgres_data_old/postgres_data': Directory not empty
mv: cannot move '/shared/postgres_data_new' to '/shared/postgres_data/postgres_data_new': Directory not empty

```

上面的内容表明的是在上面的文件夹中还有上次升级存在的文件，可以简单的把上面的文件夹移动到不同的文件夹下保存。

### Upgrade Complete suggestion scripts - do I need to do anything?

当升级完成后，系统可能会提示下面的升级信息：

```auto
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/lib/postgresql/15/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

```

针对上面的脚本运行提示信息，可以选择忽略。

### I did skip the PostgreSQL 13 update, what to do now?

如果你当前运行的 PostgreSQL 版本比 PostgreSQL 13 还要老的话。

直接进行升级就行，不需要把老的 PostgreSQL 升级到 PostgreSQL 13 后再升级到 PostgreSQL 15。

本次升级会直接升级到 PostgreSQL 15。

重构索引能够显著降低磁盘的使用，请根据官方的说明来对索引进行重构： [PostgreSQL 13 update - Announcements - Discourse Meta](https://meta.discourse.org/t/postgresql-13-update/172563#re-creating-the-indexes-8)
