概述
瀚高数据库
目录
环境
文档用途
详细信息
环境
系统平台:Linux x86-64 Red Hat Enterprise Linux 7
版本:14
文档用途
本文以pg14.2升级至pg14.6为例,向各位读者介绍pg小版本升级的方法及步骤。
详细信息
一、背景介绍
当前PostgreSQL版本号由主要版本号和小版本号组成。例如,在版本号10.1中,10是主要版本号,1是小要版本号,这意味着这将是主要版本10的第一个小版本。对于PostgreSQL版本10.0之前的版本,版本号由三个数字组成,例如9.5.3。在这些情况下,主要版本由版本号的前两位数组组成,例如9.5,而小版本是第三个数字,例如3,这意味着这将是主要版本9.5的第三个小版本。
小版本不会更改内部存储格式,并且始终与相同主要版本号的早期和后续小版本兼容。例如,版本10.1与版本10.0和版本10.6兼容。同样的9.5.3与9.5.0、9.5.1和9.5.6兼容。要在兼容版本之间进行更新,只需在数据库服务关闭时更换可执行文件并重新启动服务器即可。数据目录保持不变-小版本升级就这么简单。
以上信息为pg社区官方说明,但在实际的生产环境中,升级前需要详细的阅读各版本的release说明,比如在pg10.19升级至pg10.20的版本介绍中,为了解决一项inde-only扫描的bug,在升级完成后需要s手动重建btree索引。
二、升级步骤归纳
根据PG社区提供的说明,pg小版本升级只需在停止数据库服务后覆盖安装bin目录下的可执行命令即可,但在实际的操作过程中,推荐读者首先阅读个版本的升级说明,看是否有一些升级后续的操作事宜。pg小版本升级在具体的生产环境中的过程主要分为以下两种场景:
场景一、数据库的安装目录、数据目录未包含版本号,如/usr/local/pgsql/bin /usr/local/pgsql/data
在这种场景下,只需要规划新的安装路径,编译安装好新版本数据库,停止旧版本数据库服务后,将新版本数据库的bin目录内的文件copy到/usr/local/pgsql/bin下覆盖,启动数据库即可。如果设置了pg的开机自启服务,pg的开机自启服务文件也不需要修改。
场景二、数据库的安装目录、数据目录均包含版本号,如/usr/local/pg14.2/bin /usr/local/pg14.2/data
在这种情景下,编译安装新版本数据库,停止旧版本数据库,将旧版本数据库的数据目录移动到新装的数据库的对应路径,启动该数据库即可。此种场景下需要修改环境变量信息,另外如果设置了pg的开机自启服务,还需要修改对应的service文件,防止开机自启服务失效。
三、小版本升级示例
场景一过程简单,下面以第二种场景作为示例给读者演示下pg14.2升级到pg14.6的升级过程
1、平台信息
pg版本 pg14.2 pg14.6
OS版本:Redhat 7.7
CPU型号:X86_64
2、升级前环境变量信息
[postgres@mode2 ~]$ cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PATH=/data/pg14.2/bin:/usr/bin:/usr/sbin:/bin:/sbin:$PATH
export PGHOME=/data/pg14.2
export PGDATA=$PGHOME/data
export PGPORT=5432
export PGUSER=postgres
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/lib/:$LD_LIBRARY_PATH
3、pg14.2数据库明细
[postgres@mode2 ~]$ psql
psql (14.2)
Type "help" for help.
postgres=# l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
a | a | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 189 MB | pg_default |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 55 MB | pg_default | default administrative connection database
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +| 8433 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | postgres=CTc/postgres+| 8561 kB | pg_default | default template for new databases
| | | | | =c/postgres | | |
(4 rows)
postgres=# dx
List of installed extensions
Name | Version | Schema | Description
--------------------+---------+------------+--------------------------------------------------------------
dblink | 1.2 | public | connect to other PostgreSQL databases from within a database
pg_profile | 0.3.6 | public | PostgreSQL load profile repository and report builder
pg_stat_statements | 1.9 | public | track execution statistics of all SQL statements executed
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(4 rows)
postgres=# c a a
You are now connected to database "a" as user "a".
a=> dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+-------------------------------------------------------------------
pg_trgm | 1.6 | public | text similarity measurement and index searching based on trigrams
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
a=> dt+
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
--------+------------------+-------+-------+-------------+---------------+--------+-------------
public | ch | table | a | permanent | heap | 79 MB |
public | pgbench_accounts | table | a | permanent | heap | 15 MB |
public | pgbench_branches | table | a | permanent | heap | 288 kB |
public | pgbench_history | table | a | permanent | heap | 12 MB |
public | pgbench_tellers | table | a | permanent | heap | 416 kB |
public | t | table | a | permanent | heap | 472 kB |
public | t1 | table | a | permanent | heap | 448 kB |
public | t2 | table | a | permanent | heap | 448 kB |
(8 rows)
a=> select count(*) from ch ;
count
---------
1210000
(1 row)
a=> select * from ch limit 1;
id | hanzi | num
----+----------------------+-----
54 | 鵶獷跌緗鉄燴聀鋧扴鄨 | 1
(1 row)
a=> select * from ch where num=1;
id | hanzi | num
----+----------------------+-----
54 | 鵶獷跌緗鉄燴聀鋧扴鄨 | 1
56 | 扝噲蹘傝膷倱蕠鷦前哆 | 1
10 | 輨墦嬮牛鞑鶓欄鍷捻奉 | 1
91 | 刂唹錒浣酂锵透热壾氃 | 1
(4 rows)
4、解压缩安装pg14.6,修改安装文件属主
[root@mode2 download]# tar -zxvf postgresql-14.6.tar.gz
[root@mode2 download]# cd postgresql-14.6/
##编译安装过程略
[root@mode2 postgresql-14.6]# ./configure --prefix=/data/pg14.6 --with-perl --with-python --with-openssl --with-readline --with-libxml --with-libxslt
[root@mode2 postgresql-14.6]# make world -j 4 && make install-world -j 4
##修改安装完成后的文件属主为postgres
[root@mode2 postgresql-14.6]# chown -R postgres:postgres /data/pg14.6
[root@mode2 postgresql-14.6]# ll /data/pg14.6
总用量 12
drwxr-xr-x 2 postgres postgres 4096 12月 7 17:18 bin
drwxr-xr-x 4 postgres postgres 4096 12月 7 17:18 include
drwxr-xr-x 4 postgres postgres 4096 12月 7 17:18 lib
drwxr-xr-x 5 postgres postgres 46 12月 7 17:18 share
5、停止pg14.2数据库
[postgres@mode2 ~]$ pg_ctl stop
waiting for server to shut down....2022-12-07 17:27:21.298 CST [13677] LOG: received fast shutdown request
2022-12-07 17:27:21.298 CST [13677] LOG: aborting any active transactions
2022-12-07 17:27:21.299 CST [13677] LOG: background worker "logical replication launcher" (PID 13684) exited with exit code 1
2022-12-07 17:27:21.299 CST [13679] LOG: shutting down
2022-12-07 17:27:21.306 CST [13677] LOG: database system is shut down
done
server stopped
6、拷贝data目录到新版本数据库数据目录下
[postgres@mode2 pg14.2]$ cp -rf data /data/pg14.6/
[postgres@mode2 pg14.2]$ vi ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PATH=/data/pg14.6/bin:/usr/bin:/usr/sbin:/bin:/sbin:$PATH
export PGHOME=/data/pg14.6
export PGDATA=$PGHOME/data
export PGPORT=5432
export PGUSER=postgres
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/lib/:$LD_LIBRARY_PATH
[postgres@mode2 pg14.2]$ source ~/.bash_profile
7、核对版本信息、数据库数据
[postgres@mode2 pg14.2]$ pg_ctl start
waiting for server to start....2022-12-07 18:12:55.516 CST [14453] LOG: starting PostgreSQL 14.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2022-12-07 18:12:55.516 CST [14453] LOG: listening on IPv6 address "::1", port 5432
2022-12-07 18:12:55.516 CST [14453] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-12-07 18:12:55.518 CST [14453] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-12-07 18:12:55.541 CST [14454] LOG: database system was shut down at 2022-12-07 18:10:47 CST
2022-12-07 18:12:55.545 CST [14453] LOG: database system is ready to accept connections
done
server started
[postgres@mode2 ~]$ psql
psql (14.6)
Type "help" for help.
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 14.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
postgres=# l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
a | a | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 190 MB | pg_default |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 55 MB | pg_default | default administrative connection database
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +| 8433 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | postgres=CTc/postgres+| 8561 kB | pg_default | default template for new databases
| | | | | =c/postgres | | |
(4 rows)
postgres=# c a a
You are now connected to database "a" as user "a".
a=> select * from ch where num=1;
id | hanzi | num
----+----------------------+-----
54 | 鵶獷跌緗鉄燴聀鋧扴鄨 | 1
56 | 扝噲蹘傝膷倱蕠鷦前哆 | 1
91 | 刂唹錒浣酂锵透热壾氃 | 1
10 | 輨墦嬮牛鞑鶓欄鍷捻奉 | 1
(4 rows)
a=> dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+-------------------------------------------------------------------
pg_trgm | 1.6 | public | text similarity measurement and index searching based on trigrams
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
a=> c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# dx
List of installed extensions
Name | Version | Schema | Description
--------------------+---------+------------+--------------------------------------------------------------
dblink | 1.2 | public | connect to other PostgreSQL databases from within a database
pg_profile | 0.3.6 | public | PostgreSQL load profile repository and report builder
pg_stat_statements | 1.9 | public | track execution statistics of all SQL statements executed
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(4 rows)
数据、插件等对象核对无误,数据库版本升级完成,若设置了开机自启,再修改对数据库服务启动文件即可,本文不在赘述。
最后
以上就是耍酷手机为你收集整理的pg如何进行小版本升级的全部内容,希望文章能够帮你解决pg如何进行小版本升级所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复