阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

PostgreSQL pgsql最新版安装指南及数据存储路径更改及主从配置

118次阅读
没有评论

共计 3034 个字符,预计需要花费 8 分钟才能阅读完成。

postgresql pgsql 最新版安装指南及数据存储路径更改及主从配置

安装指南

  1. 首先在 apt 的 list 添加你当前系统版本对应的 apt 列表
    目前官网有 16.04,14.04,12.04 分别对应下面的 xenial,xenial,precise
    sudo vim /etc/apt/sources.list.d/pgdg.list
    根据对应系统复制下面的 repo 到 pgdg.list 中,
    deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main
    deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
    deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main

  2. 更新对应远程 pg 的 repo 的 key
    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

  3. 更新本地的 list
    sudo apt-get update

  4. 安装 PGSQL
    sudo apt-get install postgresql

PGSQL 更改数据的存储路径

  1. 找到配置文件查看原来的数据存储路径在哪
    sudo find / -name postgresql.conf
    一般是在/etc/postgresql/9.6/main/postgresql.conf

  2. 停掉 PGSQL
    sudo service postgresql stop

  3. 拷贝原来的数据路径到新的路径下
    sudo cp -rf /var/lib/postgresql/9.6/main/ /data/postgresql/

  4. 设置用户和权限
    sudo chown -R postgres:postgres /data/postgresql/
    sudo chmod 700 /data/postgresql/

  5. 将配置文件的数据存储路径改成新的
    sudo vim /etc/postgresql/9.6/main/postgresql.conf

  6. 再启动就行了
    sudo service postgresql start

PGSQL 主从配置

  1. 修改 postgresql.conf
    sudo vim /etc/postgresql/9.6/main/postgresql.conf
    listen_addresses = '*'
    wal_level = replica
    max_wal_senders = 2
    wal_keep_segments = 64
    hot_standby = on

  2. 创建一个超级用户来专门负责让 standby 连接去拖 WAL 日志
    create user replica superuser password '1234';
    将添加的用户添加到 hba 的配置中
    sudo vim /etc/postgresql/9.6/main/pg_hba.conf
    host replication replica 1.1.1.1 md5

  3. pg_basebackup 创建备库
    使用 postgres 用户创建备库
    pg_basebackup -D /data/postgresql/ -Fp -Xs -v -P -h 1.1.1.1 -p 5432 -U replica
    备库创建成功之后修改 hba 配置文件:
    sudo vim /etc/postgresql/9.6/main/pg_hba.conf
    添加以下的内容:
    host all all 0.0.0.0/0 trust
    host replication repuser master md5

  4. 配置 recovery.conf
  • master 端
    sudo cp /usr/share/postgresql/9.6/recovery.conf.sample /data/postgresql/recovery.done
    sudo vim /data/postgresql/recovery.done
    修改以下的配置项
    recovery_target_timeline = 'latest'
    standby_mode = on
    primary_conninfo = 'host=slave.ip port=5432 user=repuser password=repuser'
    trigger_file = '/home/postgres/data/trigger_file'

  • slave 端
    sudo cp /usr/share/postgresql/9.6/recovery.conf.sample /data/postgresql/recovery.conf
    sudo vim /data/postgresql/recovery.conf
    修改以下的配置项
    recovery_target_timeline = 'latest'
    standby_mode = on
    primary_conninfo = 'host=master.ip port=5432 user=repuser password=repuser'
    trigger_file = '/home/postgres/data/trigger_file'

主从配置基本步骤到这就结束了。
如果进入从库遇到 psql: FATAL: the database system is starting up 的问题只需要修改一下从库的配置文件 postgresql.conf,设置 standby=on 就可以了。

———————————— 华丽丽的分割线 ————————————

在 CentOS 6.5 上编译安装 PostgreSQL 9.3 数据库 http://www.linuxidc.com/Linux/2016-06/132272.htm

CentOS 6.3 环境下 yum 安装 PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htm

PostgreSQL 缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htm

Windows 平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htm

Ubuntu 下 LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htm

Ubuntu 上的 phppgAdmin 安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htm

CentOS 平台下安装 PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htm

PostgreSQL 配置 Streaming Replication 集群 http://www.linuxidc.com/Linux/2014-05/101724.htm

———————————— 华丽丽的分割线 ————————————

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139944.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3034字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中