PostgreSQL安装及Streaming Replication配置详解

数据库 其他数据库 PostgreSQL
本文我们主要介绍了PostgreSQL安装及Streaming Replication配置方法,希望本次的介绍能够对您有所帮助。

PostgreSQL安装及Streaming Replication配置是本文我们主要要介绍的内容,因为项目需要搭建postgres环境,并要求具有一定的可靠性。所以笔者在搭建这个环境的同时把步骤及命令记录下来的。笔者是DB2 DBA.但现在项目准备从DB2迁移到postgresql. postgresql笔者也是刚刚接触.笔者以后会把学到的关于postgresql的知识,以及DB2迁移postgresql过程中遇到的问题及经验总结出来,陆续整理成文档.,然后和有同样需求的朋友进行交流,希望能够对您有所帮助。

  1. -------------------------------------------------------  
  2. >>>>>>>>>INSTALL<<<<<<<<<<<<< 
  3. --primary 10.4.5.94  
  4. --standby 10.4.5.93  
  5. --standby 10.4.5.91  
  6.  
  7. psql (PostgreSQL) 9.0.4  
  8. -------------------------------------------------------  
  9. cd /root/postgresql-9.0.4  
  10. ./configure --with-wal-segsize=32 --with-wal-blocksize=16 
  11. gmake  
  12. gmake install  
  13. adduser postgres  
  14. mkdir -p /usr/local/pgsql/data  
  15. mkdir -p /usr/local/pgsql/etc  
  16. chown postgres /usr/local/pgsql/data  
  17. chown postgres /usr/local/pgsql/etc  
  18. chown postgres /pg_data_logs  
  19. cd /pg_data_logs/  
  20. mkdir pg_xlog  
  21. chown postgres pg_xlog/  
  22. su - postgres  
  23. /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data --xlogdir=/pg_data_logs/pg_xlog  
  24. mv /usr/local/pgsql/data/*.conf /usr/local/pgsql/etc  
  25. exit (su - root)  
  26. cp /root/postgresql-9.0.4/contrib/start-scripts/linux /etc/init.d/postgresd  
  27. vi /etc/init.d/postgresd  修改如下部分,用-c config_file指定postgresql.conf的位置: 
  28. ===============================================================  
  29.   start)  
  30.         echo -n "Starting PostgreSQL: "  
  31.         test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj  
  32.         su - $PGUSER -c "$DAEMON -D '$PGDATA' -c config_file=/usr/local/pgsql/etc/postgresql.conf &" >>$PGLOG 2>&1  
  33.         echo "ok"  
  34.         ;;  
  35.   restart)  
  36.         echo -n "Restarting PostgreSQL: "  
  37.         su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"  
  38.         test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj  
  39.         su - $PGUSER -c "$DAEMON -D '$PGDATA' -c config_file=/usr/local/pgsql/etc/postgresql.conf &" >>$PGLOG 2>&1  
  40.         echo "ok"  
  41.         ;;  
  42. ===============================================================  
  43.  
  44. vi /usr/local/pgsql/etc/postgresql.conf  修改如下部分: 
  45. ===============================================================  
  46. #------------------------------------------------------------------------------  
  47. # FILE LOCATIONS  
  48. #------------------------------------------------------------------------------  
  49.  
  50. # The default values of these variables are driven from the -D command-line  
  51. # option or PGDATA environment variable, represented here as ConfigDir.  
  52.  
  53. #data_directory = 'ConfigDir'           # use data in another directory  
  54.                                         # (change requires restart)  
  55. hba_file = '/usr/local/pgsql/etc/pg_hba.conf'     # host-based authentication file  
  56.                                         # (change requires restart)  
  57. ident_file = '/usr/local/pgsql/etc/pg_ident.conf' # ident configuration file  
  58.                                         # (change requires restart)  
  59.  
  60. # If external_pid_file is not explicitly set, no extra PID file is written.  
  61. #external_pid_file = '(none)'           # write an extra PID file  
  62.                                         # (change requires restart)  
  63. ===============================================================  
  64.  
  65. /etc/init.d/postgresd start  
  66.  
  67. ------------------------------------------------------- 
  1. >>>>>>>>>Streaming Replication<<<<<<<<<<<<< 
  2. ------------------------------------------------------- 
  3. --IN ALL SERVER:  
  4. 修改访问控制  
  5. vi /usr/local/pgsql/etc/pg_hba.conf  
  6. ***加一行  
  7. host all all 10.4.5.0/24 password  
  8. host all all 10.4.2.0/24 password  
  9. 修改监听范围  
  10. vi /usr/local/pgsql/etc/postgresql.conf  
  11. 修改listen_addresses = ‘localhost’为listen_addresses = ‘*’,如果前面有#号则需要删除#号  
  12. 重启  
  13. /etc/init.d/postgresd restart  
  14. --IN PRIMARY SERVER:  
  15. 设置同步账号  
  16. psql  
  17. create user repl superuser login password 'meiyoumima';  
  18. 修改访问控制  
  19. vi /usr/local/pgsql/etc/pg_hba.conf  
  20. ***添加以下内容  
  21. host replication repl 10.4.5.93/32 password  
  22. host replication repl 10.4.5.91/32 password 

 

修改postgresql服务配置文件

 

  1. vi /usr/local/pgsql/etc/postgresql.conf  
  2. ####Add by paolo for replications  
  3. wal_level = hot_standby 
  4. archive_mode = on 
  5. archive_command = 'cp -i %p /pg_data_logs/archivedir/%f </dev/null' 
  6. #archive_timeout = 600 
  7. archive_timeout = 86400 
  8. max_wal_senders = 5 
  9. wal_keep_segments = 32 

 

建立归档目录

mkdir -p /pg_data_logs/archivedir

重启

/etc/init.d/postgresd restart

--IN STANDBY SERVER:

修改postgresql服务配置文件

  1. vi /usr/local/pgsql/etc/postgresql.conf  
  2. #Add by paolo for replications  
  3. wal_level = hot_standby 
  4. hot_standby = on 
  5.  
  6. vi /usr/local/pgsql/etc/recovery.conf  
  7. #Add by paolo for replications  
  8. restore_command = 'cp /pg_data_logs/archivedir/%f %p'   
  9. archive_cleanup_command = 'pg_archivecleanup /pg_data_logs/archivedir %r' 
  10. standby_mode = 'on' 
  11. primary_conninfo = 'host=10.4.5.94 port=5432 user=repl password=meiyoumima' 
  12. trigger_file = '/home/postgres/trigger_activestb' 

 

建立归档目录

mkdir -p /pg_data_logs/archivedir

停止postgres

/etc/init.d/postgresd stop

删除原数据目录下数据文件

  1. exit (su - root)  
  2. cd /usr/local/pgsql/  
  3. rm -rf data/  
  4. mkdir data  
  5. chown postgres data  
  6. chmod -R 700 data/ 
  1. >>>>>>>>>>>>>>传送数据文件到StandBy并启动集群<<<<<<<<<<<<<<<<< 
  2. --IN PRIMARY  
  3. su - postgres  
  4. psql -c "SELECT pg_start_backup('label',true);"  
  5. cd /usr/local/pgsql/  
  6. scp -r data/ postgres@10.4.5.93:/usr/local/pgsql/  
  7. scp -r data/ postgres@10.4.5.91:/usr/local/pgsql/  
  8.  
  9. --IN STANDBY  
  10. su - postgres  
  11. cd /usr/local/pgsql/data  
  12. rm postmaster.pid  
  13. ln -s /usr/local/pgsql/etc/recovery.conf recovery.conf  
  14. cd pg_xlog  
  15. mv * /pg_data_logs/archivedir/  
  16. /etc/init.d/postgresd start  
  17.  
  18. --IN PRIMARY  
  19. su - postgres  
  20. psql -c "SELECT * from pg_stop_backup();" 

 

重启

  1. /etc/init.d/postgresd restart  
  2. -------------------------------------------------------  
  3. >>>>>>>>>pg_archivecleanup inatall<<<<<<<<<<<<< 
  4. ------------------------------------------------------- 
  5. su - root  
  6. cd postgresql-9.0.4/contrib/pg_archivecleanup/  
  7. make  
  8. make install 

 

关于PostgreSQL安装及Streaming Replication配置就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

  1. PostgreSQL数据库中pg_hba.conf文件的使用详解
  2. MongoDB数据库中Update参数使用的相关知识简介
  3. SQL Server使用UNION代替OR提升查询性能的实例
  4. Berkeley DB使用SecondKey给数据排序的实现方法
  5. SQL Server数据库中FOR XML AUTO的使用详解续
责任编辑:赵鹏 来源: ChinaUnix博客
相关推荐

2018-09-13 08:47:09

Tomcat负载均衡服务器

2017-06-06 08:31:10

Spark Strea计算模型监控

2018-04-09 12:25:11

2022-01-10 07:59:14

PostgreSQl 主从流复制归档配置

2010-06-13 17:07:10

Cacti使用手册

2019-10-21 13:28:38

UbuntuPostgreSQL命令

2009-06-11 10:00:50

Glassfish安装GlassFish配置

2013-12-26 13:19:26

PostgreSQL优化

2009-06-19 18:19:01

2010-06-03 13:21:46

Sendmail 配置

2012-02-23 09:51:58

虚拟化SRM桌面虚拟化

2010-06-21 14:57:32

Linux apt

2009-07-09 15:58:40

Ubuntu JDK安

2010-09-10 20:19:34

tftp server

2009-07-17 17:34:15

JRuby On Ra

2023-05-18 07:58:27

2010-06-07 15:12:12

Cacti配置

2009-07-06 23:30:22

2020-05-26 15:05:30

Spark安装环境

2010-06-01 16:46:38

Rsync 命令
点赞
收藏

51CTO技术栈公众号