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
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
On this page

Deploying PostgreSQL

Updated on 2024-12-09 GMT+08:00

Introduction

PostgreSQL is an open-source, highly-stable database system that provides SQL features, such as foreign keys, subqueries, triggers, and different user-defined data types and their functions. It further enhances the SQL language and provides some fine-grained capabilities to scale data workloads. It is mainly used in mobile, network, geospatial, and analysis applications and is known as the most advanced open source database in the industry. This tutorial describes how you can deploy PostgreSQL in HCE 2.0.

Preparations

  • Prepare two ECSs and assign a public IP address or EIP to each ECS.
  • Ensure that inbound security group rules allow traffic to flow to the ECSs over port 5432.

Procedure

  1. Configure the master PostgreSQL node.

    1. Run the following command to install the server, client, and related components:
      dnf install postgresql postgresql-contrib postgresql-server
    2. Run the following command to initialize the database:
      postgresql-setup --initdb --unit postgresql
    3. Run the following commands in sequence to start PostgreSQL and check the PostgreSQL status:
      systemctl start postgresql
      systemctl status postgresql

      If active (running) is displayed, PostgreSQL is started.

      NOTE:

      To set PostgreSQL to automatically enable upon system boot, run the following commands:

      systemctl enable postgresql
      systemctl daemon-reload
    4. Run the following command to log in to the database as user postgres:
      su - postgres
    5. Execute the following statement to access the psql terminal:
      psql
    6. Execute the following statement to create an account and set the password and permission:
      CREATE ROLE replica login replication ENCRYPTED PASSWORD 'replicationxxx!';

      replica is the account name, and replicationxxx! is the password. replica has the login and replication permissions.

    7. Execute the following statement to view the created account:
      SELECT usename FROM pg_user;

      Information similar to the following is displayed.

    8. Execute the following statement to view the permissions:
      SELECT rolname FROM pg_roles;

      Information similar to the following is displayed.

    9. Execute the following statement to exit the psql terminal:
      \q
    10. Execute the following statement to exit user postgres:
      exit
    11. Edit the /var/lib/pgsql/data/pg_hba.conf file to configure the replica user whitelist.

      Find IPv4 local connections and add the following information:

      host    all             all             <IPv4 address range of the slave mode>          md5
      host    replication     replica        <IPv4 address range of the slave mode>          md5

      Enter the IPv4 address range of the slave node.

    12. Open the /var/lib/pgsql/data/postgresql.conf file and configure the following parameters:
      listen_addresses = '*' # Private IP address listened on
      max_connections = 100 # Maximum number of connections. The value of max_connections of the slave node must be greater than that of the master node.
      wal_level = hot_standby # Enable the hot standby mode.
      synchronous_commit = on # Enable synchronous replication.
      max_wal_senders = 32 # Maximum number of synchronization processes
      wal_sender_timeout = 60s # Timeout for the streaming replication host to send data
    13. Run the following command to restart PostgreSQL:
      systemctl restart postgresql

  2. Configure the slave PostgreSQL node.

    1. Run the following command to install the server, client, and related components on the slave node:
      dnf install postgresql postgresql-contrib postgresql-server
    2. Run the following command to create a backup directory on the slave node:
      pg_basebackup -D /var/lib/pgsql/data -h <IP address of the master node> -p 5432 -U replica -X stream -P -R

      -X indicates the required WAL stream. -P indicates the progress. -h indicates the IP address of the master node. -p indicates the port number of the master node. -U indicates the account for logging in to the master node. -R indicates that the replication configuration is automatically written.

      If information similar to the following is displayed, the backup is successful.

      The standby.signal file is generated in the /var/lib/pgsql/data directory, and information about the master database connections is automatically written into the postgresql.auto.conf file.

    3. Run the following command to modify the user group that the directory belongs to:
      chown -R postgres.postgres /var/lib/pgsql/data
    4. Run the following command to restart PostgreSQL:
      systemctl start postgresql

  3. Check whether the configuration is successful.

    1. Run the following command on the slave node to check whether the node has become the slave node:
      sudo -u postgres psql -c "SELECT pg_is_in_recovery()"

      If t is displayed, the node has become the slave node.

    2. Run the following command on the master node to obtain the slave node information:
      sudo -u postgres psql -x -c "SELECT * FROM pg_stat_replication" -d postgres

      If information similar to the following is displayed, the configuration is successful.

CAUTION:

The password and permission configurations are used only for tests. Exercise caution when using them in the service environment.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback