#主机
192.168.89.11 pgsql1 ##主服务器##
192.168.89.12 pgsql2 ##从服务器##
#下载源码包
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
sudo su
mkdir /usr/local/pgsql
mkdir /usr/local/pgsql/postgresql-12.2
chown -R postgres:postgres /usr/local/pgsql
安装必要依赖
yum install -y bison flex readline-devel zlib-devel
yum install docbook-dtds docbook-style-xsl fop libxslt -y
yum install -y gcc
解压
su postgres
tar -zxvf postgresql-12.2.tar.gz
cd ~/postgresql-12.2
配置编译
./configure --prefix=/usr/local/pgsql/postgresql-12.2
编译
make
make install
创建数据库文件夹
mkdir -p /usr/local/pgsql/postgresql-12.2/data
初始化数据库
/usr/local/pgsql/postgresql-12.2/bin/initdb -D /usr/local/pgsql/postgresql-12.2/data/
启动数据库
/usr/local/pgsql/postgresql-12.2/bin/pg_ctl -D /usr/local/pgsql/postgresql-12.2/data/ -l logfile start
停止数据库
/usr/local/pgsql/postgresql-12.2/bin/pg_ctl -D /usr/local/pgsql/postgresql-12.2/data/ stop
重启数据库
/usr/local/pgsql/postgresql-12.2/bin/pg_ctl restart -D /usr/local/pgsql/postgresql-12.2/data/ -m fast
cd /usr/local/pgsql/postgresql-12.2/data/
cp postgresql.conf postgresql.conf_bak
vi postgresql.conf
修改 listen_addresses = ‘本地ip’ 前面的#号需要去掉。
连接数 增大一些,博主team十几个人,默认100连接数不够用,如果做主从配置,从库需要比主库大,博主将从库设置为1100
max_connections = 1000
vi /usr/local/pgsql/postgresql-12.2/data/pg_hba.conf
在文件底部,增加如下配置
host all all 0.0.0.0/0 password
vi ~/.bash_profile
增加配置
export PGHOME=/usr/local/pgsql/postgresql-12.2
export PGDATA=/usr/local/pgsql/postgresql-12.2/data
PATH=$PATH:$HOME/bin:$PGHOME/bin
刷新
source ~/.bash_profile
cd ~/postgresql-12.2/contrib/start-scripts
sudo su
chmod a+x linux
cp linux /etc/init.d/postgresql
vi /etc/init.d/postgresql
修改配置文件
修改三个地方PGUSER、prefix、PGDATA,如下
PGUSER是一个非root用户(博主的PGUSER是postgres)
PGUSER=postgres
prefix=/usr/local/pgsql/postgresql-12.2
PGDATA="/usr/local/pgsql/postgresql-12.2/data"
开机自启
chkconfig --add postgresql
chkconfig
service postgresql start
ps -ef | grep postgres
source ~/.bash_profile
psql -U postgres
ALTER USER postgres with encrypted password ‘你的密码’;
\q
问题:role “postgres” does not exist
cd /usr/local/pgsql/postgresql-12.2/bin
./createuser -s postgres
在主库上创建主从账户
psql -U postgres
CREATE ROLE rep login replication encrypted password ‘rep’;
退出
\q
vi /usr/local/pgsql/postgresql-12.2/data/pg_hba.conf
增加如下配置
#运行guoxm用户在70上复制本机数据
host replication rep 192.168.89.12/32 password
vi /usr/local/pgsql/postgresql-12.2/data/postgresql.conf
配置文件如下
listen_addresses = ‘192.168.89.12’ #监听所有ip
archive_mode = on #开启归档模式
archive_command = ‘cp %p /usr/local/pgsql/postgresql-12.2/data/%f’ #归档命令
wal_level = hot_standby #热备模式
service postgresql restart
测试数据库。命令
psql -h 192.168.89.11 -U postgres
删除从库
rm -rf /usr/local/pgsql/postgresql-12.2/data/*
从主库同步数据到从库
/usr/local/pgsql/postgresql-12.2/bin/pg_basebackup -h 192.168.89.11 -p 5432 -U rep -Fp -Xs -Pv -R -D /usr/local/pgsql/postgresql-12.2/data/
pgsql 12版本,不再支持recovery.conf
standby_mode 配置在 standby.signal 中配置
primary_conninfo 配置在 postgresql.conf 中配置
vi /usr/local/pgsql/postgresql-12.2/data/standby.signal
修改如下配置
#表示该节点是从库
standby_mode = on
vi /usr/local/pgsql/postgresql-12.2/data/postgresql.conf
配置内容如下
primary_conninfo = ‘host=192.168.89.11 port=5432 user=rep password=rep’
wal_level = replica #热备模式
hot_standby = on #说明这台机器不仅用于数据归档,还可以用于数据查询
hot_standby_feedback = on #r出现错误复制,向主机反馈
service postgresql restart
验证是否部署成功
在主库上查询从库客户端
psql -U postgres
select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-------------±-----------
192.168.89.12| async