当前位置: 首页 > news >正文

部署PG一主一从

1.准备数据库安装包
二进制包。解压
tar xvf postgresql.tar.gz
mkdir -p /data/pgsql/12/{archive,data,log} #归档目录 ,数据目录 ,日志文件目录

2.主备参数文件
vi postgresql.conf

listen_addresses = '0.0.0.0'
port = 5432
max_connections = 100
unix_socket_directories = '.'
superuser_reserved_connections = 10ssl = off
tcp_keepalives_idle = 60
tcp_keepalives_interval = 10
tcp_keepalives_count = 10#02 - Memory-Resource
#shared_buffers
shared_buffers = 1GB
maintenance_work_mem = 256MB
autovacuum_work_mem = 256MB
temp_buffers = 64MB
work_mem = 5592kB
huge_pages = try
dynamic_shared_memory_type = posixbgwriter_delay = 10ms
effective_io_concurrency = 0
max_worker_processes = 20
max_parallel_workers = 20
old_snapshot_threshold = 3h#03 - WAL-Checkpoint-Archiving
wal_level = replica
wal_log_hints = on
wal_buffers = 16MB
synchronous_commit = offmin_wal_size = 2GB
max_wal_size = 1GB
checkpoint_timeout = 30min
checkpoint_warning = 60s
checkpoint_completion_target = 0.9archive_mode = always
archive_command = 'test ! -f /data/pgsql/12/archive/%f && cp %p /data/pgsql/12/archive/%f'
archive_timeout = 1800#04 - Replication
max_wal_senders = 32
max_replication_slots = 10
wal_sender_timeout = 600s
track_commit_timestamp = on#synchronous_standby_names = ''
hot_standby = on
hot_standby_feedback = on#05 - Reporting-Logging
#log_destination = 'csvlog'
log_destination = 'stderr'
logging_collector = on
log_directory = '/data/pgsql/12/log'
log_filename = 'postgresql-%m-%d.log'
log_line_prefix = '%t:%r:%u@%d:[%p]: '
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_timezone = 'Asia/Shanghai'log_min_duration_statement = 1000
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_statement = 'ddl'
log_replication_commands = on
log_temp_files = -1
cluster_name = 'kong-cluster'#06 - QueryTuning-Statistics-Autovacuum
effective_cache_size = 1GB
default_statistics_target = 200
track_activities = on
track_counts = on
track_io_timing = on
track_functions = pl
track_activity_query_size = 10240autovacuum = on
autovacuum_max_workers = 5
log_autovacuum_min_duration = 1000#07 - Lock
deadlock_timeout = 5s
max_locks_per_transaction = 1024#08 - Client Connection
timezone = 'Asia/Shanghai'
datestyle = 'iso, mdy'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'#09 - pg_stat_statements-auto_explain
shared_preload_libraries = 'pg_stat_statements,auto_explain'
pg_stat_statements.max = 1000
pg_stat_statements.track = top
pg_stat_statements.track_utility = true
pg_stat_statements.save = true
auto_explain.log_min_duration = 10s
auto_explain.log_nested_statements = on
auto_explain.log_analyze = true
auto_explain.log_verbose = true
auto_explain.log_timing = true
auto_explain.log_buffers = true
auto_explain.log_format = json
primary_conninfo = 'application_name=kong1 user=repl password=repl host=1x2.x1.1x.19 port=5432 sslmode=disable sslcompression=1 target_session_attrs=any'

vi pg_hba.conf

local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trusthost replication repl 0.0.0.0/0 md5
host all all 0.0.0.0/0 md5

3.初始化2个节点。

su - postgres
vi .bash_profile

PATH=PATH:PATH:PATH:HOME/.local/bin:$HOME/bin:/data/pgsql/12/bin
export PATH
export PGDATA=/data/pgsql/12/data

–初始化数据。两个节点都初始化。
initdb -D /postgresql/data -E UTF8 --lc-collate=C --lc-ctype=en_US.utf8 -U postgres

创建复制用。
psql

create role repuser login encrypted password ‘repl’ replication;

4.删除从库,重做从库。
pg_ctl stop
rm -rf /data/

pg_basebackup -h 1x2.x1.1x.19 -p 5432 -U repl -W -X stream -F p -P -R -D /data/pgsql/12/data -l backup20250401

