PostgreSQL主从部署

1.依赖安装

yum install gcc-c++ -y
yum install perl-ExtUtils-Embed -y
yum install readline-devel -y
yum install zlib-devel -y
yum install python-devel -y

2.创建用户

groupadd postgres
useradd -g postgres postgres
echo "postgres"|passwd postgres --stdin

3.编译安装

tar -xvf postgresql-13.4.tar.gz
cd postgresql-13.4
./configure --prefix=/usr/local/pgsql13.4 --with-perl --with-python
make && make install
ln -sf /usr/local/pgsql13.4 /usr/local/pgsql

chown -R postgres:postgres /usr/local/pgsql13.4/
chown -R postgres:postgres /usr/local/pgsql/

安装contrib目录下的工具

cd postgresql-13.4/contrib
make && make install

4.环境变量

vim .bash_profile
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PGHOST=/tmp
export LANG=en_US.UTF-8
export PGDATA=/postgres/

vim /etc/profile
export PATH=/usr/local/pgsql/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

5.创建数据库实例

mkdir /postgres/
chown -R postgres:postgres /postgres/
export PGDATA=/postgres/
initdb -k   #对于数据可靠性要求很高的尝尽,建议打开数据块checksum校验功能
#12之后可以后期可以打开
pg_checksums -c -D /postgres/
pg_checksums: error: data checksums are not enabled in cluster #表示没有开启
pg_checksums -e -P -D /postgres/ #开启checksum校验功能

Success. You can now start the database server using:

    pg_ctl -D /postgres -l logfile start

6.启库

pg_ctl -D /postgres -l logfile start
pg_ctl stop -D /postgres

pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]

其中-m用于指定数据库的停止方法,有以下3种模式:
·smart:等所有连接中止后,关闭数据库。如果客户端连接不终止,则无法关闭数据库。
·fast:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。相当于Oracle数据库关闭时的immediate模式。
·immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行恢复。相当于Oracle数据库关闭时的abort模式。

7.设置管理员密码

psql
\password postgres

8.参数配置

postgresql配置文件默认在数据存储目录

vim /postgres/postgresql.conf
logging_collector = on	#10版本默认打开

#10版本以上默认为如下方案
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on 
log_rotation_age = 1d
log_rotation_size = 0

9.主库启用流复制

vim pg_hba.conf
host    replication     all             0/0                     md5

vim postgresql.conf
listen_addresses = '*'
max_wal_senders = 10
wal_level = replica
min_wal_size = 800MB

10.standby生成基础备份

pg_basebackup -h 192.168.124.61 -U postgres -F p -P -X stream -R -D $PGDATA -l osdbabackup202110231936

chmod 700 -R /postgres/
export PGDATA=/postgres/
pg_ctl start

11.主库查看链接状态,测试

select client_addr,state,sync_state from pg_stat_replication;

create  table test01(id int primary key, note text);
insert into test01 values(1,'11111');
insert into test01 values(2,'22222');
insert into test01 values(3,'33333');

standby查看状态
\x
select * from pg_stat_wal_receiver;

12.主从切换
主从全部停止

pg_ctl stop

主库:

touch $PGDATA/standby.signal
vim postgresql.auto.conf
primary_conninfo = 'user=postgres password=postgres host=192.168.124.62 port=5432 sslmode=prefer sslcompression=0'

从库:

mv $PGDATA/standby.signal $PGDATA/standby.signal.202110232053

另一个从库:
修改主库IP

vim postgresql.auto.conf
primary_conninfo = 'user=postgres password=postgres host=192.168.124.62 port=5432 sslmode=prefer sslcompression=0'

主从全部启动

pg_ctl start

select pid,usename,application_name,client_addr,client_hostname,client_port,backend_start,state from pg_stat_replication;
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