1.查看原postgresql安装方法(默认在线安装)
数据库位置(/var/lib/postgresql/12/main )
脚本文件位置(/usr/lib/postgresql/12/bin)
配置文件位置(/etc/postgresql/12/main)
2.安装13版本库
(1)因为原12版本库是默认安装,所以新13版本库也使用默认在线安装
sudo apt-get update
sudo apt-get install postgresql-13 postgresql-server-dev-13
如果不能使用上面sudo apt-get 进行在线升级
请切换下载源或者查找正确的安装包名称.
(2)检查配置文件是否存在异常(通常不会出现问题)
diff /etc/postgresql/12/main/postgresql.conf /etc/postgresql/13/main/postgresql.conf
diff /etc/postgresql/12/main/pg_hba.conf /etc/postgresql/13/main/pg_hba.conf
3.开始升级
(1) 停止 PostgreSQL 服务。
sudo systemctl stop postgresql.service
(2)开始升级
sudo /usr/lib/postgresql/13/bin/pg_upgrade \
-d /var/lib/postgresql/12/main \
-D /var/lib/postgresql/13/main \
-b /usr/lib/postgresql/12/bin \
-B /usr/lib/postgresql/13/bin \
-o '-c config_file=/etc/postgresql/12/main/postgresql.conf' \
-O '-c config_file=/etc/postgresql/13/main/postgresql.conf'
如果出现错误,可以进入log文件查看,会有提示(一般多为权限问题)
(3)将端换为旧的和新的 PostgreSQL 版本。
sudo vim /etc/postgresql/13/main/postgresql.conf
# ...and change "port = 5433" to "port = 5432"
sudo vim /etc/postgresql/12/main/postgresql.conf
# ...and change "port = 5432" to "port = 5433"
(4)启动 PostgreSQL 服务。
sudo systemctl start postgresql.service
(5)检查新的PostgreSQL版本。
psql -c "SELECT version();"
(6) 运行生成的脚本。analyze_new_cluster
./analyze_new_cluster.sh
(7)检查安装了哪些旧的PostgreSQL软件包。
apt list --installed | grep postgresql
(8)删除旧的PostgreSQL软件包(从上面的列表中)。
sudo apt-get remove postgresql-12 postgresql-server-dev-12
(10)删除旧配置和集群。
sudo rm -rf /etc/postgresql/12/
./delete_old_cluster.sh
注意:本步骤同样适用postgresql 版本10升级至11