El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Elastic Cloud Server/ Best Practices/ Setting Up an Application/ Setting Up Master-Slave Replication on PostgreSQL

Setting Up Master-Slave Replication on PostgreSQL

Updated on 2024-07-31 GMT+08:00

What Is PostgreSQL?

PostgreSQL is an open source object-relational DBMS (ORDBMS) with an emphasis on extensibility and standards compliance. It applies to business-oriented online transaction processing (OLTP) scenarios and supports NoSQL (JSON, XML, or hstore) and geographic information system (GIS) data types. It has won a good reputation in reliability and data integrity, and applies widely to Internet websites, location-based applications, and complex data object processing.

This section helps you use Huawei Cloud ECSs to set up PostgreSQL.

Preparations

Configuring the Master Node

  1. Run the following commands to install PostgreSQL on the master node:

    # yum update -y

    # yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

    # yum install postgresql11-server

    # yum install postgresql11

    # /usr/pgsql-11/bin/postgresql-11-setup initdb

    # systemctl enable postgresql-11

    # systemctl start postgresql-11

  2. Run the following command to switch to the default user postgres:

    # su - postgres

  3. Run the following command to enter the database:

    # psql

  1. Run the following command to create an account and assign permissions to it:

    create role Username login replication encrypted password 'Password'

    NOTE:

    The password in the preceding command must be enclosed in single quotation marks.

    Assume the username is dbar and the password is xxxxx. Run the following command:

    create role dbar login replication encrypted password 'xxxxx';

  2. Run the following command to open configuration file /var/lib/pgsql/11/data/pg_hba.conf:

    # vim /var/lib/pgsql/11/data/pg_hba.conf

    Add the following content to the file:
    host all all 192.168.1.0/24 md5 #Allows for MD5 password authentication connection in the VPC network segment.
    host replication dbar IP address of the slave database/24 md5 #Allows for data replication from the master database to the slave database.
  3. Run the following command to open file /var/lib/pgsql/11/data/postgresql.conf:

    # vim postgresql.conf

    Add the following content to the file:

    wal_level = hot_standby
    max_wal_senders= 6
    wal_sender_timeout = 60s
    max_connections = 512 #The max_connections value of the slave database must be greater than that of the master database.
    archive_command= 'cp %p /var/lib/pgsql/11/data/archivelog/%f'
    wal_keep_segments=10240
    archive_mode = on
    listen_addresses= xxx.xx.xx.xx
  4. Run the following command to restart PostgreSQL:

    # systemctl restart postgresql-11

Configuring the Slave Node

  1. Run the following commands to install PostgreSQL on the slave node:

    # yum update -y

    # yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

    # yum install postgresql11-server

    # yum install postgresql11

  2. Run the following commands to copy the configuration file from the master node:

    # pg_basebackup -h IP address of the master node -U dbar -D /var/lib/pgsql/11/data -X stream -P

    # cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf

  3. Run the following command to modify the recovery.conf file:

    # vim recovery.conf

    standby_mode = on  # This node is used as the slave database.
    primary_conninfo = 'host=IP address of the master node  port=5432 user=dbar password=xxxxx (Do not enclose the password in single quotation marks.)
    trigger_file = '/var/lib/pgsql/11/data/trigger.kenyon'    #Trigger file for master/slave switchover
    recovery_target_timeline = 'latest' 
    restore_command = 'cp /var/lib/pgsql/11/data/archivelog/%f %p'
    archive_cleanup_command = 'pg_archivecleanup /var/lib/pgsql/11/data/archivelog %r' #Clear outdated archives.
  4. Run the following command to modify the postgresql.conf file:

    # chown -R postgres.postgres /var/lib/pgsql/11/data

  5. Add the following content to the /var/lib/pgsql/11/data/postgresql.conf file.

    listen_addresses= xxx.xx.xx.xx

    max_connections = 600

  6. Run the following commands to start PostgreSQL and enable it to start automatically upon ECS startup:

    #systemctl enable postgresql-11

    #systemctl start postgresql-11

Verifying Master-Slave Replication

  1. Run the following command to check whether process sender runs on the master node:

    # ps aux |grep sender

  2. Run the following command to check whether process receiver runs on the slave node:

    # ps aux | grep receiver

  3. Run the following commands to check whether the status of the slave database can be viewed from the master database:

    # su - postgres

    -bash-4.2# psql

    replication=# select * from pg_stat_replication;

  1. Create a database from the master database and check whether the newly created database is synchronized to the slave database.
    1. Run the following commands to create a database from the master database:

      postgres=# create database testdb;

      postgres=# \l

    2. Run the following command to check whether the newly created database is synchronized to the slave database.

      postgres=# \l

Utilizamos cookies para mejorar nuestro sitio y tu experiencia. Al continuar navegando en nuestro sitio, tú aceptas nuestra política de cookies. Descubre más

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback