当前位置:   article > 正文

PostgreSQL 12安装及主备配置_pgsql12

pgsql12

安装

1.1.下载PostgreSQL 12安装包

下载地址:https://ftp.postgresql.org/pub/source/

1.2. 安装依赖包

yum install wget gcc gcc-c++  epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed readline readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel -y

1.3.解压安装包

  1. # tar -zxvf postgresql-12.0.tar.gz

1.4. 编译安装

到解压出来的目录下

    1. # postgresql-12.0]$#./configure --prefix=/usr/local/pgsql/12 --enable-nls --with-python --with-tcl --with-openssl --with-pam --with-ldap --with-systemd --with-libxml --with-libxslt
    2. # make && make install
    3. # chown -R postgres:postgres /usr/local/pgsql -R

1.5.修改文件

  1. # vi /etc/profile.d/pgsql.sh
  2. 添加
  3. export PATH=/usr/local/pgsql/12/bin:$PATH
  4. # source /etc/profile.d/pgsql.sh

1.6.初始化

切换到postgres用户,创建data目录

  1. # su - postgres
  2. pgsql_data]$ mkdir data

初始化

$ initdb -D /pgsql_data/data/ -U postgres —locale=en_US.UTF8 -E UTF8


1.7. 启动数据库

pg_ctl -D /pgsql_data/data -l /pgsql_data/data/serverlog start

至此单节点PG配置完成。

2 PG主备配置

主端:172.20.5.163 :5432
备端:172.20.5.225 :5432

2.1主库端操作

修改配置文件:

  1. cd /pgsql_data/data
  2. $ cp pg_hba.conf pg_hba.conf.bak
  3. # 允许repl用户进行流复制
  4. cat <<-eof >>/pgsql_data/data/pg_hba.conf
  5. host all all 0.0.0.0/0 md5
  6. host replication repl 172.20.0.0/22 trust
  7. eof

配置postgresql.conf

  1. cat <<-eof >/pgsql_data/data/postgresql.conf
  2. listen_addresses = '*'
  3. port = 5432
  4. max_connections = 1000
  5. superuser_reserved_connections = 100
  6. full_page_writes = on
  7. wal_log_hints = off
  8. max_wal_senders = 500
  9. hot_standby = on
  10. log_destination = 'csvlog'
  11. logging_collector = on
  12. log_directory = 'log'
  13. log_filename = 'postgresql-%Y-%m-%d_%H%M%S'
  14. log_rotation_age = 1d
  15. log_rotation_size = 10MB
  16. log_statement = 'mod'
  17. log_timezone = 'PRC'
  18. timezone = 'PRC'
  19. unix_socket_directories = '/tmp'
  20. shared_buffers = 512MB
  21. temp_buffers = 16MB
  22. work_mem = 32MB
  23. effective_cache_size = 2GB
  24. maintenance_work_mem = 128MB
  25. #max_stack_depth = 2MB
  26. dynamic_shared_memory_type = posix
  27. ## PITR
  28. full_page_writes = on
  29. wal_buffers = 16MB
  30. wal_writer_delay = 200ms
  31. commit_delay = 0
  32. commit_siblings = 5
  33. wal_level = replica
  34. archive_mode = on
  35. archive_command = 'test ! -f /data/pgdata/archivedir/%f && cp %p /data/pgdata/archivedir/%f'
  36. archive_timeout = 60s
  37. Eof

重启数据库。

2.2创建主备流复制用户:

  1. psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'postgres';"
  2. psql -U postgres -c "CREATE USER repl WITH PASSWORD 'postgres' REPLICATION;"

修改文件:pg_hba.conf
添加:

host    replication     repl            172.20.5.225/24            trust

2.3 备库端操作

2.3.1 备库端安装PG同主库端,备库安装到初始化步骤。

2.3.2 清空data 目录,重建DATA目录:

2.3.3 执行命令,流复制建库:

pg_basebackup -h 172.20.5.163 -p 5432 -U repl -w -Fp -Xs -Pv -R -D /pgsql_data/data/

-h 连接的是主库端的IP,-p 主库port。

注意报错

此错误出现在配置流复制备节点执行pg_basebackup的时候
解决方法:重启下主端节点,由于主节点有新的配置,因此需要重启使其生效。

2.3.4 修改从库端配置文件postgresql.conf

  1. max_connections = 150
  2. wal_level = hot_standby
  3. archive_mode = on
  4. hot_standby = on
  5. archive_timeout = 60s
  6. max_standby_streaming_delay = 30s
  7. wal_receiver_status_interval = 10s
  8. hot_standby_feedback = on

2.3.5 修改文件:postgresql.auto.conf

添加:

primary_conninfo = 'user=repl passfile=''/home/postgres/.pgpass'' host=172.20.5.163 port=5432 sslmode=prefer sslcompression=0 gssencmode=disable target_session_attrs=any'

2.3.6 修改data目录权限,启动备库

  1. ]$ chmod -R 0750 /pgsql_data/data
  2. ]$ pg_ctl -D /pgsql_data/data -l /pgsql_data/data/serverlog start

2.4 主备状态检查

主库端:

  1. select * from pg_stat_replication;

  1. postgres=# select pg_is_in_recovery(); 主机结果为:f,备机结果为t

备库端:

至此,PG主备配置完成。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/256496
推荐阅读
相关标签
  

闽ICP备14008679号