Install PostgreSQL 12.5 master-slave cluster using source code
Original link of this article: https://blog.csdn.net/xzk9381/article/details/110385135
-
Postgresql version : 12.5
-
Operating system version : CentOS Linux release 7.6.1810 (Core)
-
IP of the master node : 10.211.55.11
-
Slave node IP : 10.211.55.12
1. Environmental preparation
The following operations are performed on two machines.
1. Download the source package
PostgreSQL version 12.5 source code package download address: https://ftp.postgresql.org/pub/source/v12.5/postgresql-12.5.tar.gz
After the download is complete, create the following directories on the two machines:
mkdir -p /opt/postgresql_data/{
data,logs}
2. Configure yum source
Configure Ali yum warehouse in the machine:
wget http://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS7-Base-ali.repo
sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-ali.repo
Install PostgreSQL dependency packages:
yum -y install readline-devel zlib-devel
3. Turn off the firewall and SELinux
In order to avoid being denied access, you need to turn off the firewall and SELinux:
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
4. Create User
PostgreSQL recommends using the postgres user to manage, first create the corresponding user and group:
groupadd postgres
useradd -g postgres -G postgres -d /home/postgresql postgres
echo postgres_admin | passwd --stdin postgres
Change the owner and group of the /opt/postgresql_data directory to postgres:
chown postgres.postgres -R /opt/postgresql_data/
Two, install the master node
1. Install PostgreSQL
In 10.211.55.11, unzip the PostgreSQL source package:
tar zxf postgresql-12.5.tar.gz -C /opt
In the decompressed directory, execute the following compile and install commands:
./configure --prefix=/opt/postgresql_data
make && make install
# 下面的命令可以安装 PostgreSQL 的第三方工具
cd contrib
make && make install
After the installation is complete, executable files and library files will be stored in the /opt/postgresql_data directory:
[root@localhost ~]# tree -L 1 -d /opt/postgresql_data/
/opt/postgresql_data/
├── bin
├── data
├── include
├── lib
├── logs
└── share
6 directories
Next, set environment variables under the postgres user, and add the following two lines to the .bash_profile file in the postgres user's home directory and make it effective:
export LD_LBRARY_PATH=/opt/postgresql_data/lib:$LD_LIBRARY_PATH
export PATH=/opt/postgresql_data/bin:$PATH
2. Initialize the database
The following steps are performed using the postgres user:
First initialize PostgreSQL, and specify the data directory as the /opt/postgresql_data/data directory:
initdb --locale=C -E UNICODE -D /opt/postgresql_data/data/
After the command is executed successfully, the following will be output:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /opt/postgresql_data/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /opt/postgresql_data/data/ -l logfile start
3. Modify the configuration file
After the initialization is complete, check the contents under the /opt/postgresql_data/data directory:
[postgres@localhost ~]$ ls /opt/postgresql_data/data/
base pg_hba.conf pg_notify pg_stat pg_twophase postgresql.auto.conf
global pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
pg_commit_ts pg_logical pg_serial pg_subtrans pg_wal
pg_dynshmem pg_multixact pg_snapshots pg_tblspc pg_xact
The main configuration file is postgresql.conf, and the remote access permission configuration file is pg_hba.conf.
First modify the content of the postgresql.conf file and modify the following configuration items:
listen_addresses = '*' # 修改监听地址为 *
max_connections = 1000 # 修改最大连接数为 1000
log_destination = 'stderr' # 去掉该配置项的注释
logging_collector = on # 去掉该配置项的注释,并将 off 修改为 on,代表开启日志记录
log_directory = '../logs' # 去掉该配置项的注释,并指定写入日志的目录
log_filename = 'postgresql-%Y-%m-%d.log' # 去掉该配置项的注释,并指定日志名称的格式
Next, modify the pg_hba.conf configuration file and add the following line:
host all all 0.0.0.0/0 trust
This representative allows all users to use passwords to access the database on any remote machine.
4. Start the database
There are two ways to start the database, one is to start it manually, and the other is to start it through a script.
4.1 Manual start
To start the database manually, you need to use the postgres user to execute the pg_ctl command. When executing the command, you need to specify the path of the data directory. The command is as follows:
pg_ctl -D /opt/postgresql_data/data/ -l ../log/start_log start
The -l parameter is to output the log at startup to the specified file
To stop the database, just replace start with stop.
4.2 Script start
In the PostgreSQL source code package, a script command to start the database is provided. The path is as follows:
[postgres@localhost ~]$ ls /opt/postgresql-12.5/contrib/start-scripts/
freebsd linux macos
The linux file is the script used to start the service in Linux. Use the root user to copy the script to the /etc/init.d directory and rename it to postgresql:
cp -pr /opt/postgresql-12.5/contrib/start-scripts/linux /etc/init.d/postgresql
chmod u+x /etc/init.d/postgresql
Modify the following configuration items in the script:
# Installation prefix
prefix=/opt/postgresql_data
# Data directory
PGDATA="/opt/postgresql_data/data"
# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="/opt/postgresql_data/logs/serverlog"
Set boot up:
chkconfig --add postgresql
Then you can use the script to control the server start and stop:
service postgresql start
Original link of this article: https://blog.csdn.net/xzk9381/article/details/110385135
5. Create a replication role
Use the postgres user to operate PostgreSQL to create a role for master-slave replication:
[postgres@localhost ~]$ createuser --replication -P -e replicator
Enter password for new role:
Enter it again:
SELECT pg_catalog.set_config('search_path', '', false);
CREATE ROLE replicator PASSWORD 'md5eb5c69398a31748cd75327d33bc1f52b' NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN REPLICATION;
-P option is used to set a password;
-e option is used to set echo;
To set remote access permissions for this role, add the following line to the pg_hba.conf file:
host replication replicator 10.211.55.12/24 md5
This representative allows the use of the replicator role on the 10.211.55.12 machine to remotely access the database to perform replication operations.
Restart the database:
service postgresql restart
Three, install the slave node
First follow the steps to install the master node, and perform steps 1-4.
Then stop the service of the slave node, back up the data in the data directory, and then clear it. Use the postgres user to execute the pg_basebackup command to connect to the remote master node for backup:
pg_basebackup -h 10.211.55.11 -D /opt/postgresql_data/data/ -U replicator -P -v -R -X stream -C -S pgstandby1
-h
: Specify the host as the master server.-D
: Specify the data directory.-U
: Specify the connection user.-P
: Enable progress reporting.-v
: Enable detailed mode.-R
: Enable the creation of the recovery configuration: Create a standby.signal file and append the connection settings to postgresql.auto.conf under the data directory .-X
: Used to include the required write-ahead log file (WAL file) in the backup. The value of stream indicates that the WAL was streamed when the backup was created.-C
: Before starting the backup, it is allowed to create a replication slot named by the -S option.-S
: Specify the name of the replication slot.
After running the command, the following will be output:
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created replication slot "pgstandby1"
24591/24591 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/2000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: base backup completed
After the backup process is completed, a standby.signal
file will be created in the data directory and primary_conninfo
written to the postgresql.auto.conf
file:
[postgres@CentOS-2 data]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = 'user=replicator password=admin host=10.211.55.11 port=5432 sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
primary_slot_name = 'pgstandby1'
Start the slave node service:
service postgresql start
Fourth, verify the master-slave synchronization
First use the postgres user on the master node to view the replication slot information:
[postgres@localhost ~]$ psql -c "\x" -c "SELECT * FROM pg_replication_slots"
Expanded display is on.
-[ RECORD 1 ]-------+-----------
slot_name | pgstandby1
plugin |
slot_type | physical
datoid |
database |
temporary | f
active | t
active_pid | 12331
xmin |
catalog_xmin |
restart_lsn | 0/3000148
confirmed_flush_lsn |
View the status (process and database) of the slave server (WAL receiver process) in the slave server:
Process status:
[postgres@CentOS-2 ~]$ ps aux | grep walreceiver | grep -v grep
postgres 11225 0.0 0.0 318820 1868 ? Ss 17:00 0:00 postgres: walreceiver streaming 0/3000148
Database status:
[postgres@CentOS-2 ~]$ psql -c "\x" -c "SELECT * FROM pg_stat_wal_receiver;"
Expanded display is on.
-[ RECORD 1 ]---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 11225
status | streaming
receive_start_lsn | 0/3000000
receive_start_tli | 1
received_lsn | 0/3000148
received_tli | 1
last_msg_send_time | 2020-11-27 17:05:20.341976+08
last_msg_receipt_time | 2020-11-27 17:05:20.486497+08
latest_end_lsn | 0/3000148
latest_end_time | 2020-11-27 17:01:19.763682+08
slot_name | pgstandby1
sender_host | 10.211.55.11
sender_port | 5432
conninfo | user=replicator password=******** dbname=replication host=10.211.55.11 port=5432 fallback_application_name=walreceiver sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any
Next, create a new library in the main library:
[postgres@localhost ~]$ psql -c "create database repli_test;"
CREATE DATABASE
[postgres@localhost ~]$ psql -c "\l"
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
repli_test | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Go to the slave library to see if there is this library:
[postgres@CentOS-2 ~]$ psql -c "\l"
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
repli_test | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
You can see that the database information has been synchronized, which means that the current synchronization is normal.
Original link of this article: https://blog.csdn.net/xzk9381/article/details/110385135