第一种方法, pg_dumpall方法
pg_dumpall 可以转储一个数据库集群里的所有数据库到一个脚本文件。该脚本文件包含可以用于作为 psql 输入的 SQL 命令,从而恢复数据库。
先下载并安装高版本数据库(下载高版本PG数据库的RPM包即可,在此忽略)
第一步:在新目录中初始化新版本集群,并更改端口号为目标端口号
mkdir -p /dbdir/postgresql/5433/data
/usr/pgsql-10/bin/initdb -D /dbdir/postgresql/5433/data/
vi /dbdir/postgresql/5433/data/postgresql.conf
port=5433
listen_addresses='*'
max_connections=1000 –合适的值
wal_level=logical –逻辑**
cat postgresql.conf |grep port
cat postgresql.conf |grep listen
第二步:切换到新集群目录,拷贝白名单文件
cd /dbdir/postgresql/5433/data
cp /dbdir/postgresql/5432/data/pg_hba.conf .
5432指的是老版本目录
第三步: 更改旧实例的default_transaction_read_only参数,防止老环境写入新数据
show default_transaction_read_only ;
alter system set default_transaction_read_only =on ;
select pg_reload_conf();
show default_transaction_read_only ;
第四步:pg_dumpall 旧实例上所有的库表和全局信息,最好先提前评估一下数据量,否则耗时较长
pg_dumpall -h /tmp/ -p 5432 -f 5432.dat
第五步:关闭旧实例,启动新版本集群后
/usr/pgsql-9.4/bin/pg_ctl -D /dbdir/postgresql/5432/data/ stop -m fast
/usr/pgsql-10/bin/pg_ctl -D /dbdir/postgresql/5433/data/ start
psql -h /tmp/ -p 5433 -f 5432.dat
第六步:备份旧实例data目录文件为一个zip包,删掉data目录,防止不小心启动该旧实例
zip -r data.zip data
rm -rf data
第二种方法,pg_upgrade方法
pg_upgrade允许存储在PostgreSQL数据文件中的数据升级到更高的PostgreSQL主版本,而不需要进行主版本升级所需的数据的dump/restore。
第一步:在新目录中初始化升级集群
/dbdir/postgresql/5433/data
/usr/pgsql-10/bin/initdb -D /dbdir/postgresql/5433/data/
第二步:切换到新集群目录,拷贝白名单文件
cd /dbdir/postgresql/5433/data
cp /dbdir/postgresql/5432/data/pg_hba.conf .
5432指的是老版本目录
第三步:使用pg_upgrade 进行升级检查,安装了插件的话升级的话可能会遇到一些问题
$ /usr/pgsql-10/bin/pg_upgrade -b /usr/pgsql-9.4/bin -B /usr/pgsql-10/bin -d /dbdir/postgresql/5432/data -D /dbdir/postgresql/5433/data -k -c
Performing Consistency Checks on Old Live Server
————————————————
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing miatch ok
Checking for invalid "unknown" user columns ok
Checking for hash indexes ok
Checking for roles starting with "pg_" ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
*Clusters are compatible*
$ /usr/pgsql-10/bin/pg_upgrade -b /usr/pgsql-9.4/bin -B /usr/pgsql-10/bin -d /dbdir/postgresql/5432/data -D /dbdir/postgresql/5433/data
There seems to be a postmaster servicing the old cluster.
Please shutdown that postmaster and try again.
Failure, exiting
安装插件后可能会遇到下面问题:
could not load library "$libdir/postgis-2.2": ERROR: could not access file "$libdir/postgis-2.2": No such file or directory
could not load library "$libdir/rtpostgis-2.2": ERROR: could not access file "$libdir/rtpostgis-2.2": No such file or directory
could not load library "$libdir/postgis_topology-2.2": ERROR: could not access file "$libdir/postgis_topology-2.2": No such file or directory
第四步:存在插件的可以先删除不必要的插件,升级检查通过后,关闭实例
$ /usr/pgsql-9.4/bin/pg_ctl -D /dbdir/postgresql/5432/data/ stop -m fast
waiting for server to shut down…. done
server stopped
第五步:pg_upgrade升级集群到新目录
$ /usr/pgsql-10/bin/pg_upgrade -b /usr/pgsql-9.4/bin -B /usr/pgsql-10/bin -d /dbdir/postgresql/5432/data -D /dbdir/postgresql/5433/data
Performing Consistency Checks
—————————–
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing miatch ok
Checking for invalid "unknown" user columns ok
Checking for roles starting with "pg_" ok
Creating dump of global objects ok
Creating dump of database schemas
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
——————
Analyzing all rows in the new cluster ok
Freezing all rows in the new cluster ok
Deleting files from new pg_xact ok
Copying old pg_clog to new server ok
Setting next transaction ID and epoch for new cluster ok
Deleting files from new pg_multixact/offsets ok
Copying old pg_multixact/offsets to new server ok
Deleting files from new pg_multixact/members ok
Copying old pg_multixact/members to new server ok
Setting next multixact ID and offset for new cluster ok
Resetting WAL archives ok
Setting frozenxid and minmxid counters in new cluster ok
Restoring global objects in the new cluster ok
Restoring database schemas in the new cluster
ok
Copying user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to *yze new cluster ok
Creating script to delete old cluster ok
Checking for hash indexes ok
Upgrade Complete
—————-
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./*yze_new_cluster.sh
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
第六步:启动实例,也可以做一些*yze等维护操作。
/usr/pgsql-10/bin/pg_ctl -D /dbdir/postgresql/5433/data start
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。