PostgreSQL

PostgreSQL单点部署
  1. ansible-playbook -i inventories/${project} playbooks/common/postgresql_standalone.yml -e init_postgresql=true -e postgresql_data_dir=/data/postgresql/11/main -e postgresql_archivedir_dir=/data/postgresql/archivedir -b
  2. # 创建数据目录
  3. # 默认数据目录/var/lib/postgresql/11/main
  4. # 默认配置目录/etc/postgresql/11/main
  5. mkdir -p /data/pgsql/11/data
  6. # 迁移数据目录
  7. rsync -avr /var/lib/postgresql/11/main/* /data/pgsql/11/data/
  8. cp -fr /etc/postgresql/11/main/* /data/pgsql/11/data/
  9. chown -R postgres.postgres /data/pgsql

PostgreSQL集群部署

新增 Apt源
  1. # 官方源(x86)
  2. echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" > /etc/apt/sources.list.d/pgdg.list
  3. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  4. # 国内源(x86)
  5. echo "deb https://mirrors.tuna.tsinghua.edu.cn/postgresql/repos/apt/ `lsb_release -cs`-pgdg main" > /etc/apt/sources.list.d/pgdg.list
  6. wget --quiet -O - https://mirrors.tuna.tsinghua.edu.cn/postgresql/repos/apt/ACCC4CF8.asc | sudo apt-key add -
  7. # 官方源(arm)
  8. sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
  9. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  10. sudo apt-get update

安装PG及依赖
  1. # pg
  2. apt install -y postgresql-11 postgresql-11-postgis-2.5 postgresql-server-dev-11
  3. # pgpool
  4. apt install -y postgresql-11-pgpool2 pgpool2 arping

安装分词器
  1. cd /root
  2. wget -c http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
  3. bzip2 -d scws-1.2.3.tar.bz2
  4. tar -xf scws-1.2.3.tar
  5. cd scws-1.2.3
  6. ./configure
  7. make install
  8. cd /root
  9. wget -c https://codeload.github.com/amutu/zhparser/zip/master
  10. unzip master
  11. cd zhparser-master
  12. export PATH=$PATH:/usr/lib/postgresql/11/bin
  13. SCWS_HOME=/root/scws-1.2.3 ; make && make install

PG数据目录初始化
  1. # 创建数据和归档目录
  2. # 默认数据目录/var/lib/postgresql/11/main
  3. # 默认配置目录/etc/postgresql/11/main
  4. mkdir -p /data/pgsql/11/data
  5. mkdir -p /data/pgsql/archivedir
  6. # 迁移数据目录[主&从]
  7. rsync -avr /var/lib/postgresql/11/main/* /data/pgsql/11/data/
  8. cp -fr /etc/postgresql/11/main/* /data/pgsql/11/data/
  9. chown -R postgres.postgres /data/pgsql
  10. # 移除原配置和数据目录[主&从]
  11. mv /etc/postgresql/11/main /etc/postgresql/11/main_unuse
  12. mv /var/lib/postgresql/11/main /var/lib/postgresql/11/main_unuse
  13. ln -s /data/pgsql/11/data /etc/postgresql/11/main
  14. ln -s /data/pgsql/11/data /var/lib/postgresql/11/main

PG流复制
  1. # 生成postgres用户的ssh免密互登录(节点相互交换密钥)
  2. su - postgres
  3. ssh-keygen
  4. # 流复制用户replica免密
  5. su - postgres
  6. cat > .pgpass <<EOF
  7. pg_01:5432:replication:replica:ve406wVs2hCeh
  8. pg_02:5432:replication:replica:ve406wVs2hCeh
  9. EOF
  10. # 权限
  11. chmod 600 /var/lib/postgresql/.pgpass
  12. # 流复制用户
  13. create role replica login replication encrypted password 've406wVs2hCeh';
  14. # 从节点复制数据
  15. pg_basebackup -X stream -c fast -P -D /data/pgsql/11/data -h pg-01 -p 5432 -U replica

配置 recovery.done文件

/data/pgsql/11/data/recovery.done

  1. standby_mode = 'on'
  2. primary_conninfo = 'host=hhhtlt-pg2 port=5432 user=replica'
  3. restore_command = 'scp -P 22 hhhtlt-pg2:/data/pgsql/archivedir/%f %p'

生成pgpool用户
  1. pg_md5 --md5auth --username=root <password of root user>
  2. pg_md5 --md5auth --username=lingmou <password of lingmou user>
  3. pg_md5 --md5auth --username=capture <password of capture user>

生成 pcp 用户
  1. pg_md5 -p

生成业务用户
  1. -- lingmou
  2. CREATE USER lingmou WITH PASSWORD 've406wVs2hCeh';
  3. CREATE DATABASE "k8s_lingmou" OWNER lingmou;
  4. GRANT ALL PRIVILEGES ON DATABASE "k8s_lingmou" TO lingmou;
  5. ALTER USER lingmou WITH SUPERUSER;
  6. -- uar
  7. CREATE USER uar WITH PASSWORD 'CM7JwCsPn4wb7';
  8. CREATE DATABASE "k8s_uar" OWNER uar;
  9. GRANT ALL PRIVILEGES ON DATABASE "k8s_uar" TO uar;
  10. -- communitySync
  11. CREATE USER communitydatasync WITH PASSWORD 'NI3BtGigLYyEL';
  12. CREATE DATABASE "k8s_communitySync" OWNER communitydatasync;
  13. GRANT ALL PRIVILEGES ON DATABASE "k8s_communitySync" TO communitydatasync;

Pgpool

  1. - 创建目录/var/log/pgpool且给pgpool启动用户rw权限,此目录failover.sh脚本需要写日志
  2. - template1库创建扩展函数pgpool_recovery