重建失败:数据目录 /shared/postgres_data 不能由 root 拥有

提前为一些细节缺失道歉……我确实把自己搞得很混乱……
如果您非常了解 /var/discourse/shared/standalone 中的文件权限要求,或许可以直接跳到本文底部查看核心问题,而跳过那些让我陷入困境的细节 :sweat:

最新的基于 Web 的升级搞砸了我的安装——我认为情况类似于 https://meta.discourse.org/t/error-updating-from-3-4-0-beta3-dev-to-3-4-0-beta3/349847,但我当时应该是在 3.4.0.beta4-dev 版本上。

我的系统也是 Ubuntu 20,现在运行 ./launcher rebuild app 会返回以下错误:

ERROR: Docker version 19.03.13 not supported, please upgrade to at least 20.10.0, or recommended 24.0.7

我认为这不太可能实现,所以我创建了一台新服务器,并按照 Move a Discourse site to another VPS with rsync 中的步骤操作,同时额外添加了从旧服务器迁移卷的命令:
rsync -rvz root@104.236.77.249:/var/lib/docker/volumes/ /var/lib/docker/volumes

现在,在新 Ubuntu 24 服务器上,运行 ./launcher rebuild app 返回了以下错误:

Stopping PostgreSQL 15 database server: mainError: Data directory /shared/postgres_data must not be owned by root … failed!

