postgres大版本升级

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
进行升级版本之前请一定做好备份!
查看当前版本:
[postgres@node1 ~]$ psql
psql (9.4.4)
Type "help" for help.
 
postgres=# select version();
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.4.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit
(1 row)
 
$PGDATA:
/opt/pg9.4.4//data
/opt/pg9.6.2//data
 
升级至9.6.2
安装新版本:
[root@node1 upload]# mkdir -p /opt/pg9.6.2//data
[root@node1 upload]# tar -zxvf postgresql-9.6.2.tar.gz
./configure --prefix=/opt/pg9.6.2/
  1. --with-pgport=5433  \  【建议在此处直接更改了端口】
  2. --with-perl --with-python --with-tcl  \  
  3. --with-openssl  --without-ldap  \  
  4.  --with-libxml  --with-libxslt    \  
  5. --enable-thread-safety    \  
  6. --with-wal-blocksize=64   \  
  7. --with-blocksize=32 \  
  8. --with-wal-segsize=64 \  
  9. -enable-dtrace  \  
  10. --enable-debug  
make
make install
[postgres@node1 ~]$ /opt/pg9.6.2/bin/initdb -D /opt/pg9.6.2/data/
--encoding=utf8   -U postgres  【可以指定字符集和用户】
如果安装的时候没有更改端口的话则此时应该:
vim postgresql.conf
port = 5433
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
 
[postgres@node1 data]$ /opt/pg9.6.2/bin/pg_ctl -D /opt/pg9.6.2/data/ start
 
[postgres@node1 data]$ /opt/pg9.6.2/bin/pg_ctl -D /opt/pg9.6.2/data/ status
pg_ctl: server is running (PID: 12720)
/opt/pg9.6.2/bin/postgres "-D" "/opt/pg9.6.2/data"
[postgres@node1 data]$ exit
logout
[root@node1 postgresql-9.6.2]# netstat -lntp | grep postgres
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 2271/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 12720/postgres
tcp 0 0 :::5432 :::* LISTEN 2271/postgres
tcp 0 0 ::1:5433 :::* LISTEN 12720/postgres
 
至此,新库安装启动成功
 
关闭两个库:
[postgres@node1 ~]$ /opt/pg9.6.2/bin/pg_ctl -D /opt/pg9.6.2/data/ stop
[postgres@node1 ~]$ /opt/pg9.4.4/bin/pg_ctl -D /opt/pg9.4.4/data/ stop
 
升级前的检测:
[postgres@node1 bin]$ /opt/pg9.6.2/bin/pg_upgrade -c --link -b /opt/pg9.4.4/bin -B /opt/pg9.6.2/bin -d /opt/pg9.4.4/data -D /opt/pg9.6.2/data/ -p 5432 -P 5433
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* system OID user data types ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for roles starting with 'pg_' ok
Checking for presence of required libraries fatal
 
Your installation references loadable libraries that are missing from the
new installation. You can add these libraries to the new installation,
or remove the functions using them from the old installation. A list of
problem libraries is in the file:
loadable_libraries.txt
 
Failure, exiting
 
备注: -b, -B 分别表示老版本 PG bin 目录,新版本 PG bin目录, -d, -D 分别表示老版本PG 数据目录,新版本 PG 数据目录, -c 表示仅检查,并不会做任何更改, 根据提示查看文件 loadable_libraries.txt 。
 
cat /opt/pg9.4.4/bin/loadable_libraries.txt
could not load library "$libdir/dblink":
ERROR: could not access file "$libdir/dblink": No such file or directory
 
could not load library "$libdir/postgres_fdw":
ERROR: could not access file "$libdir/postgres_fdw": No such file or directory
 
could not load library "$libdir/slony1_funcs":
ERROR: could not access file "$libdir/slony1_funcs": No such file or directory
 
检测发现:新库缺少一些旧库已经安装的工具和软件
 
解决方案:
cd /upload/postgresql-9.6.2/contrib/
make
make install postgres_fdw
make install dblink
【这两个插件9.6.2也有,且是常用的小型插件,可以直接在9.6.2环境中安装】
 
此时node1和node2的test01和test02是可以同步的,即slony-i是可以正常使用的。
思路:可以卸载slony-i 来实现升级postgresql或者同时升级slony-i来升级postgresql。
本博客使用卸载升级:(博主认为像slony-i这种工具由于对不同版本的postgres数据库有着不同的版本要求,升级slony-i更容易出问题,不如直接删除然后在新的数据库重新安装同步)
① 关闭守护进程:
slon_kill 1
slon_kill 2
② 卸载节点:
slonik_uninstall_nodes | slonik
③查看安装模式:
master=# \dn
List of schemas
Name | Owner
--------------+----------
_replication | slony
public | postgres
(2 rows)
 
④删除Slony安装的模式:【此处应确认slon运行守护程序已经关闭】
master=# DROP SCHEMA _replication CASCADE;
slave=# DROP SCHEMA _replication CASCADE;
 
[postgres@node1 ~]$ slonslony_show_configuration
-bash: slonslony_show_configuration: command not found
【检测一下配置,发现已经完全卸载干净了,需要注意的是slon-i并不是删除所有有关目录和文件即可删除!】
 
重新检测:
[postgres@node1 ~]$ /opt/pg9.6.2/bin/pg_upgrade -c --link -b /opt/pg9.4.4/bin -B /opt/pg9.6.2/bin -d /opt/pg9.4.4/data -D /opt/pg9.6.2/data/ -p 5432 -P 5433
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* system OID user data types ok
Checking for contrib/isn with bigint-passing mismatch 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*
 
 
两种升级方式:
1).缺省的通过拷贝数据文件到新的data目录下,拷贝的方式升级较慢,但是原库还可用;
2).硬链接的方式升级较快,但是原库不可用.
一般来说是建议使用硬链接的方式来升级的,这样更符合实际生产
但是需要注意无论是哪种方式,升级要停库且升级时间不可控,这也是升级的弊端
升级:
[postgres@node1 ~]$ /opt/pg9.6.2/bin/pg_upgrade --link -b /opt/pg9.4.4/bin -B /opt/pg9.6.2/bin -d /opt/pg9.4.4/data -D /opt/pg9.6.2/data/ -p 5432 -P 5433
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* system OID user data types ok
Checking for contrib/isn with bigint-passing mismatch 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 on the new cluster ok
Deleting files from new pg_clog 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
*failure*
Consult the last few lines of "pg_upgrade_utility.log" for
the probable cause of the failure.
Failure, exiting
提示:恢复新集群中的全局变量失败,到该日志下查看并没有发现有用的信息。猜测是因为心集群无法正常启动。
 
[postgres@node1 pg9.6.2]$ /opt/pg9.6.2/bin/initdb -D /opt/pg9.6.2/data/
根据前面的提示重新初始化了新的数据库,但是还是没有任何改变。
[postgres@node1 ~]$ /opt/pg9.6.2/bin/psql -p5433
/opt/pg9.6.2/bin/psql: symbol lookup error: /opt/pg9.6.2/bin/psql: undefined symbol: PQsetErrorContextVisibility
果然,问题出现在了新集群的启动上,出现该问题的原因是目前状态的lib仍是指定到了旧的数据库,临时定义LD_LIBRARY_PATH到新数据库
[postgres@node1 pg9.6.2]$ export LD_LIBRARY_PATH=/opt/pg9.6.2/lib:$HOME/lib
[postgres@node1 pg9.6.2]$ /opt/pg9.6.2/bin/psql -p5433
psql (9.6.2)
Type "help" for help.
 
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
 
postgres=# \q
[postgres@node1 pg9.6.2]$ pg_ctl start
server starting
[postgres@node1 pg9.6.2]$ LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
 
[postgres@node1 pg9.6.2]$ psql
psql (9.4.4)
Type "help" for help.
 
postgres=#
postgres=# \q
 
可以发现此时新旧数据库都可以登入
关闭服务:
[postgres@node1 pg9.6.2]$ pg_ctl stop
[postgres@node1 pg9.6.2]$ /opt/pg9.6.2/bin/pg_ctl -D /opt/pg9.6.2/data/ stop
重新升级:
[postgres@node1 pg9.6.2]$ /opt/pg9.6.2/bin/pg_upgrade --link -b /opt/pg9.4.4/bin -B /opt/pg9.6.2/bin -d /opt/pg9.4.4/data -D /opt/pg9.6.2/data/ -p 5432 -P 5433
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* system OID user data types ok
Checking for contrib/isn with bigint-passing mismatch 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 on the new cluster ok
Deleting files from new pg_clog 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
Adding ".old" suffix to old global/pg_control ok
 
If you want to start the old cluster, you will need to remove
the ".old" suffix from /opt/pg9.4.4/data/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.
 
Linking 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
 
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
 
查看新数据库端口:
[postgres@node1 pg9.6.2]$ grep -i "^port" /opt/pg9.6.2/data/postgresql.conf
port = 5433 # (change requires restart)
修改端口号为5432
【sed -i直接修改读取的文件内容,而不是输出到终端。详情请参考博主另一篇博客http://blog.csdn.net/oraclesand/article/details/68923042】
[postgres@node1 pg9.6.2]$ sed -i "s/5433/5432/1" /opt/pg9.6.2/data/postgresql.conf
[postgres@node1 pg9.6.2]$ grep -i "^port" /opt/pg9.6.2/data/postgresql.conf
port = 5432 # (change requires restart)
 
修改环境变量:
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
# User specific environment and startup programs
 
PATH=$PATH:$HOME/bin
 
export PATH
#export PGHOME=/opt/pg9.4.4/
export PGHOME=/opt/pg9.6.2/
export PGDATA=$PGHOME/data
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/lib
 
#默认端口
#export PGPORT=5432
#默认密码
#export PGPASSWORD=''
#默认字符集
#export LANG=en_US.utf8
#PostgreSQL默认主机地址
#export PGHOST=127.0.0.1
#默认的数据库名
#export PGDATABASE=postgres
#PostgreSQL的 man 手册
#export MANPATH=$PGHOME/share/man:$MANPATH
#PostgreSQL 连接库文件
#export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
#export DATE=`date +"%Y%m%d%H%M"`
 
source ~/.bash_profile
 
检测升级效果,查看新库是否有原有数据:
[postgres@node1 pg9.6.2]$ pg_ctl start
server starting
[postgres@node1 pg9.6.2]$ LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
 
[postgres@node1 pg9.6.2]$ psql
psql (9.6.2)
Type "help" for help.
 
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
master | slony | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+
| | | | | =c/postgres
(4 rows)
 
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------------+---------------+----------
public | dblink_test00_view | view | postgres
public | qqq | foreign table | postgres
public | test01 | table | postgres
(3 rows)
一切正常
 
 
执行脚本
[postgres@node1 pg9.6.2]$ ./analyze_new_cluster.sh
这个脚本其实就一条 vacuumdb 命令,收集新库统计信息
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:
"/opt/pg9.6.2/bin/vacuumdb" --all --analyze-only
 
vacuumdb: processing database "master": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "master": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "master": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics
 
删除旧数据库data:
[postgres@node1 pg9.6.2]$ cat delete_old_cluster.sh
#!/bin/sh
 
rm -rf '/opt/pg9.4.4/data'
[postgres@node1 pg9.6.2]$ ./delete_old_cluster.sh
[postgres@node1 opt]$ ls pg9.4.4/ -l
total 20
drwxr-xr-x. 2 postgres postgres 4096 Mar 30 17:59 bin
drwxr-xr-x. 4 postgres postgres 4096 Mar 13 12:56 include
drwxr-xr-x. 4 postgres postgres 4096 Mar 30 14:22 lib
drwxr-xr-x. 4 postgres postgres 4096 Mar 28 16:54 share
drwxrwxr-x. 3 postgres postgres 4096 Mar 30 14:46 slonylog
 
[postgres@node1 opt]$ postgres --version
postgres (PostgreSQL) 9.6.2
至此升级完成!
根据需求实际修改postgresql.conf, pg_hba.conf等文件。
 
官方参考文档:
http://slony.info/documentation/1.2/dropthings.html
一个有趣的相同问题:
http://www.postgresql-archive.org/Slony-error-please-help-td5908267.html#a5908317

 

 

 

转自:https://blog.csdn.net/Oraclesand/article/details/68923078

你可能感兴趣的:(数据库,postgres,大版本,升级)