Centos7 yum install postgresql 9.5

Add RPM

yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-3.noarch.rpm

Install PostgreSQL 9.5

yum install postgresql95-server postgresql95-contrib

Initialize the database

/usr/pgsql-9.5/bin/postgresql95-setup initdb

 boot

systemctl enable postgresql-9.5.service

 Start the database service

systemctl start postgresql-9.5.service

Query version

psql --version

Change password

su - postgres
psql -U postgres
ALTER USER postgres WITH PASSWORD '123456'
\q

 

 

postgresql9.5 master-slave replication

system version

CPU name

IP addresses

postgresql version

Character

centos7.6

postgresql-master

192.168.216.130

9.5.18

Main library

centos7.6

postgresql-slave

192.168.216.129

9.5.18

From the library

Description: The two servers are already pre-installed with yum good way postgresql 9.5, note that there is no need to initialize and start the service from the library , this document is a master-slave replication configuration, the main library can read and write, read only from the library You can not write data.

Main library configuration:

>> CAT / var / lib / pgSQL / 9.5 / Data / << the postgresql.conf the EOF 

wal_level hot_standby = # (default is minimal) 

max_wal_senders = 2 # (default 0) 

wal_keep_segments # = 64 (default 0) 

the EOF

Explanation

wal_level represents start to build Hot Standby, max_wal_senders will need to be set to a number greater than 0, which represents the main library how many concurrent standby database up to, and the last wal_keep_segments should also be set to a largest possible value, in order to prevent the main library WAL logs generated too fast, the log has not had time to transmit standby to be covered, but need to consider disk space permits, the size of a WAL log file is 16M

As shown above, a WAL log file is 16M, if wal_keep_segments set to 64, that will standby database keeps 64 WAL log files, it will take up disk space 16 * 64 = 1GB, it is necessary to consider, in the disk the case is set larger space allows, it will reduce the risk of re-build of standby. Next you need to create a super user in the main library dedicated to make standby connection to mop WAL log

Create a synchronization user

postgres=# create user rep1 superuser password '123456';

 Modify the configuration file, to allow connecting the main bank from the bank server log data to mop WAL

vim /var/lib/pgsql/9.5/data/pg_hba.conf

host     replication     rep1            192.168.216.0/24        md5

  

 Here you need to configure the listener address as the default monitor local

vi /var/lib/pgsql/9.5/data/postgresql.conf
listen_addresses = '192.168.216.130' 

  

Restart Service

systemctl restart postgresql-9.5.service

 From the library configuration:

Execute the following command to back up data from the primary database

/usr/pgsql-9.5/bin/pg_basebackup -h 192.168.216.130 -U rep1 -F p -x -P -R -D /var/lib/pgsql/9.5/data/ -l rep_backup

The backup process is actually a process of copying data from the master data directory in the physical library.

Parameter Description

-F specifies the format of the output, support p (as output) or t (tar output format).

-x represents the job was started, another stream copy start WAL coupled to receive logs from the primary library.

-p representation allows real-time during the backup in progress print backup.

-R represent recovery.conf file will be automatically generated at the end of the backup, thus avoiding the manual creation.

-D specify which directory the backup is written, attention: the need to manually clear the data from the catalog backup before making basis.

-l represents specify the identity of a backup.

Since the copied file owner is root here need to change the owner and group

chown -R postgres.postgres /var/lib/pgsql/9.5/data/

 Modify the configuration file from the library

vim /var/lib/pgsql/9.5/data/postgresql.conf

247 #hot_standby = off          # "on" allows queries during recovery

248 hot_standby = on    #添加此行

 Note that row 59 need to annotate the monitoring address, since all copies of data files in the master database /, so there needs to be modified

 

Start from the library

systemctl start postgresql-9.5.service

 test:

Written test from the library:

Create a test table in the main library, while the query from the library

In the query from the library: as shown below

 

Guess you like

Origin www.cnblogs.com/caidingyu/p/11285882.html