postgresql大版本升级

本文介绍了从 PostgreSQL 9.4.6 版本升级到 9.5.1 版本的过程。包括安装新版本、数据库初始化、使用 pg_upgrade 工具进行数据迁移等步骤。
  1. 原文地址:http://blog.youkuaiyun.com/yangzhawen/article/details/51006625
  2. 环境:  
  3. 当前版本:pg 9.4.6 安装路劲为:/data/pg946/  
  4. 目标版本:pg 9.5.1 安装路劲为:/data/pg951/  
  5.   
  6.   
  7. *****************************************************  
  8. 1.安装新版本  
  9. *****************************************************  
  10.   
  11.   
  12. /*******  
  13.   
  14. make --version  
  15.   
  16. gcc --version  
  17.   
  18. perl --version  
  19.   
  20. python --version  
  21.   
  22.   
  23.   
  24. ----1.安装环境包  
  25.   
  26. yum -y install wget gcc gcc-c++ readline-devel zlib-devel make   systemtap systemtap-sdt-devel \  
  27. perl perl-devel python python-devel tcl  tcl-devel    perl-ExtUtils-Embed \  
  28. sgml-common docbook stylesheets openjade  sgml-tools  xsltproc libxslt libxslt-devel \  
  29. libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel   pam pam-devel bison flex libreadline6-devel   
  30.   
  31.   
  32. ******/  
  33.   
  34. ----2.编译安装  
  35.   
  36.   
  37.   
  38. #useradd postgres && echo 'password' |passwd --stdin  postgres  
  39.   
  40.   
  41. #mkdir -p /data/pg951/data   && chown -R postgres /data/pg951/data  
  42.   
  43.   
  44. # wget https://ftp.postgresql.org/pub/source/v9.5.1/  
  45.   
  46. #mkdir -p /soft && cd /soft  
  47.   
  48. #rz  
  49.   
  50. # tar xf postgresql-9.5.1.tar.gz && cd postgresql-9.5.1  
  51.   
  52.   
  53.   
  54.   
  55. #./configure  
  56.   
  57.   
  58. ./configure --prefix=/data/pg951 \  
  59. --with-pgport=5435  \  
  60. --with-perl --with-python --with-tcl  \  
  61. --with-openssl  --without-ldap  \  
  62.  --with-libxml  --with-libxslt    \  
  63. --enable-thread-safety    \  
  64. --with-wal-blocksize=64   \  
  65. --with-blocksize=32 \  
  66. --with-wal-segsize=64 \  
  67. -enable-dtrace  \  
  68. --enable-debug  
  69.   
  70.   
  71. #make && make install  
  72.   
  73.   
  74. -----3.执行数据库初始化脚本(指定字符集)  
  75.   
  76. $/data/pg951/bin/initdb  -D /data/pg951/data  --encoding=utf8   -U postgres  
  77.   
  78.   
  79.   
  80. ---结果如下  
  81.   
  82. The files belonging to this database system will be owned by user "postgres".  
  83. This user must also own the server process.  
  84.   
  85. The database cluster will be initialized with locale "C".  
  86. The default database encoding has accordingly been set to "SQL_ASCII".  
  87. The default text search configuration will be set to "english".  
  88.   
  89. Data page checksums are disabled.  
  90.   
  91. fixing permissions on existing directory /data/pg951/data ... ok  
  92. creating subdirectories ... ok  
  93. selecting default max_connections ... 100  
  94. selecting default shared_buffers ... 128MB  
  95. selecting dynamic shared memory implementation ... posix  
  96. creating configuration files ... ok  
  97. creating template1 database in /data/pg951/data/base/1 ... ok  
  98. initializing pg_authid ... ok  
  99. initializing dependencies ... ok  
  100. creating system views ... ok  
  101. loading system objects' descriptions ... ok  
  102. creating collations ... ok  
  103. creating conversions ... ok  
  104. creating dictionaries ... ok  
  105. setting privileges on built-in objects ... ok  
  106. creating information schema ... ok  
  107. loading PL/pgSQL server-side language ... ok  
  108. vacuuming database template1 ... ok  
  109. copying template1 to template0 ... ok  
  110. copying template1 to postgres ... ok  
  111. syncing data to disk ... ok  
  112.   
  113. WARNING: enabling "trust" authentication for local connections  
  114. You can change this by editing pg_hba.conf or using the option -A, or  
  115. --auth-local and --auth-host, the next time you run initdb.  
  116.   
  117. Success. You can now start the database server using:  
  118.   
  119.     /data/pg951/bin/pg_ctl -D /data/pg951/data -l logfile start  
  120.   
  121.   
  122.   
  123. ----4.启动pg新版本  
  124.   
  125. $cp postgresql.conf  /data/pg951/data/  
  126.   
  127.   
  128.   
  129. $/data/pg951/bin/pg_ctl  -D /data/pg951/data  status  
  130.   
  131. $/data/pg951/bin/pg_ctl  -D /data/pg951/data  start   
  132.   
  133.   
  134.   
  135. $退出变更登录  
  136. exit  
  137.   
  138. *****************************************************  
  139. ---2.升级  
  140. *****************************************************  
  141.   
  142. -----1.将两个库都停止服务  
  143.   
  144.   
  145. $ netstat -lntp | grep postgres  
  146. tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      6287/postgres         
  147. tcp        0      0 127.0.0.1:5435              0.0.0.0:*                   LISTEN      6303/postgres     
  148.   
  149.   
  150. $/data/pg946/bin/pg_ctl   -D /data/pg946/data  stop   
  151.   
  152.   
  153. $/data/pg951/bin/pg_ctl  -D /data/pg951/data  stop    
  154.   
  155.   
  156.   
  157. $ /data/pg946/bin/pg_ctl  -D /data/pg946/data  status  
  158. pg_ctl: no server running  
  159.   
  160.   
  161. $ /data/pg951/bin/pg_ctl  -D /data/pg951/data  status  
  162. pg_ctl: no server running  
  163.   
  164.   
  165.   
  166.   
  167. ------2.执行pg_upgrade  
  168.   
  169.   
  170. #mkdir -p /data/upgrade   && chown -R postgres /data/upgrade    
  171.   
  172.   
  173.   
  174. ----2.1 进行pg_upgrade检查  
  175.   
  176. $cd /data/upgrade/  
  177.   
  178. $/data/pg951/bin/pg_upgrade   -c    -b /data/pg946/bin   \  
  179. -B /data/pg951/bin/ -d /data/pg946/data -D /data/pg951/data -p 5432 -P 5435  
  180.   
  181. Performing Consistency Checks  
  182. -----------------------------  
  183. Checking cluster versions                                   ok  
  184. Checking database user is the install user                  ok  
  185. Checking database connection settings                       ok  
  186. Checking for prepared transactions                          ok  
  187. Checking for reg* system OID user data types                ok  
  188. Checking for contrib/isn with bigint-passing mismatch       ok  
  189. Checking for presence of required libraries                 ok  
  190. Checking database user is the install user                  ok  
  191. Checking for prepared transactions                          ok  
  192.   
  193. *Clusters are compatible*  
  194.   
  195.   
  196.   
  197.   
  198.   
  199. ----2.2进行pg_upgrade升级  
  200.   
  201. 两种升级方式:  
  202. 1).缺省的通过拷贝数据文件到新的data目录下,拷贝的方式升级较慢,但是原库还可用;  
  203. 2).硬链接的方式升级较快,但是原库不可用.  
  204.   
  205.   
  206.   
  207. $/data/pg951/bin/pg_upgrade   -b /data/pg946/bin   \  
  208. -B /data/pg951/bin/ -d /data/pg946/data -D /data/pg951/data -p 5432 -P 5435  
  209.   
  210. ----执行结果  
  211. Performing Consistency Checks  
  212. -----------------------------  
  213. Checking cluster versions                                   ok  
  214. Checking database user is the install user                  ok  
  215. Checking database connection settings                       ok  
  216. Checking for prepared transactions                          ok  
  217. Checking for reg* system OID user data types                ok  
  218. Checking for contrib/isn with bigint-passing mismatch       ok  
  219. Creating dump of global objects                             ok  
  220. Creating dump of database schemas  
  221.                                                             ok  
  222. Checking for presence of required libraries                 ok  
  223. Checking database user is the install user                  ok  
  224. Checking for prepared transactions                          ok  
  225.   
  226. If pg_upgrade fails after this point, you must re-initdb the  
  227. new cluster before continuing.  
  228.   
  229. Performing Upgrade  
  230. ------------------  
  231. Analyzing all rows in the new cluster                       ok  
  232. Freezing all rows on the new cluster                        ok  
  233. Deleting files from new pg_clog                             ok  
  234. Copying old pg_clog to new server                           ok  
  235. Setting next transaction ID and epoch for new cluster       ok  
  236. Deleting files from new pg_multixact/offsets                ok  
  237. Copying old pg_multixact/offsets to new server              ok  
  238. Deleting files from new pg_multixact/members                ok  
  239. Copying old pg_multixact/members to new server              ok  
  240. Setting next multixact ID and offset for new cluster        ok  
  241. Resetting WAL archives                                      ok  
  242. Setting frozenxid and minmxid counters in new cluster       ok  
  243. Restoring global objects in the new cluster                 ok  
  244. Restoring database schemas in the new cluster  
  245.                                                             ok  
  246. Creating newly-required TOAST tables                        ok  
  247. Copying user relation files  
  248.                                                             ok  
  249. Setting next OID for new cluster                            ok  
  250. Sync data directory to disk                                 ok  
  251. Creating script to analyze new cluster                      ok  
  252. Creating script to delete old cluster                       ok  
  253.   
  254. Upgrade Complete  
  255. ----------------  
  256. Optimizer statistics are not transferred by pg_upgrade so,  
  257. once you start the new server, consider running:  
  258.     ./analyze_new_cluster.sh  
  259.   
  260. Running this script will delete the old cluster's data files:  
  261.     ./delete_old_cluster.sh  
  262.   
  263.   
  264.   
  265.   
  266. ---3. 修改新版本为正常端口号  
  267.   
  268. $ grep -i "^port" /data/pg951/data/postgresql.conf   
  269. port = 5435         # (change requires restart)  
  270.   
  271.   
  272. $ sed  -i "s/5435/5432/1" /data/pg951/data/postgresql.conf   
  273. port = 5432         # (change requires restart)  
  274.   
  275.   
  276. ----4.修改环境变量  
  277.   
  278. # su - postgres  
  279.   
  280. $  vi  ~/.bash_profile   
  281.   
  282.   
  283. # postgres  
  284. #PostgreSQL端口  
  285. PGPORT=5432  
  286.   
  287. #PostgreSQL数据目录  
  288. PGDATA=/data/pg951/data  
  289.   
  290. export PGPORT PGDATA   
  291.   
  292. #所使用的语言  
  293. export LANG=en_US.utf8  
  294.   
  295. #PostgreSQL 安装目录  
  296. export PGHOME=/data/pg951  
  297.   
  298. #PostgreSQL 连接库文件  
  299. export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH  
  300. export DATE=`date +"%Y%m%d%H%M"`  
  301.   
  302. #将PostgreSQL的命令行添加到 PATH 环境变量  
  303. export PATH=$PGHOME/bin:$PATH  
  304.   
  305. #PostgreSQL的 man 手册  
  306. export MANPATH=$PGHOME/share/man:$MANPATH  
  307.   
  308. #PostgreSQL的默认用户  
  309. export PGUSER=postgres  
  310.   
  311. #PostgreSQL默认主机地址  
  312. export PGHOST=127.0.0.1  
  313.   
  314. #默认的数据库名  
  315. export PGDATABASE=postgres  
  316.   
  317.   
  318.   
  319. #source ~/.bash_profile   
  320.   
  321.   
  322.   
  323.   
  324.   
  325. ----5.PostgreSQL执行脚本  
  326.   
  327. #复制PostgreSQL执行脚本  
  328. cp /soft/postgresql-9.5.1/contrib/start-scripts/linux  /etc/init.d/postgresql  
  329.   
  330. #增加执行权限  
  331. chmod +x /etc/init.d/postgresql  
  332.   
  333.   
  334. #编辑PostgreSQL执行脚本,确定以下参数或修改  
  335.   
  336. #vi /etc/init.d/postgresql  
  337.   
  338. # Installation prefix  
  339. prefix=/data/pg951  
  340.   
  341. # Data directory  
  342. PGDATA="/data/pg951/data"  
  343.   
  344. # Who to run the postmaster as, usually "postgres".  (NOT "root")  
  345. PGUSER=postgres  
  346.   
  347. Where to keep a log file  
  348. PGLOG="$PGDATA/serverlog"  
  349.   
  350.   
  351. ----6.启动新版本  
  352. /data/pg951/bin/pg_ctl  -D /data/pg951/data  start  
  353.   
  354.   
  355.   
  356. -----7.验证  
  357. $ /data/pg951/bin/psql  --version  
  358. psql (PostgreSQL) 9.5.1  
  359.   
  360.   
  361. $ /data/pg951/bin/psql   
  362. psql (9.5.1)  
  363. Type "help" for help.  
  364.   
  365. postgres@127.0.0.1 ~=# \l  
  366.                                  List of databases  
  367.    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges     
  368. -----------+----------+----------+------------+------------+-----------------------  
  369.  postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |   
  370.  template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +  
  371.            |          |          |            |            | postgres=CTc/postgres  
  372.  template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | postgres=CTc/postgres+  
  373.            |          |          |            |            | =c/postgres  
  374.  wind      | postgres | UTF8     | en_US.utf8 | en_US.utf8 |   
  375. (4 rows)  
  376.   
  377. postgres@127.0.0.1 ~=# \c wind  
  378. You are now connected to database "wind" as user "postgres".  
  379. postgres@127.0.0.1 wind=# \d  
  380.         List of relations  
  381.  Schema | Name | Type  |  Owner     
  382. --------+------+-------+----------  
  383.  public | t00  | table | postgres  
  384. (1 row)  
  385.   
  386. postgres@127.0.0.1 wind=# select count(*) from t00;  
  387.  count   
  388. -------  
  389.   1000  
  390. (1 row)  
  391.   
  392. Time: 2.308 ms  
  393. postgres@127.0.0.1 wind=#   
  394.   
  395.   
  396.   
  397. ---8.删除老版本软件  
  398.   
  399. $ cat delete_old_cluster.sh   
  400. #!/bin/sh  
  401.   
  402. rm -rf '/data/pg941/data'  
  403.   
  404. $ ./delete_old_cluster.sh   
### PostgreSQL 主从架构版本升级的方法和注意事项 #### 1. 准备工作 在进行 PostgreSQL版本升级之前,确保已经进行了充分的准备工作。这包括但不限于备份现有数据库、测试新版本的功能以及评估可能的风险。 对于主从架构而言,在执行任何操作前应停止所有写入活动并暂停应用服务器连接到数据库实例,以防止数据不一致[^1]。 #### 2. 数据库备份策略 采用物理备份的方式来进行整个集群的数据保护是非常重要的一步。可以通过 `pg_basebackup` 工具创建一个完整的文件系统级别的副本作为基础备份;也可以利用逻辑导出工具如 `pg_dumpall` 或者针对特定模式/表使用的 `pg_dump` 来获取SQL脚本形式的备份内容。 #### 3. 新环境搭建与同步 在一个独立于生产环境之外的新环境中安装目标版本PostgreSQL 软件包,并按照官方文档指导完成初始化设置过程。接着将先前准备好的备份恢复至新的主节点上,再通过流复制机制将其余备用节点加入形成一个新的主从关系链路结构[^4]。 #### 4. 测试验证阶段 一旦完成了上述步骤,则需要进行全面细致的功能性测试来确认迁移后的系统能否正常运作。特别是要注意检查那些依赖于特定版本特性的部分是否仍然有效,比如某些扩展模块的支持情况等。 #### 5. 切换上线流程 当一切就绪之后就可以规划正式切换的时间窗口了。通常建议选择业务低峰期实施此变更动作,减少对用户体验的影响程度。具体来说就是先切断旧版服务端口监听,随后启动新版程序实例接管对外请求流量。 #### 注意事项 - **兼容性考量**:不同版本之间可能存在语法差异或是废弃特性等问题,所以在实际部署前提早识别这些问题并通过调整应用程序代码或配置参数加以规避是很必要的。 - **第三方组件支持**:如果当前正在使用额外开发出来的插件或者其他辅助工具的话,务必提前咨询供应商获得关于跨版本移植方面的帮助和支持信息[^3]。 - **性能调优**:随着内核功能增强的同时也可能带来资源消耗上的变化,因此有必要重新审视现有的索引设计、查询语句效率等方面的表现以便及时作出相应改进措施. ```bash # 使用 pg_upgrade 进行原地升级 (仅适用于单机场景) pg_upgrade \ --old-bindir=/path/to/old/postgresql/bin \ --new-bindir=/path/to/new/postgresql/bin \ --old-datadir=/var/lib/postgresql/9.x/main \ --new-datadir=/var/lib/postgresql/10/main \ --link ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值