Postgresql 14.5 master-slave configuration

Table of contents

Main library configuration

Configure from library

Additional: Script to execute pgsql commands

If you don’t know how to install pgsql, go to my pgsql column to check the installation

Main library configuration

1. Create a user who is responsible for syncing from the library (optional). If you don’t want to create a dedicated user, you can use the login user of your main library

CREATE ROLE testuser LOGIN PASSWORD '123456';

2. Add an entity to the last line of the pg_hba.conf file under the data directory. I am here to allow all users and all IPs to copy the main library. If you want to limit it, you can modify it yourself

host    replication     all             0.0.0.0/0           trust

3. Add configuration to the postgresql.conf file in the data directory

wal_level = replica
listen_addresses = '*'
max_wal_senders = 10

 popular science

The WAL (Write-Ahead Logging) mechanism of PgSQL is used to ensure the data consistency and durability of the database. wal_level is used to specify the detail level and record content of WAL log. 

  • minimal: The minimum logging level, only records necessary information in the WAL log, such as transaction start and end, etc. This setting is suitable for scenarios that have high requirements for WAL log size but low requirements for data recovery speed.
  • replica: replication level, in addition to recording minimal level log information, it will also record all operations that change data, including DDL and DML statements. This setting is suitable for use in the master-standby replication scenario, and can ensure that the standby database is consistent with the main database.
  • logical: Logical replication level. In addition to recording log information at the replica level, it also records information related to logical replication, such as metadata required by logical replication streams. This setting is suitable for use in scenarios such as logical backup and data synchronization.

4. Restart the main Postgresql service 

Configure from library

1. Create a new data directory. If the data directory has been initialized, delete this directory. You need to shut down pgsql before deleting

2. Back up the main database

pg_basebackup -D data directory -h ${ip} -p ${port} -U ${username} -X stream -P

 3. Modify the data directory and all file permissions under the directory

chown -R username data directory/*

 4. Add postgresql.conf configuration from the library

max_connections = 1000 #Must be greater than the maximum number of connections of the main library
recovery_target_timeline = 'latest'
primary_conninfo = 'host=${ip} port=${port} user=${username} password=${pass}' hot_standby =
on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on

 5. Create a new standby.signal in the data storage directory

standby_mode = on

6. Switch the user to start the slave library

7. Test, operate in the main library to see if the slave library changes

Additional: Script to execute pgsql commands

#!/bin/bash

# Configure database user
PGUSER="username"

# Command
command="psql statement"

# Set the content of the .pgpass file
echo "ip:port:database:username:password" > ~/.pgpass
chmod 0600 ~/.pgpass

# Execute the command to create a database without manually entering the password
/usr/local/pgsql/bin/psql -U $PGUSER -h ip -p port -c "$command" database
 

Guess you like

Origin blog.csdn.net/wai_58934/article/details/130882410