• 技术文章 >数据库 >PostgreSQL

    postgresql 如何更新

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-25 16:34:40原创1915

    1、升级工具

    在程序的bin目录下,提供了很多的数据库工具,有一个pg_upgrade的工具就是专门用于数据库升级的。关于该工具可以使用帮助命令来查看具体的用法:

    [postgres@pgmaster pgdata]$ pg_upgrade --help
    pg_upgrade upgrades a PostgreSQL cluster to a different major version.
    Usage:
      pg_upgrade [OPTION]...
    Options:
      -b, --old-bindir=BINDIR       old cluster executable directory
      -B, --new-bindir=BINDIR       new cluster executable directory
      -c, --check                   check clusters only, don't change any data
      -d, --old-datadir=DATADIR     old cluster data directory
      -D, --new-datadir=DATADIR     new cluster data directory
      -j, --jobs                    number of simultaneous processes or threads to use
      -k, --link                    link instead of copying files to new cluster
      -o, --old-options=OPTIONS     old cluster options to pass to the server
      -O, --new-options=OPTIONS     new cluster options to pass to the server
      -p, --old-port=PORT           old cluster port number (default 50432)
      -P, --new-port=PORT           new cluster port number (default 50432)
      -r, --retain                  retain SQL and log files after success
      -U, --username=NAME           cluster superuser (default "postgres")
      -v, --verbose                 enable verbose internal logging
      -V, --version                 display version information, then exit
      -?, --help                    show this help, then exit
    Before running pg_upgrade you must:
      create a new database cluster (using the new version of initdb)
      shutdown the postmaster servicing the old cluster
      shutdown the postmaster servicing the new cluster
    When you run pg_upgrade, you must provide the following information:
      the data directory for the old cluster  (-d DATADIR)
      the data directory for the new cluster  (-D DATADIR)
      the "bin" directory for the old version (-b BINDIR)
      the "bin" directory for the new version (-B BINDIR)
    For example:
      pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin
    or
      $ export PGDATAOLD=oldCluster/data
      $ export PGDATANEW=newCluster/data
      $ export PGBINOLD=oldCluster/bin
      $ export PGBINNEW=newCluster/bin
      $ pg_upgrade
    Report bugs to <pgsql-bugs@postgresql.org>.

    帮助文件中,提到了使用pg_upgrade工具前,必须创建一个新的数据库,并且是已经初始化的,同时关闭原来的数据库和新的数据库。使用pg_upgrade时候,必须要加上前后版本的data目录和bin目录。

    2、升级过程

    首先确认的是,原来的数据库版本是pg9.6,数据目录在/data/pgdata。然后,安装完pg10.5后,不要初始化目录。

    将原来的9.6版本数据目录重命名为pgdata.old

    mv /data/pgdata /data/pgdata.old

    在/data/下创建一个pgdata目录,作为新版本的数据库数据目录,需要注意的是,这个目录权限是700,owner是postgres

    cd /data/
    mkdir pgdata
    chmod 700 pgdata
    chown -R postgres.postgres pgdata

    使用pg10.5的initdb初始化/data/pgdata目录

    initdb -D /data/pgdata

    进行升级check,注意后面加上-c,这一步只是检查不会实际执行升级。所有项都是ok即认为是可以升级。

    [postgres@pgmaster ~]$ pg_upgrade -b /usr/local/pgsql-9.6/bin -B /usr/local/pgsql/bin/ -d /data/pgdata.old/ 
    -D /data/pgdata -p 5432 -P 5432 -c
    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 mismatch       ok
    Checking for invalid "unknown" user columns                 ok
    Checking for hash indexes                                   ok
    Checking for presence of required libraries                 ok
    Checking database user is the install user                  ok
    Checking for prepared transactions                          ok
    *Clusters are compatible*

    执行升级。即在上一步去掉-c,需要注意的是这一步根据数据库的大小执行时间长短不一,执行完毕后会产生两个脚本analyze_new_cluster.sh和delete_old_cluster.sh,根据实际需要来进行执行,一般都会执行第一个脚本,第二个不建议执行,以防需要回滚升级,保留原来的数据目录比较保险。

    [postgres@pgmaster ~]$ pg_upgrade -b /usr/local/pgsql-9.6/bin -B /usr/local/pgsql/bin/ -d /data/pgdata.old/ 
    -D /data/pgdata -p 5432 -P 5432 
    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 mismatch       ok
    Checking for invalid "unknown" user columns                 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 analyze 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:
        ./analyze_new_cluster.sh
    Running this script will delete the old cluster's data files:
        ./delete_old_cluster.sh

    执行脚本前,需要先启动数据库pg_ctl -D /data/pgdata start

    [postgres@pgmaster ~]$ pg_ctl -D /data/pgdata start
    waiting for server to start....2019-10-08 17:18:51.402 CST [35827] LOG:  listening on IPv6 address 
    "::1", port 5432
    2019-10-08 17:18:51.402 CST [35827] LOG:  listening on IPv4 address "127.0.0.1", port 5432
    2019-10-08 17:18:51.408 CST [35827] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
    2019-10-08 17:18:51.437 CST [35828] LOG:  database system was shut down at 2019-10-08 17:16:11 CST
    2019-10-08 17:18:51.442 CST [35827] LOG:  database system is ready to accept connections
    done
    server started

    执行脚本./analyze_new_cluster.sh ,从运行日志来看,主要是创建统计信息

    [postgres@pgmaster ~]$ ./analyze_new_cluster.sh 
    This script will generate minimal optimizer statistics rapidly
    so your system is usable, and then gather statistics twice more
    with increasing accuracy.  When it is done, your system will
    have the default level of optimizer statistics.
    If you have used ALTER TABLE to modify the statistics target for
    any tables, you might want to remove them and restore them after
    running this script because they will delay fast statistics generation.
    If you would like default statistics as quickly as possible, cancel
    this script and run:
        "/usr/local/pgsql/bin/vacuumdb" --all --analyze-only
    vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
    vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
    vacuumdb: processing database "test": Generating minimal optimizer statistics (1 target)
    vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
    vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
    vacuumdb: processing database "test": Generating medium optimizer statistics (10 targets)
    vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
    vacuumdb: processing database "template1": Generating default (full) optimizer statistics
    vacuumdb: processing database "test": Generating default (full) optimizer statistics
    Done

    至此,查看version,发现已经由原来的9.6升级为10.5,升级结束。

    postgres=# select version();
                                          version
    ---------------------------------------------------------------------------------------------------------
     PostgreSQL 10.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
    (1 row)

    Python学习网,大量的免费PostgreSQL入门教程,欢迎在线学习!

    专题推荐:postgresql 更新
    上一篇:postgresql安装详细步骤 下一篇:qt如何使用postgresql

    相关文章推荐

    • 想安装PostgreSQL,看这篇也就够了• 一文了解什么是PostgreSQL• postgresql是什么• postgresql如何启动• postgresql无法启动服务怎么办

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网