Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the configuration method of Failover in PostgreSQL

Shulou Source: shulou.com Published: 2022-05-31 15:23:46 10月03日 Update

This article introduces the relevant knowledge of "what is the configuration method of Failover in PostgreSQL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The libpq version of PostgreSQL 10 + provides Seamless Application Failover function. When connecting to PG, you can specify multiple target libraries and specify target_session_attrs attributes (read-write, any, read-only). Libpq can distribute / switch to different databases according to the status of the target library.

For example, for example, there are two PG instances on 26.28Drex5433 and 26.26 5432, both of which can be read and written externally. The configuration of Failover is as follows:

1.read-write/any

[xdb@localhost] $psql 'postgres://192.168.26.26:5432192.168.26.28:5433/postgres?target_session_attrs=read-write'-U xdb-c "select inet_server_addr ()" Timing is on.Expanded display is used automatically. Inet_server_addr-192.168.26.26 (1 row) Time: 2.439 ms [xdb@localhost] $

Connect to the first matching instance. Turn off pg on 26.26th, reconnect, and automatically switch to pg on 26.28th.

[xdb@localhost ~] $pg_ctl stopwaiting for server to shut down.... Doneserver stopped [xdb@localhost] $[xdb@localhost] $psql 'postgres://192.168.26.26:5432192.168.26.28:5433/postgres?target_session_attrs=read-write'-U xdb-c "select inet_server_addr ()" Timing is on.Expanded display is used automatically. Inet_server_addr-192.168.26.28 (1 row) Time: 0.837 ms [xdb@localhost] $

Build a master-slave replication environment, with one master library and one backup database, which are 26.28 and 26.25, respectively.

[xdb@localhost] $psql 'postgres://192.168.26.25:5432192.168.26.28:5432/postgres?target_session_attrs=read-write'-c "select inet_server_addr ()"-U pg12Password for user pg12: inet_server_addr-192.168.26.28 (1 row) [xdb@localhost] $psql' postgres://192.168.26.25:5432192. 168.26.28UR 5432 Universe Postgresses targeting targets sessions at row any'- c "select inet_server_addr ()"-U pg12Password for user pg12: inet_server_addr-192.168.26.25 (1 row)

Using the read-write option automatically connects to the master library, while using the any option gives priority to connecting to the slave library in order.

2.read-only

Change the connection properties to something else, such as read-only

[xdb@localhost] $psql 'postgres://192.168.26.26:5432192.168.26.28:5433/postgres?target_session_attrs=read-only'-U xdb-c "select inet_server_addr ()" psql: invalid target_session_attrs value: "read-only" [xdb@localhost ~] $

Invalid attribute values are prompted. In fact, only read-write and any options are supported.

This is the end of the content of "what is the method of configuring Failover in PostgreSQL". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Configuration two instances properties methods content reality more goals knowledge switching different practical successful learning next master and slave examples functions dilemmas multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi MariaDB Linux Microsoft