当从Heroku向Fly进行数据库传输时发生错误时,应采取以下措施解决

由于 Heroku 免费计划的取消,我正在将应用程序迁移到 Fly,但在数据库传输过程中出现了错误。

我将总结解决方法。

过渡步骤

由于Fly为从Heroku迁移进行了官方文档的准备,我们参考了该文档并进行了迁移。请访问以下链接了解详情:
https://fly.io/docs/rails/getting-started/migrate-from-heroku/

产生的错误

在执行数据库的pg_dump时,出现以下错误。

# pg_dump -Fc --no-acl --no-owner -d $HEROKU_DATABASE_URL | pg_restore --verbose --clean --no-acl --no-owner -d $DATABASE_URL

pg_dump: error: server version: 14.7 (Ubuntu 14.7-1.pgdg20.04+1); pg_dump version: 13.9 (Debian 13.9-0+deb11u1)
pg_dump: error: aborting because of server version mismatch
pg_restore: error: input file is too short (read 0, expected 5)

错误的原因 de

由于安装在 App 服务器上的 PostgreSQL 客户端的版本与在 Heroku 上运行的 PostgreSQL 服务器的版本不匹配。

Fly的App服务器安装的PostgreSQL客户端版本是13.9。

$ fly ssh console
# pg_dump --version
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
pg_dump (PostgreSQL) 13.9 (Debian 13.9-0+deb11u1)

Heroku运行的PostgreSQL服务器版本为14.7。

$ heroku pg:info
Plan:                  Mini
Status:                Available
Connections:           0/20
PG Version:            14.7
Created:               2019-02-19 13:56 UTC
Data Size:             14.9 MB/1.00 GB (In compliance)
Tables:                5
Rows:                  5776/10000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-parallel-80887

虽然与错误无直接关系,但正在运行的Fly PostgreSQL Server版本为15.2。有点复杂。。。

$ fly pg connect --app (FlyのApp名)-db
# SELECT version();
                                                           version

--------------------------------------------------------------------------------
---------------------------------------------
 PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by g
cc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)

应对方法

尽管本来理想的情况下应该采用全部相同的版本,但这次我们选择了忽略版本差异本身,以避免错误并进行数据库转移。

首先,备份Heroku的数据库。

$ heroku pg:backups:capture --app (HerokuのApp名)
$ heroku pg:backups:download --app (HerokuのApp名)

将下载的最新latest.dump文件传输到App服务器上。

$ flyctl sftp shell
# put latest.dump

一旦传输完成,进入App服务器的控制台并进行恢复操作。

$ fly ssh console
# pg_restore --verbose --clean --no-acl --no-owner -d $DATABASE_URL latest.dump

这样一来,数据库的传输就完成了。

广告
将在 10 秒后关闭
bannerAds