哈喽!大家好,我是【IT邦德】,江湖人称jeames007,10余年DBA及大数据工作经验
一位上进心十足的【大数据领域博主】!
中国DBA联盟(ACDU)成员,目前服务于工业互联网
擅长主流Oracle、MySQL、PG、高斯及Greenplum运维开发,备份恢复,安装迁移,性能优化、故障应急处理等。
✨ 如果有对【数据库】感兴趣的【小可爱】,欢迎关注【IT邦德】
❤️❤️❤️感谢各位大可爱小可爱!❤️❤️❤️
数据库升级分为两种,一种是小版本迭代升级,另一种是主板本升级。小版本升级很简单,只需要重启一下数据库即可。要在兼容的版本间升级,你只需要简单地在服务器关闭时替换可执行文件并重启服务器。 数据目录则保持不变–次要升级就这么简单。
对于PostgreSQL的主发行, 内部数据存储格式常被改变,这使升级复杂化。传统的把数据移动到新主版本的方法是先转储然后重新载入到数据库,不过这可能会很慢,所以使用 一种更快的方式pg_upgrade。
groupadd -g 60000 pg14
useradd -u 60000 -g pg14 pg14
echo “pg14” | passwd --stdin pg14
mkdir -p /pg96/{pgdata,archive,scripts,backup,postgresql,soft}
cp /opt/postgresql-9.6.24.tar.gz /pg96/soft/
chown -R pg96:pg96 /pg96
chmod -R 775 /pg96
yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel
yum install -y zlib-devel perl python36 tcl openssl ncurses-devel openldap pam
yum install -y zlib libicu
su - pg14
cd /pg14/soft
tar zxvf postgresql-14.9.tar.gz
cd postgresql-14.9
./configure --prefix=/pg14/postgresql --without-readline
make -j 4 && make install
chown -R pg96:pg96 /pg14
su - pg96
rm -rf /pg14/pgdata
/pg14/postgresql/bin/initdb -D /pg14/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
/pg14/postgresql/bin/pg_upgrade -c -k -b /pg96/postgresql/bin -B /pg14/postgresql/bin -d /pg96/pgdata -D /pg14/pgdata -p 5400 -P 5432
postgres=# create database testdb;
[pg96@centos79 ~]$ pgbench -i -s 10 -U postgres testdb
升级前需要关闭源库
[pg96@centos79 ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[pg96@centos79 ~]$ /pg14/postgresql/bin/pg_upgrade -k -b /pg96/postgresql/bin -B /pg14/postgresql/bin -d /pg96/pgdata -D /pg14/pgdata -p 5400 -P 5432
新版本的postgresql.conf和pg_hba.conf等配置文件匹配旧集簇参数
cat >> /pg14/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/pg14/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
cat > /pg14/pgdata/pg_hba.conf << EOF
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 md5
EOF
cat >> ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5432
export PGDATA=/pg14/pgdata
export PGHOME=/pg14/postgresql
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
EOF
source ~/.bash_profile
[pg96@centos79 ~]$ pg_ctl start
[pg96@centos79 ~]$ /pg14/postgresql/bin/vacuumdb --all --analyze-in-stages
[pg96@centos79 ~]$ psql
psql (14.9)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres=CTc/postgres+
| | | | | =c/postgres
testdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
(4 rows)
postgres=# \c testdb postgres
You are now connected to database "testdb" as user "postgres".
testdb=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------------+-------+----------
public | pgbench_accounts | table | postgres
public | pgbench_branches | table | postgres
public | pgbench_history | table | postgres
public | pgbench_tellers | table | postgres
(4 rows)
testdb=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 14.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)