CentOS7 postgresql installation and use

   Copyright statement: This article is an original article by Shaon Puppet. Please indicate the original address for reprinting. Thank you very much. https://blog.csdn.net/wh211212/article/details/80666735

CentOS7 postgresql installation and use#

Installation configuration postgresql

# installation
[ root@vm-06~]# yum -y install postgresql-server
# initialization
[ root@vm-06~]# postgresql-setup initdb 
Initializing database ... OK
# Set up postgresql can be remotely connected to log in
[ root@vm-06~]# vi /var/lib/pgsql/data/postgresql.conf
# Uncomment line 59 and change it to:
listen_addresses ='*'
# Line 395, add
log_line_prefix ='%t %u %d '[root@vm-06~]# systemctl start postgresql  
[ root@vm-06~]# systemctl enable postgresql 
[ root@vm-06~]# firewall-cmd --add-service=postgresql --permanent 
success
[ root@vm-06~]# firewall-cmd --reload 
success
[ root@vm-06~]# su - postgres 
- bash-4.2$ psql -c "alter user postgres with password 'password'"
ALTER ROLE
- bash-4.2$ createuser devops
- bash-4.2$ createdb testdb -O devops
- bash-4.2$ exit
logout
[ root@vm-06~]# su - devops
[ devops@vm-06~]$ 
[ devops@vm-06~]$ 
[ devops@vm-06~]$ 
[ devops@vm-06~]$ psql -l
         List of databases
 Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8| en_US.UTF-8| 
 template0 | postgres | UTF8     | en_US.UTF-8| en_US.UTF-8|=c/postgres          +||||| postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8| en_US.UTF-8|=c/postgres          +||||| postgres=CTc/postgres
 testdb    | devops   | UTF8     | en_US.UTF-8| en_US.UTF-8|(4 rows)[devops@vm-06~]$ psql testdb
psql(9.2.23)
Type "help"for help.

testdb=> alter user devops with password 'password';
ALTER ROLE
testdb=> create table test(no int,name text );
CREATE TABLE
testdb=> insert into test(no,name)values(1,'devops');         
INSERT 01
testdb=> select *from test;
 no |  name  
- - - - +- - - - - - - - 1| devops(1 row)

testdb=> drop table test;
DROP TABLE
testdb=> \q
[ devops@vm-06~]$ dropdb testdb

PostgreSQL master-slave configuration##

192.168.1.124 master
192.168.1.123 slave01

Configure PostgreSQL replication settings. The configuration is a master-slave setting.

[ root@vm-06~]# yum -y install postgresql-server
[ root@vm-06~]# postgresql-setup initdb 
Initializing database ... OK
[ root@vm-06~]# vi /var/lib/pgsql/data/postgresql.conf #Edit configuration file
# Line 59, uncomment and change to:
listen_addresses ='*'
# Line 165, uncomment and change to:
wal_level = hot_standby
# Line 168, uncomment and change to:
# on ⇒ sync
# remote_write ⇒ memory sync
# local ⇒ slave is asynchronous
# off ⇒ asynchronous
synchronous_commit = local
# On line 194, uncomment and change to:
archive_mode = on
# On line 196, uncomment and change to:
archive_command ='cp %p /var/lib/pgsql/archive/%f'
# On line 212, uncomment and change to:
max_wal_senders =2
# On line 214, uncomment and change to:
wal_keep_segments =10
# On line 221, uncomment and change to:
synchronous_standby_names ='slave01'
# 
[ root@vm-06~]# vi /var/lib/pgsql/data/pg_hba.conf
# File added at the end
# host replication [replication user][allowed IP addresses] password
 host    replication     replica          127.0.0.1/32            md5
host    replication     replica          192.168.1.1/32            md5

[ root@vm-06~]# systemctl start postgresql 
[ root@vm-06~]# systemctl enable postgresql 
# create a user for replication
[ root@vm-06~]# su - postgres
- bash-4.2$ createuser --replication -P replica 
Enter password fornewrole: #password
Enter it again: #password
# installation
[ root@vm-05~]# yum -y install postgresql-server
[ root@vm-05~]# su - postgres
# get backup from Master Server
- bash-4.2$ pg_basebackup -h 192.168.1.124-U replica -D /var/lib/pgsql/data -P --xlog 
Password:     # "replica" user's password
- bash-4.2$ vi /var/lib/pgsql/data/postgresql.conf

