Postgresql 安装及升级

1. 安装新的 Yum 源,以及新版本的 Postgresql。

[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
[root@localhost ~]# yum install -y postgresql15-server

# Optionally initialize the database and enable automatic start:
[root@localhost ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
[root@localhost ~]# systemctl enable postgresql-15

2. 使用新版的 pg_upgrade 升级数据库文件

[root@localhost ~]# su - postgressu - postgres

-bash-4.2$ /usr/pgsql-15/bin/pg_upgrade --old-datadir /var/lib/pgsql/13/data --new-datadir /var/lib/pgsql/15/data --old-bindir /usr/pgsql-13/bin --new-bindir /usr/pgsql-15/bin

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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             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
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
  w 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_xact to new server                           ok
Setting oldest XID for new cluster                          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 delete old cluster                       ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/pgsql-15/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

3. 启动 postgresql

[root@localhost ~]# systemctl start postgresql-15

4. 对新的数据库执行清理

-bash-4.2$ /usr/pgsql-15/bin/vacuumdb --all --analyze-in-stages
vacuumdb: processing database "metabase": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "sonarqube": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "metabase": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "sonarqube": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "metabase": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "sonarqube": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

5. 复制 pg_hba.conf、postgresql.conf 文件至新的目录

[root@localhost ~]# cp -rp /var/lib/pgsql/13/data/pg_hba.conf /var/lib/pgsql/15/data
[root@localhost ~]# cp -rp /var/lib/pgsql/13/data/postgresql.conf /var/lib/pgsql/15/data

默认 postgresql 仅监听本地地址,并仅允许本地访问,所以需要修改 pg_hba.conf、postgresql.conf 两个文件的配置

  • pg_hba.conf 增加以下配置
host    all             all             0.0.0.0/0               scram-sha-256
  • postgresql.conf 修改以下两处配置
listen_addresses = '*'
max_connections = 500

6. 重启 postgresql 使新配置生效

[root@localhost ~]# systemctl restart postgresql-15

7. 参考:

猜你喜欢

转载自blog.csdn.net/xieshaohu/article/details/129944792