postgresql pgsql最新版安装指南及数据存储路径更改及主从配置
安装指南
- 首先在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 - 更新对应远程pg的repo的key
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - - 更新本地的list
sudo apt-get update - 安装PGSQL
sudo apt-get install postgresql
PGSQL更改数据的存储路径
- 找到配置文件查看原来的数据存储路径在哪
sudo find / -name postgresql.conf
一般是在/etc/postgresql/9.6/main/postgresql.conf - 停掉PGSQL
sudo service postgresql stop - 拷贝原来的数据路径到新的路径下
sudo cp -rf /var/lib/postgresql/9.6/main/ /data/postgresql/ - 设置用户和权限
sudo chown -R postgres:postgres /data/postgresql/
sudo chmod 700 /data/postgresql/ - 将配置文件的数据存储路径改成新的
sudo vim /etc/postgresql/9.6/main/postgresql.conf - 再启动就行了
sudo service postgresql start
PGSQL主从配置
- 修改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 - 创建一个超级用户来专门负责让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 - 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 - 配置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.htmCentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htmPostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htmWindows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htmUbuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htmUbuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htmCentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htmPostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm------------------------------------华丽丽的分割线------------------------------------
PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里
本文永久更新链接地址