hot_standby = on
- bash-4.2$ cp /usr/share/pgsql/recovery.conf.sample /var/lib/pgsql/data/recovery.conf 
- bash-4.2$ vi /var/lib/pgsql/data/recovery.conf

restore_command ='scp 192.168.1.124:/var/lib/pgsql/archive/%f %p'

standby_mode = on

primary_conninfo ='host=192.168.1.124 port=5432 user=replica password=password application_name=slave01'-bash-4.2$ exit 
logout
[ root@vm-05~]# systemctl start postgresql 
[ root@vm-05~]# systemctl enable postgresql 
[ root@vm-06~]# su - postgres        
Last login: Tue Jun 1215:35:12+082018 on pts/0-bash-4.2$ psql -c "select application_name, state, sync_priority, sync_state from pg_stat_replication;" 
 application_name |   state   | sync_priority | sync_state 
------------------+-----------+---------------+------------
 slave01          | streaming |1|sync(1 row)

1
1
1
1

Recommended Posts

CentOS7 postgresql installation and use
centos7 kvm installation and use
Centos7 elk7.1.1 installation and use
Installation and use of Mysql under CentOS
KVM installation and preliminary use under CentOS 7.2
Centos7 installation and configuration prometheus
CentOS 7 installation and configuration PPTP
CentOS installation and configuration cmake
Centos7.5 installation and configuration MongoDB4.0.4
CentOS 7 installation and configuration PPTP
GitLab installation and basic use
Ubuntu PostgreSQL installation and configuration
2019-07-09 CentOS7 installation
centos7_1708 installation
Centos7 installation and configuration of Jenkins
Use Nginx and u under CentOS
Centos6.5 installation and deployment of KVM
CentOS7 installation and maintenance of Gitlab
CentOS7.3 install iptables and detailed use
CentOS 7.X system installation and optimization
Java-JDK installation and configuration under CentOS
Install and use docker under CentOS 6.8
CentOS 7 Tomcat service installation and configuration
CentOS7 install and use SQL Server
CentOS NTP server installation and configuration
CentOs7 installation and deployment Zabbix3.4 original
Erlang 20.2 installation and deployment under CentOS 7
Centos7 mysql database installation and configuration
Centos5 installation guide
CentOS 7 system installation and configuration graphic tutorial
Tomcat installation and configuration under CentOS 7 (Tomcat startup)
MySQL 8.0 installation, deployment and configuration under CentOS 6/7
Centos-6.5 installation and deployment of LNMP environment
Linux kernel compilation and CentOS system installation
Python - centos6 installation
Installation and configuration of redis under centos7
Centos7.6 operating system installation and optimization record
Centos7 installation and deployment of gitlab server
Docker installation (CentOS7 installation)
Installation and use of Win10 subsystem Ubuntu
Centos python3 compile installation and compile gcc upgrade
Zabbix installation and deployment and localization under CentOS
Centos7 hive stand-alone mode installation and configuration
CentOS7 installation zabbix 4.0 tutorial (graphics and text)
Jenkins installation and deployment tutorial under CentOS 7
CentOS install PostgreSQL 9.1
CentOS7 docker installation
Centos 7 install PostgreSQL
Centos7 installation and deployment of Airflow detailed
Installation and configuration of JDK in CentOS 7 system
How to install and use Docker on CentOS 7
CentOS 6.5 system installation and configuration graphic tutorial (detailed graphic)
How to install and use Composer on CentOS 8
CentOS 7 installation and configuration graphic tutorials under VMware10
Centos7 installation of PHP and Nginx tutorial detailed
How to install and use PostgreSQL on Ubuntu 16.04
Installation and configuration of CentOS 7 in VMware Workstation
How to install and use Curl on CentOS 8
MySQL 8.0 installation and deployment under CentOS, super detailed!
MySQL 8.0 installation, deployment and configuration tutorial on CentOS 8
Installation and use of SSH in Ubuntu environment