地图-安装PostgreSQL

时间:2025-02-12 14:52:48

安装PostgreSQL

  1. 服务信息如下:
    表1 服务信息2

    服务名

    服务器

    安装目录

    端口

    账号/密码

    Postgresql-9.6.8

    10.190.x.x(主库)

    10.190.x.x(从库)

    /app/postgresql

    5432

    Postgres/******

  2. 安装PG
    1. 准备安装包

      将编译好的在ARM架构欧拉系统编译通过的PostgreSQL+PostGIS软件包放到服务器/app目录下并解压:

      tar xf postgresql_9.6.8_arm_eulerOS.tar.gz
    2. 添加用户并授权
      groupadd  postgresuseradd postgres -g postgres -m -d /home/postgreschown -R postgres:postgres /app/postgresql
    3. 切换用户,启动PG
      su - postgrescd /app/postgresql./bin/pg_ctl -D data -l logfile start
    4. 配置主从同步

      以172.16.1.22(主),172.16.1.21(从)为例,编辑主节点配置

      vi /app/pg/data/pg_hba.conf   #如下:host     all      all       slave1ip/32       trust   #允许连接到主服务器host   replication    replica     slave1ip/32      md5   #允许用replica复制

      这里在最下面添加两行:

      host     all             all             172.16.1.21/32          trusthost     replication     replica         172.16.1.21/32          trust vi /app/postgresql/data/postgresql.conf     #如下:data_directory = '/app/postgresql/data'             #自定义data目录listen_addresses = '*'                           #监听所有iparchive_mode = on                             #允许归档archive_command = 'cp %p /app/postgresql/data/pg_archive/%f'   #使用命令归档wal_level = replica                            #选择热备replica或logicalmax_wal_senders = 16                         #最多多少个流复制链接wal_keep_segments = 256                      #流复制保留最多的xlog数wal_sender_timeout = 60s                      #流复制主机发送数据超时时间max_connections = 5000               #从库的max_connections必须大于主库的
    5. 创建用户replica进行主从同步,并赋予登录和复制的权限
      • 登录到数据库里(主节点)
        su postgrescd  /app/postgresql/./bin/psqlCREATE ROLE replica login replication encrypted password 'replica';./bin/pg_ctl -D data -l logfile restart
      • slave1部分:#先备份数据,再同步数据
        ./bin/pg_ctl -D data -l logfile stop   ## 保持服务处于关闭状态#自定义存档目录,先把旧的data全部移走mkdir /home/postgres/pg_archive/mv  /app/postgresql/data/  /home/postgres/pg_archive/chmod 700 pg_archive && chown postgres:postgres pg_archive/su  postgresrm -rf  /app/postgresql/data/*   #先将data目录下的数据都清空#为空的情况下,把主节点的数据用pg_basebackup同步到从节点cd /app/postgresql/binpg_basebackup -P -h masterip -U replica -D /app/postgresql/data -X stream./pg_basebackup -P -h 172.16.1.22 -U replica -D /app/postgresql/data -X stream#配置recovery.conf,最底下添加三行。cp /app/postgresql/share/recovery.conf.sample /app/postgresql/data/recovery.confvi /app/postgresql/data/recovery.confstandby_mode = on        #该节点为从primary_conninfo = 'host=$masterip port=5432 user=replica password=replica'                        #主服务器的ip、userrecovery_target_timeline = 'latest'##trigger_file = '/tmp/trigger_file0'#配置postgresql.conf   ## 添加到75行位置,其余内容不需修改。vi /app/postgresql/data/postgresql.confmax_connections = 5500     #尽量大于主连接数的10%max_standby_streaming_delay = 30swal_receiver_status_interval = 10shot_standby = on    ##从节点默认用off , 配置on启动psql可查询数据。hot_standby_feedback = on  #出现错误复制,向主机反馈#开启从数据库../bin/pg_ctl -D data -l logfile startnetstat  -tlnp
    6. 查看复制状态(主库172.16.1.22)
      ./bin/psqlselect client_addr,sync_state from pg_stat_replication;
      图1 查看复制状态
      ##主从节点的进程多了wal进程:ps -ef | grep postgrespostgres: wal sender process replica 172.16.1.21postgres: wal receiver process   streaming#用pg_controldata命令查询主从集群运行状态[postgres@host-172-16-1-22 bin]$ ./pg_controldata /app/postgresql/data/pg_control version number:            960Catalog version number:               201608131Database system identifier:           7127200572656879006Database cluster state:               in production[postgres@host-172-16-1-21 bin]$ ./pg_controldata /app/postgresql/data/pg_control version number:            960Catalog version number:               201608131Database system identifier:           7127200572656879006Database cluster state:               in archive recovery

      主从搭建成功后,主库的集群状态是in production,从库是in archive recovery,当主库崩溃,可以切换从库为主库。这时候主库状态是shut down,而从库是in production。

support.huaweicloud.com/sfmcdtp-mapslt/sfmcdtp_07.html