随笔 - 366  文章 - 0  评论 - 101  阅读 - 30万

PostgreSql 主备同步 数据库备份 pgsql

  数据库备份和恢复 (Backup and Restore)   参考上面View Code

 

数据主备搭建

psql (PostgreSQL) 9.2.24
PGDATA=/var/lib/pgsql/data
systemctl start postgresql

systemctl enable postgresql

You can now start the database server using:

/usr/bin/postgres -D /var/lib/pgsql/data
or
/usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start

 


#admin用户创建用户
-bash-4.2$ psql
psql (9.2.24)
Type "help" for help.

postgres=# create user replica login replication encrypted password 'replica';
CREATE ROLE

#ambari登录再查
psql -U ambari
select * from pg_user;

修改主配置文件postgresql.conf
修改主配置文件pg_hba.conf
systemctl restart postgresql.service


su - postgres
psql -U ambari
Password for user ambari:ambari
#查当前已连接的数据库
\c ambari
#给ambari授权
grant all privileges on database ambari to ambari;
#列出所有表
\d
select * from users;(ambari界面登录的用户)
hosts 主机列表

 

备库192.168.159.12
安装好数据库,不要启动
rm -rf /var/lib/pgsql/data/*

主库192.168.159.11
root

vi postgresql.conf

wal_level = hot_standby

max_wal_senders = 10
archive_mode=on
archive_command = 'cp %p /var/lib/pgsql/data/pg_bak/%f'

vi pg_hba.conf

host all all 192.168.159.11/32 trust
host replication all 192.168.159.12/32 trust
host replication replica 192.168.159.12/32 md5
host replication replica 192.168.159.11/32 trust


systemctl restart postgresql.service


su - postgres
-bash-4.2$ psql
postgres=# create user replica login replication encrypted password 'replica';
postgres=# ALTER USER replica with encrypted password '123456';
mkdir -pv /tmp/bak1111
pg_basebackup -h 192.168.159.11 -p 5432 -U replica -F p -x -P -D /tmp/bak1111
scp -r /tmp/bak1111/*  192.168.159.12:/var/lib/pgsql/data

备库192.168.159.12
chown -R postgres: /var/lib/pgsql/data
vi postgresql.conf 参数修改 (hot_standby = on   其他参数同主库,不用改)

hot_standby = on

cp /usr/share/pgsql/recovery.conf.sample /var/lib/pgsql/data/
cp /usr/share/pgsql/recovery.conf.sample /var/lib/pgsql/data/recovery.conf
vi /var/lib/pgsql/data/recovery.conf

  


standby_mode = on
primary_conninfo = 'host=192.168.159.11 user=replica password=123456'

 

主库进程walsender,参考输出
[root@hdp1 data]# ps -ef | grep "wal" | grep -v "grep"
postgres 57233 57228 0 14:31 ? 00:00:00 postgres: wal writer process
select pg_is_in_recovery();
# 如果返回t 说明是备库,返回f 说明是主库。

 

#配置重载
[root@hdp1 ~]# su - postgres
Last login: Sun Nov 19 14:52:14 CST 2023 on pts/1
-bash-4.2$ pg_ctl reload -D $PGDATA
server signaled

 

users
hosts

 参考 https://www.cnblogs.com/52shaidan/p/17198309.html

https://blog.csdn.net/weixin_33860147/article/details/92944588

https://www.cnblogs.com/warmingsun/p/6738840.html

https://www.modb.pro/db/647647

https://codeantenna.com/a/vyfdgucZqP

https://blog.51cto.com/u_16175447/6799619

https://www.cnblogs.com/ezgod/p/15843221.html

 

 

PostgreSQL中 drop table指令出现ERROR: cannot drop table userinfo because

other objects depend on it

DROP TABLE  hosts CASCADE;

posted on   寒星12345678999  阅读(202)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2014-12-17 grub
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示