–启动从库。
pg_ctl start

5.检查主从是否同步 。

–主库检查

[root@host-1x2-x1-1x-19 ~]# ps -ef |grep postgres
postgres 14594 1 0 Mar26 ? 00:00:24 /data/pgsql/12/bin/postgres
postgres 14595 14594 0 Mar26 ? 00:00:05 postgres: kong-cluster: logger
postgres 14597 14594 0 Mar26 ? 00:00:00 postgres: kong-cluster: checkpointer
postgres 14598 14594 0 Mar26 ? 00:02:55 postgres: kong-cluster: background writer
postgres 14599 14594 0 Mar26 ? 00:00:09 postgres: kong-cluster: walwriter
postgres 14600 14594 0 Mar26 ? 00:00:12 postgres: kong-cluster: autovacuum launcher
postgres 14601 14594 0 Mar26 ? 00:00:01 postgres: kong-cluster: archiver last was 00000001000000000000000B # 数据库处于归档状态。
postgres 14602 14594 0 Mar26 ? 00:00:42 postgres: kong-cluster: stats collector
postgres 14603 14594 0 Mar26 ? 00:00:00 postgres: kong-cluster: logical replication launcher
postgres 65430 14594 0 11:53 ? 00:00:00 postgres: kong-cluster: walsender repl 1x2.x1.1x.41(54282) streaming 0/C000000 #有一个从库正在进行流复制。
postgres 66064 14594 0 13:38 ? 00:00:00 postgres: kong-cluster: kong kong 1x2.x1.1x.186(64780) idle
postgres 66068 14594 0 13:39 ? 00:00:00 postgres: kong-cluster: kong kong 1x2.x1.1x.1x6(4183) idle
postgres 66077 14594 0 13:41 ? 00:00:00 postgres: kong-cluster: kong kong 1x2.x1.1x.186(41246) idle
postgres 66081 14594 0 13:42 ? 00:00:00 postgres: kong-cluster: kong kong 1x2.x1.1x.1x6(10129) idle
postgres 66082 14594 0 13:42 ? 00:00:00 postgres: kong-cluster: kong kong 1x2.x1.1x.186(35745) idle
root 66100 66084 0 13:42 pts/0 00:00:00 grep --color=auto postgres

从库查看

[postgres@host-1x2-x1-1x-41 ~]$ ps -ef |grep postgres
postgres 40043 1 0 11:53 ? 00:00:00 /data/pgsql/12/bin/postgres
postgres 40044 40043 0 11:53 ? 00:00:00 postgres: kong-cluster: logger #集群的名称叫 Kong-cluster
postgres 40045 40043 0 11:53 ? 00:00:00 postgres: kong-cluster: startup recovering 00000001000000000000000B #正在恢复状态。
postgres 40046 40043 0 11:53 ? 00:00:00 postgres: kong-cluster: checkpointer
postgres 40047 40043 0 11:53 ? 00:00:03 postgres: kong-cluster: background writer
postgres 40048 40043 0 11:53 ? 00:00:00 postgres: kong-cluster: archiver last was 00000001000000000000000A
postgres 40049 40043 0 11:53 ? 00:00:00 postgres: kong-cluster: stats collector
postgres 40050 40043 0 11:53 ? 00:00:08 postgres: kong-cluster: walreceiver streaming 0/C000000 #正在接收wal日志。

–从库日志查看 。

2025-04-01 11:53:41 CST:😡:[40045]: LOG: entering standby mode #备库模式。
2025-04-01 11:53:41 CST:😡:[40045]: LOG: redo starts at 0/A000028
2025-04-01 11:53:41 CST:😡:[40045]: LOG: consistent recovery state reached at 0/A000138 #恢复位置。
2025-04-01 11:53:41 CST:😡:[40043]: LOG: database system is ready to accept read only connections
2025-04-01 11:53:41 CST:😡:[40050]: LOG: started streaming WAL from primary at 0/B000000 on timeline 1
2025-04-01 11:56:31 CST:[local]:[unknown]@[unknown]:[40057]: LOG: connection received: host=[local]
2025-04-01 11:56:31 CST:[local]:postgres@postgres:[40057]: LOG: connection authorized: user=postgres database=postgres application_name=psql
2025-04-01 11:56:40 CST:[local]:[unknown]@[unknown]:[40058]: LOG: connection received: host=[local]
2025-04-01 11:56:40 CST:[local]:postgres@kong:[40058]: LOG: connection authorized: user=postgres database=kong application_name=psql
2025-04-01 11:56:40 CST:[local]:postgres@postgres:[40057]: LOG: disconnection: session time: 0:00:08.670 user=postgres database=postgres host=[local]
2025-04-01 11:56:47 CST:[local]:postgres@kong:[40058]: LOG: disconnection: session time: 0:00:07.108 user=postgres database=kong host=[local]
2025-04-01 12:23:41 CST:😡:[40046]: LOG: restartpoint starting: time
2025-04-01 12:23:43 CST:😡:[40046]: LOG: restartpoint complete: wrote 0 buffers (0.0%); 1 WAL file(s) added, 0 removed, 0 recycled; write=0.027 s, sync=0.000 s, total=1.924 s; sync files=0, longest=0.000 s, average=0.000 s; distance=16384 kB, estimate=16384 kB
2025-04-01 12:23:43 CST:😡:[40046]: LOG: recovery restart point at 0/B000060

–主库里面查看复制的从库。

[postgres@host-1x2-x1-1x-19 ~]$ psql
psql (12.3)
Type “help” for help.

postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
--------------±-----------
1x2.x1.1x.41 | async
(1 row)

postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state | reply_time
-------±---------±--------±-----------------±-------------±----------------±------------±------------------------------±-------------±----------±----------±----------±----------±-----------±----------±----------±-----------±--------------±-----------±------------------------------
65430 | 16386 | repl | kong-cluster | 1x2.x1.1x.41 | | 54282 | 2025-04-01 11:53:41.839507+08 |854 | streaming | 0/C000000 | 0/C000000 | 0/C000000 | 0/C000000 | | | | 0 | async| 2025-04-01 13:50:13.664137+08 #恢复时间。接近当前时间表示同步,与当前时间相差很远可能不同步。
(1 row)

http://www.dtcms.com/a/347426.html

相关文章:

  • 使用Proxifier+vmware碰到的一些问题
  • GEO优化服务商:AI时代数字经济的新引擎——解码行业发展与技术创新实践
  • 蔬菜溯源系统的开发与设计小程序
  • 【基础-判断】使用http模块发起网络请求时,必须要使用on(‘headersReceive’)订阅请求头,请求才会成功。
  • 算法第五十五天:图论part05(第十一章)
  • 微服务架构中服务发现机制的实现与优化 NO.3(回复征途黯然.)
  • Navigation2 源码阅读 —— map_server 到底干了哈?
  • 53.Redis持久化-RDB
  • 【golang】制作linux环境+golang的Dockerfile | 如何下载golang镜像源
  • ESP-NOW详解(esp-idf)
  • NFC 电路理论计算
  • 支持电脑课程、游戏、会议、网课、直播录屏 多场景全能录屏工具
  • Python 学习(十六) 下一代 Python 包管理工具:UV
  • 1.十天通关常见算法100题(第一天)
  • 嵌入式LINUX-------------数据库
  • html-docx-js 导出word
  • Redis 从入门到精通:原理、实战与性能优化全解析
  • MySQL InnoDB Buffer Pool详解:原理、配置与性能优化
  • 单元测试的使用以及Vue3-Element Plus入门
  • 【假设微调1B模型,一个模型参数是16bit,计算需要多少显存?】
  • 雷卯针对香橙派Orange Pi 3G-IoT-B开发板防雷防静电方案
  • 结合 Kernel Memory 与 Ollama 实现文档处理与智能问答
  • 51单片机-实现外部中断模块教程
  • 力扣hot100 | 图论 | 200. 岛屿数量、994. 腐烂的橘子、207. 课程表、208. 实现 Trie (前缀树)
  • 【数据分享】2025年全国路网矢量数据道路shp数据
  • Ubuntu 系统中解压 ZIP 文件可以通过图形界面或命令行操作
  • 【设计模式08】组合模式
  • LLaMA-Factory 中配置文件或命令行里各个参数的含义
  • 深度集成Dify API:基于Vue 3的智能对话前端解决方案
  • Maven仓库与Maven私服架构