Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the configuration method of Failover in PostgreSQL

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report