完整命令输出
x86_64 arch detected.
WARNING: containers/app.yml file is world-readable. You can secure this file by running: chmod o-rwx containers/app.yml
Ensuring launcher is up to date
Launcher is up-to-date
2.0.20250129-0720: Pulling from discourse/base
Digest: sha256:01b8516e5504c0e9bc3707773015ff4407be03a89154194ff3b5b8699291bc26
Status: Image is up to date for discourse/base:2.0.20250129-0720
docker.io/discourse/base:2.0.20250129-0720
/usr/local/lib/ruby/gems/3.3.0/gems/pups-1.2.1/lib/pups.rb
/usr/local/bin/pups --stdin
I, [2025-02-05T15:28:12.350842 #1]  INFO -- : Reading from stdin
I, [2025-02-05T15:28:12.364293 #1]  INFO -- : File > /etc/service/postgres/run  chmod: +x  chown: 
I, [2025-02-05T15:28:12.369358 #1]  INFO -- : File > /etc/service/postgres/log/run  chmod: +x  chown: 
I, [2025-02-05T15:28:12.373684 #1]  INFO -- : File > /etc/runit/3.d/99-postgres  chmod: +x  chown: 
I, [2025-02-05T15:28:12.378720 #1]  INFO -- : File > /root/install_postgres  chmod: +x  chown: 
I, [2025-02-05T15:28:12.383793 #1]  INFO -- : File > /root/upgrade_postgres  chmod: +x  chown: 
I, [2025-02-05T15:28:12.384811 #1]  INFO -- : Replacing data_directory = '/var/lib/postgresql/15/main' with data_directory = '/shared/postgres_data' in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.386020 #1]  INFO -- : Replacing (?-mix:#?listen_addresses *=.*) with listen_addresses = '*' in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.391549 #1]  INFO -- : Replacing (?-mix:#?synchronous_commit *=.*) with synchronous_commit = $db_synchronous_commit in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.392476 #1]  INFO -- : Replacing (?-mix:#?shared_buffers *=.*) with shared_buffers = $db_shared_buffers in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.393539 #1]  INFO -- : Replacing (?-mix:#?work_mem *=.*) with work_mem = $db_work_mem in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.394234 #1]  INFO -- : Replacing (?-mix:#?default_text_search_config *=.*) with default_text_search_config = '$db_default_text_search_config' in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.394860 #1]  INFO -- : Replacing (?-mix:#?checkpoint_segments *=.*) with checkpoint_segments = $db_checkpoint_segments in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.395701 #1]  INFO -- : Replacing (?-mix:#?logging_collector *=.*) with logging_collector = $db_logging_collector in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.396354 #1]  INFO -- : Replacing (?-mix:#?log_min_duration_statement *=.*) with log_min_duration_statement = $db_log_min_duration_statement in /etc/postgresql/15/main/postgresql.conf
I, [2025-02-05T15:28:12.396997 #1]  INFO -- : Replacing (?-mix:^#local +replication +postgres +peer$) with local replication postgres  peer in /etc/postgresql/15/main/pg_hba.conf
I, [2025-02-05T15:28:12.397677 #1]  INFO -- : Replacing (?-mix:^host.*all.*all.*127.*$) with host all all 0.0.0.0/0 md5 in /etc/postgresql/15/main/pg_hba.conf
I, [2025-02-05T15:28:12.398272 #1]  INFO -- : Replacing (?-mix:^host.*all.*all.*::1\/128.*$) with host all all ::/0 md5 in /etc/postgresql/15/main/pg_hba.conf
I, [2025-02-05T15:28:12.398841 #1]  INFO -- : > if [ -f /root/install_postgres ]; then
  /root/install_postgres && rm -f /root/install_postgres
elif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
fi

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
W: https://dl.yarnpkg.com/debian/dists/stable/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
debconf: delaying package configuration, since apt-utils is not installed
I, [2025-02-05T15:28:32.940792 #1]  INFO -- : Generating locales (this might take a while)...
  en_US.UTF-8... done
Generation complete.
Upgrading PostgreSQL from version 13 to 15
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /shared/postgres_data_new ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

    /usr/lib/postgresql/15/bin/pg_ctl -D /shared/postgres_data_new -l logfile start

Get:1 http://deb.debian.org/debian bookworm-backports InRelease [59.0 kB]
Get:2 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:3 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:4 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:5 https://deb.nodesource.com/node_22.x nodistro InRelease [12.1 kB]
Get:6 https://dl.yarnpkg.com/debian stable InRelease [17.1 kB]
Get:7 http://deb.debian.org/debian bookworm-backports/main amd64 Packages [282 kB]
Get:8 http://deb.debian.org/debian bookworm/main amd64 Packages [8,792 kB]
Get:9 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg InRelease [129 kB]
Get:10 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.5 kB]
Get:11 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [243 kB]
Get:12 https://deb.nodesource.com/node_22.x nodistro/main amd64 Packages [5,274 B]
Get:13 https://dl.yarnpkg.com/debian stable/main all Packages [10.9 kB]
Get:14 https://dl.yarnpkg.com/debian stable/main amd64 Packages [10.9 kB]
Get:15 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 Packages [360 kB]
Fetched 10.2 MB in 2s (4,214 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  postgresql-client-13
Suggested packages:
  postgresql-doc-13
The following NEW packages will be installed:
  postgresql-13 postgresql-13-pgvector postgresql-client-13
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 17.3 MB of archives.
After this operation, 56.7 MB of additional disk space will be used.
Get:1 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-client-13 amd64 13.18-1.pgdg120+1 [1,523 kB]
Get:2 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-13 amd64 13.18-1.pgdg120+1 [15.4 MB]
Get:3 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-13-pgvector amd64 0.8.0-1.pgdg120+1 [297 kB]
Fetched 17.3 MB in 2s (8,411 kB/s)
Selecting previously unselected package postgresql-client-13.
(Reading database ... 33363 files and directories currently installed.)
Preparing to unpack .../postgresql-client-13_13.18-1.pgdg120+1_amd64.deb ...
Unpacking postgresql-client-13 (13.18-1.pgdg120+1) ...
Selecting previously unselected package postgresql-13.
Preparing to unpack .../postgresql-13_13.18-1.pgdg120+1_amd64.deb ...
Unpacking postgresql-13 (13.18-1.pgdg120+1) ...
Selecting previously unselected package postgresql-13-pgvector.
Preparing to unpack .../postgresql-13-pgvector_0.8.0-1.pgdg120+1_amd64.deb ...
Unpacking postgresql-13-pgvector (0.8.0-1.pgdg120+1) ...
Setting up postgresql-client-13 (13.18-1.pgdg120+1) ...
Setting up postgresql-13 (13.18-1.pgdg120+1) ...
Creating new PostgreSQL cluster 13/main ...
/usr/lib/postgresql/13/bin/initdb -D /var/lib/postgresql/13/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/13/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctlcluster 13 main start

Warning: The selected stats_temp_directory /var/run/postgresql/13-main.pg_stat_tmp
is not writable for the cluster owner. Not adding this setting in
postgresql.conf.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up postgresql-13-pgvector (0.8.0-1.pgdg120+1) ...
Processing triggers for postgresql-common (267.pgdg120+1) ...
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
Stopping PostgreSQL 13 database server: main.
Stopping PostgreSQL 15 database server: mainError: Data directory /shared/postgres_data must not be owned by root ... failed!
 failed!

could not open version file "/shared/postgres_data/PG_VERSION": Permission denied
Failure, exiting
-------------------------------------------------------------------------------------
UPGRADE OF POSTGRES FAILED

Please visit https://meta.discourse.org/t/postgresql-15-update/349515 for support.

You can run ./launcher start app to restart your app in the meanwhile
-------------------------------------------------------------------------------------



FAILED
--------------------
Pups::ExecError: if [ -f /root/install_postgres ]; then
  /root/install_postgres && rm -f /root/install_postgres
elif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
fi
 failed with return #<Process::Status: pid 18 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.3.0/gems/pups-1.2.1/lib/pups/exec_command.rb:132:in `spawn'
exec failed with the params {"tag"=>"db", "cmd"=>"if [ -f /root/install_postgres ]; then\n  /root/install_postgres && rm -f /root/install_postgres\nelif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then\n  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1\nfi\n"}
bootstrap failed with exit code 1
** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.

/var/discourse/shared 文件夹在旧服务器和新服务器上均由 root 拥有。不过,深入查看更深层级时,我注意到 /var/discourse/shared/standalone 中某些文件夹的权限存在差异……

旧服务器:

/var/discourse/shared/standalone# ls -alh
total 56K
drwxr-xr-x 14 root      root       4.0K Jan 22  2021 .
drwxr-xr-x  3 root      root       4.0K Mar 12  2016 ..
drwxr-xr-x  3      1000 www-data   4.0K Sep 21  2015 backups
drwxr-xr-x  8 root      root       4.0K Dec 10 19:36 letsencrypt
drwxr-xr-x  4 root      root       4.0K Jun 12  2015 log
drwxr-xr-x  2 syslog    syslog     4.0K Jun 12  2015 postgres_backup
drwx------ 19 syslog    syslog     4.0K Feb  4 20:03 postgres_data
drwx------ 20 colord    landscape  4.0K Jan 22  2021 postgres_data_old
drwxrwsr-x  7 syslog    syslog     4.0K Feb  4 20:03 postgres_run
drwxr-xr-x  2 landscape messagebus 4.0K Feb  4 20:03 redis_data
drwxr-xr-x  2 root      root       4.0K Nov  7  2019 ssl
drwxr-xr-x  4 root      root       4.0K Jun 12  2015 state
drwxr-xr-x  4      1000 www-data   4.0K Feb  4 20:03 tmp
drwxr-xr-x  4      1000 www-data   4.0K Jun 16  2017 uploads

新服务器:

/var/discourse/shared/standalone# ls -alh
total 60K
drwxr-xr-x 15 root       root   4.0K Feb  5 15:32 .
drwxr-xr-x  3 root       root   4.0K Feb  5 01:59 ..
drwxr-xr-x  3 root       root   4.0K Feb  5 01:59 backups
drwxr-xr-x  8 root       root   4.0K Feb  5 02:00 letsencrypt
drwxr-xr-x  4 root       root   4.0K Feb  5 01:59 log
drwxr-xr-x  2 root       root   4.0K Feb  5 01:59 postgres_backup
drwx------ 20 root       root   4.0K Feb  5 02:42 postgres_data
drwx------ 20 messagebus tss    4.0K Feb  5 15:33 postgres_data_new
drwx------ 20 root       root   4.0K Feb  5 02:04 postgres_data_older
drwxrwxr-x  8 messagebus tss    4.0K Feb  5 02:41 postgres_run
drwxr-xr-x  2 root       root   4.0K Feb  5 02:05 redis_data
drwxr-xr-x  2 root       root   4.0K Feb  5 02:05 ssl
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 state
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 tmp
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 uploads

因此,我在新服务器上修改了其中两个文件夹的所有权:

chown -R syslog: postgres_data
chown -R syslog: postgres_backup

这导致错误信息变成了我不知道该如何处理的内容:

Stopping PostgreSQL 15 database server: mainError: Config owner (postgres:101) and data owner (messagebus:102) do not match, and config owner is not root … failed!

为明确起见,我新服务器当前的权限如下:

/var/discourse/shared/standalone# ls -alh
total 60K
drwxr-xr-x 15 root       root   4.0K Feb  5 15:32 .
drwxr-xr-x  3 root       root   4.0K Feb  5 01:59 ..
drwxr-xr-x  3 root       root   4.0K Feb  5 01:59 backups
drwxr-xr-x  8 root       root   4.0K Feb  5 02:00 letsencrypt
drwxr-xr-x  4 root       root   4.0K Feb  5 01:59 log
drwxr-xr-x  2 syslog     syslog 4.0K Feb  5 01:59 postgres_backup
drwx------ 20 syslog     syslog 4.0K Feb  5 02:42 postgres_data
drwx------ 20 messagebus tss    4.0K Feb  5 15:33 postgres_data_new
drwx------ 20 root       root   4.0K Feb  5 02:04 postgres_data_older
drwxrwxr-x  8 messagebus tss    4.0K Feb  5 02:41 postgres_run
drwxr-xr-x  2 root       root   4.0K Feb  5 02:05 redis_data
drwxr-xr-x  2 root       root   4.0K Feb  5 02:05 ssl
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 state
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 tmp
drwxr-xr-x  4 root       root   4.0K Feb  5 02:04 uploads

:person_shrugging:
我猜可能需要某些 chown 命令来修复这个问题,但我不知道具体该怎么做、为什么以及怎么做……

由于我记录了原始(尽管不正确)的权限设置,我决定继续测试不同的 chown 变体。最终,以下这组命令帮我绕过了那个错误:

chown -R messagebus:tss /var/discourse/shared/standalone/postgres_backup
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_data
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_run
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_data_new
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_data_older

现在,当我运行 ./launcher rebuild app 时,又遇到了在 PostgreSQL 15 update - #61 by xFocus 中提到的相同错误:

源集群未干净关闭。
失败,正在退出

完整命令输出
x86_64 架构已检测到。
正在确保 launcher 为最新版本
Launcher 已是最新
2.0.20250129-0720: 正在从 discourse/base 拉取
摘要:sha256:01b8516e5504c0e9bc3707773015ff4407be03a89154194ff3b5b8699291bc26
状态:discourse/base:2.0.20250129-0720 镜像已是最新
docker.io/discourse/base:2.0.20250129-0720
/usr/local/lib/ruby/gems/3.3.0/gems/pups-1.2.1/lib/pups.rb
/usr/local/bin/pups --stdin
I, [2025-02-05T16:19:09.574812 #1]  INFO -- : 正在从 stdin 读取
I, [2025-02-05T16:19:09.605350 #1]  INFO -- : 文件 > /etc/service/postgres/run  chmod: +x  chown: 
I, [2025-02-05T16:19:09.614043 #1]  INFO -- : 文件 > /etc/service/postgres/log/run  chmod: +x  chown: 
I, [2025-02-05T16:19:09.626167 #1]  INFO -- : 文件 > /etc/runit/3.d/99-postgres  chmod: +x  chown: 
I, [2025-02-05T16:19:09.634599 #1]  INFO -- : 文件 > /root/install_postgres  chmod: +x  chown: 
I, [2025-02-05T16:19:09.643169 #1]  INFO -- : 文件 > /root/upgrade_postgres  chmod: +x  chown: 
I, [2025-02-05T16:19:09.644682 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 data_directory = '/var/lib/postgresql/15/main' 替换为 data_directory = '/shared/postgres_data'
I, [2025-02-05T16:19:09.646186 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?listen_addresses *=.*) 替换为 listen_addresses = '*'
I, [2025-02-05T16:19:09.653810 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?synchronous_commit *=.*) 替换为 synchronous_commit = $db_synchronous_commit
I, [2025-02-05T16:19:09.655422 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?shared_buffers *=.*) 替换为 shared_buffers = $db_shared_buffers
I, [2025-02-05T16:19:09.656864 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?work_mem *=.*) 替换为 work_mem = $db_work_mem
I, [2025-02-05T16:19:09.657789 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?default_text_search_config *=.*) 替换为 default_text_search_config = '$db_default_text_search_config'
I, [2025-02-05T16:19:09.659640 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?checkpoint_segments *=.*) 替换为 checkpoint_segments = $db_checkpoint_segments
I, [2025-02-05T16:19:09.660906 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?logging_collector *=.*) 替换为 logging_collector = $db_logging_collector
I, [2025-02-05T16:19:09.661925 #1]  INFO -- : 正在将 /etc/postgresql/15/main/postgresql.conf 中的 (?-mix:#?log_min_duration_statement *=.*) 替换为 log_min_duration_statement = $db_log_min_duration_statement
I, [2025-02-05T16:19:09.663114 #1]  INFO -- : 正在将 /etc/postgresql/15/main/pg_hba.conf 中的 (?-mix:^#local +replication +postgres +peer$) 替换为 local replication postgres  peer
I, [2025-02-05T16:19:09.663912 #1]  INFO -- : 正在将 /etc/postgresql/15/main/pg_hba.conf 中的 (?-mix:^host.*all.*all.*127.*$) 替换为 host all all 0.0.0.0/0 md5
I, [2025-02-05T16:19:09.664817 #1]  INFO -- : 正在将 /etc/postgresql/15/main/pg_hba.conf 中的 (?-mix:^host.*all.*all.*::1\/128.*$) 替换为 host all all ::/0 md5
I, [2025-02-05T16:19:09.665701 #1]  INFO -- : > if [ -f /root/install_postgres ]; then
  /root/install_postgres && rm -f /root/install_postgres
elif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
fi

initdb: 警告:为本地连接启用“信任”认证
initdb: 提示:下次运行 initdb 时,您可以通过编辑 pg_hba.conf 或使用选项 -A,或 --auth-local 和 --auth-host 来更改此设置。
W: https://dl.yarnpkg.com/debian/dists/stable/InRelease: 密钥存储在传统的 trusted.gpg 密钥环 (/etc/apt/trusted.gpg) 中,有关详细信息,请参阅 apt-key(8) 中的 DEPRECATION 部分。
debconf: 由于未安装 apt-utils,将延迟软件包配置。
I, [2025-02-05T16:19:29.797884 #1]  INFO -- : 正在生成区域设置(这可能需要一些时间)...
  en_US.UTF-8... 完成
生成完成。
正在将 PostgreSQL 从版本 13 升级到 15
属于此数据库系统的文件将由用户 "postgres" 拥有。
该用户还必须拥有服务器进程。

数据库集群将使用区域设置 "en_US.UTF-8" 进行初始化。
默认数据库编码已相应设置为 "UTF8"。
默认文本搜索配置将设置为 "english"。

数据页校验和已禁用。

正在修复现有目录 /shared/postgres_data_new 的权限 ... 完成
创建子目录 ... 完成
选择动态共享内存实现 ... posix
选择默认 max_connections ... 100
选择默认 shared_buffers ... 128MB
选择默认时区 ... Etc/UTC
创建配置文件 ... 完成
运行引导脚本 ... 完成
执行引导后初始化 ... 完成
将数据同步到磁盘 ... 完成


成功。您现在可以使用以下命令启动数据库服务器:

    /usr/lib/postgresql/15/bin/pg_ctl -D /shared/postgres_data_new -l logfile start

正在获取:1 http://deb.debian.org/debian bookworm-backports InRelease [59.0 kB]
正在获取:2 http://deb.debian.org/debian bookworm InRelease [151 kB]
正在获取:3 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
正在获取:4 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
正在获取:5 https://dl.yarnpkg.com/debian stable InRelease [17.1 kB]
正在获取:6 https://deb.nodesource.com/node_22.x nodistro InRelease [12.1 kB]
正在获取:7 http://deb.debian.org/debian bookworm-backports/main amd64 Packages [282 kB]
正在获取:8 http://deb.debian.org/debian bookworm/main amd64 Packages [8,792 kB]
正在获取:9 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.5 kB]
正在获取:10 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg InRelease [129 kB]
正在获取:11 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [243 kB]
正在获取:12 https://dl.yarnpkg.com/debian stable/main amd64 Packages [10.9 kB]
正在获取:13 https://dl.yarnpkg.com/debian stable/main all Packages [10.9 kB]
正在获取:14 https://deb.nodesource.com/node_22.x nodistro/main amd64 Packages [5,274 B]
正在获取:15 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 Packages [360 kB]
已获取 10.2 MB,耗时 3 秒 (3,942 kB/s)
正在读取软件包列表...
正在读取软件包列表...
正在构建依赖关系树...
正在读取状态信息...
将安装以下附加软件包:
  postgresql-client-13
建议安装:
  postgresql-doc-13
将安装以下新软件包:
  postgresql-13 postgresql-13-pgvector postgresql-client-13
0 个已升级,3 个新安装,0 个移除,0 个未升级。
需要下载 17.3 MB 的归档文件。
此操作后,将额外使用 56.7 MB 的磁盘空间。
正在获取:1 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-client-13 amd64 13.18-1.pgdg120+1 [1,523 kB]
正在获取:2 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-13 amd64 13.18-1.pgdg120+1 [15.4 MB]
正在获取:3 https://apt.postgresql.org/pub/repos/apt bookworm-pgdg/main amd64 postgresql-13-pgvector amd64 0.8.0-1.pgdg120+1 [297 kB]
已获取 17.3 MB,耗时 2 秒 (9,988 kB/s)
正在选择之前未选中的软件包 postgresql-client-13。
(正在读取数据库 ... 当前已安装 33363 个文件和目录。)
正在准备解包 .../postgresql-client-13_13.18-1.pgdg120+1_amd64.deb ...
正在解包 postgresql-client-13 (13.18-1.pgdg120+1) ...
正在选择之前未选中的软件包 postgresql-13。
正在准备解包 .../postgresql-13_13.18-1.pgdg120+1_amd64.deb ...
正在解包 postgresql-13 (13.18-1.pgdg120+1) ...
正在选择之前未选中的软件包 postgresql-13-pgvector。
正在准备解包 .../postgresql-13-pgvector_0.8.0-1.pgdg120+1_amd64.deb ...
正在解包 postgresql-13-pgvector (0.8.0-1.pgdg120+1) ...
正在设置 postgresql-client-13 (13.18-1.pgdg120+1) ...
正在设置 postgresql-13 (13.18-1.pgdg120+1) ...
正在创建新的 PostgreSQL 集群 13/main ...
/usr/lib/postgresql/13/bin/initdb -D /var/lib/postgresql/13/main --auth-local peer --auth-host md5
属于此数据库系统的文件将由用户 "postgres" 拥有。
该用户还必须拥有服务器进程。

数据库集群将使用区域设置 "C.UTF-8" 进行初始化。
默认数据库编码已相应设置为 "UTF8"。
默认文本搜索配置将设置为 "english"。

数据页校验和已禁用。

正在修复现有目录 /var/lib/postgresql/13/main 的权限 ... 完成
创建子目录 ... 完成
选择动态共享内存实现 ... posix
选择默认 max_connections ... 100
选择默认 shared_buffers ... 128MB
选择默认时区 ... Etc/UTC
创建配置文件 ... 完成
运行引导脚本 ... 完成
执行引导后初始化 ... 完成
将数据同步到磁盘 ... 完成

成功。您现在可以使用以下命令启动数据库服务器:

    pg_ctlcluster 13 main start

invoke-rc.d: 无法确定当前运行级别
invoke-rc.d: policy-rc.d 拒绝执行启动。
正在设置 postgresql-13-pgvector (0.8.0-1.pgdg120+1) ...
正在处理 postgresql-common (267.pgdg120+1) 的触发器 ...
正在从已安装的 myspell/hunspell 软件包构建 PostgreSQL 词典...
正在删除过时的词典文件:
正在停止 PostgreSQL 13 数据库服务器:main。
正在停止 PostgreSQL 15 数据库服务器:main。
执行一致性检查
-----------------------------
正在检查集群版本                                   完成

源集群未干净关闭。
失败,正在退出
-------------------------------------------------------------------------------------
POSTGRES 升级失败

请访问 https://meta.discourse.org/t/postgresql-15-update/349515 获取支持。
在此期间,您可以运行 ./launcher start app 来重启您的应用。
-------------------------------------------------------------------------------------



失败
--------------------
Pups::ExecError: if [ -f /root/install_postgres ]; then
  /root/install_postgres && rm -f /root/install_postgres
elif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then
  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1
fi
 执行失败,返回状态为 #<Process::Status: pid 18 exit 1>
失败位置:/usr/local/lib/ruby/gems/3.3.0/gems/pups-1.2.1/lib/pups/exec_command.rb:132:in `spawn'
执行失败,参数为 {"tag"=>"db", "cmd"=>"if [ -f /root/install_postgres ]; then\n  /root/install_postgres && rm -f /root/install_postgres\nelif [ -e /shared/postgres_run/.s.PGSQL.5432 ]; then\n  socat /dev/null UNIX-CONNECT:/shared/postgres_run/.s.PGSQL.5432 || exit 0 && echo postgres already running stop container ; exit 1\nfi\n"}
引导失败,退出代码为 1
** 引导失败 ** 请向上滚动并查找之前的错误消息,可能不止一条。
./discourse-doctor 可能有助于诊断问题。
e2940f8bb9e51f0f290e0b6cb78257d5fa5d7563542591405689531d1af8432b

在另一个帖子中,有人建议检查以下命令的输出:
tail /var/discourse/shared/standalone/log/var-log/postgres/current

2025-02-05 01:03:07.494 UTC [567] LOG:  收到智能关闭请求
2025-02-05 01:03:08.698 UTC [567] LOG:  后台工作进程 "logical replication launcher" (PID 593) 以退出代码 1 退出
2025-02-05 01:03:08.701 UTC [588] LOG:  正在关闭
2025-02-05 01:03:08.786 UTC [567] LOG:  数据库系统已关闭
2025-02-05 01:03:34.209 UTC [565] LOG:  正在启动 PostgreSQL 13.14 (Debian 13.14-1.pgdg110+2),运行在 x86_64-pc-linux-gnu 上,由 gcc (Debian 10.2.1-6) 10.2.1 20210110 编译,64 位
2025-02-05 01:03:34.211 UTC [565] LOG:  正在监听 IPv4 地址 "0.0.0.0",端口 5432
2025-02-05 01:03:34.211 UTC [565] LOG:  正在监听 IPv6 地址 "::",端口 5432
2025-02-05 01:03:34.222 UTC [565] LOG:  正在监听 Unix 套接字 "/var/run/postgresql/.s.PGSQL.5432"
2025-02-05 01:03:34.261 UTC [587] LOG:  数据库系统于 2025-02-05 01:03:08 UTC 关闭
2025-02-05 01:03:34.339 UTC [565] LOG:  数据库系统已准备好接受连接

还有提到停止进程等内容,但我目前没有任何正在运行的容器,所以这似乎不适用于我的情况。

我完全不知道接下来该怎么办。

我决定用这个过程再试一次:

rm -rf /var/discourse
rm -rf /var/lib/docker/volumes
docker stop app # 在旧服务器上 - 我没有意识到它还在运行
rsync -rvzgop root@IP:/var/discourse /var
rsync -rvz root@IP:/var/lib/docker/volumes/ /var/lib/docker/volumes
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_backup
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_data
chown -R messagebus:tss /var/discourse/shared/standalone/postgres_data_older
./launcher rebuild app

这_似乎_奏效了。它产生了以下输出,我现在必须对其进行解码:

Checking for extension updates                              notice

Your installation contains extensions that should be updated
with the ALTER EXTENSION command.  The file
    update_extensions.sql
when executed by psql by the database superuser will update
these extensions.


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
-------------------------------------------------------------------------------------
UPGRADE OF POSTGRES COMPLETE

Old 13 database is stored at /shared/postgres_data_old

To complete the upgrade, rebuild again using:

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

2aa0a17599100bb507a82e6b420826c135da1e06c9623408aa4579a53ece6cb2

啊,我只需要再运行一次 ./launcher rebuild app。关于 update_extensions.sql 等的其他输出似乎来自 PostgreSQL 本身,而不是 Discourse 的升级过程。

在第二次重建运行后,我选择使用 ./launcher cleanup 进行清理,然后使用以下命令进行 vacuum/reindex:

./launcher enter app
su postgres
psql
\connect discourse
REINDEX SCHEMA CONCURRENTLY public;
VACUUM VERBOSE ANALYZE;
\q
exit
exit

好吧,看起来你已经解决了,但其实更简单的方法是在旧服务器上备份,然后在新服务器上恢复数据库。这样你就不用经历 PostgreSQL 升级,也不需要处理权限问题。而且你也不需要去处理那些你可能没有在 PostgreSQL 15 更新 主题中阅读到的关于索引等方面的麻烦事。

我本来是打算那么做的,但站内升级完全搞砸了一切,以至于我不得不采取 rsync 的方式。
我没有保存站内升级 UI 中的错误输出,所以我不记得错误消息是什么了,但我认为它与论坛需要比 Ubuntu 20 更新的版本有关。
我的第一步是尝试就地执行 do-release-upgrade,但那也失败了——似乎是关于 Docker 使用 aufs 的问题……而那里的修复让我觉得数据不安全。

我一定会好好研究一下索引,谢谢你的链接……这个新网站太慢了 :sweat_smile: