Postgres-12版本数据库主从模式部署实现数据迁移_pg 主从 不同版本(1)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

说明:
wal_level 指定写入到wal中的信息,默认是minimal,只写从crash中恢复或者快速shutdown需要的信息。
#replica 是在minimal的基础上添加wal archiving需要的信息。
#logical 增加逻辑编码需要的信息。minimal wal不包含从base backup和wal log重新构建数据库数据的信息。replica或者logical可以。老版本的参数archive或者hot_standby 在这里被映射到replica模式

  • 修改postgres数据的密码
[postgres@docker-slave-79101 ~]$ psql
psql (12.3)
Type "help" for help.

postgres=# alter user postgres with password 'postgres';
ALTER ROLE
postgres=# select \* from pg\_shadow;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |               passwd                | valuntil | useconfig 
----------+----------+-------------+----------+---------+--------------+-------------------------------------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | md53175bce1d3201d16594cebf9d7eb3f9d |          | 

- 重启数据库

 su - postgres -c "pg\_ctl -D /PG/12/ restart"

从节点上操作

- 从主节点上获取数据

pg_basebackup -h 192.168.79.101 -p 5432 -U rsyn -Fp -Xs -Pv -R -D /PG/12

  • -h –指定作为主服务器的主机。
    -D –指定数据目录。
    -U –指定连接用户。
    -P –启用进度报告。
    -v –启用详细模式。
    -R–启用恢复配置的创建:创建一个standby.signal文件,并将连接设置附加到数据目录下的postgresql.auto.conf。
    -X–用于在备份中包括所需的预写日志文件(WAL文件)。流的值表示在创建备份时流式传输WAL。
    -C –在开始备份之前,允许创建由-S选项命名的复制插槽。
    -S –指定复制插槽名称。

- 修改配置文件:standby.signal和postgresql.conf

[postgres@docker-slave-79102 12]$ vim standby.signal 
[postgres@docker-slave-79102 12]$ cat standby.signal 
standby_mode = 'on'

[postgres@docker-slave-79102 12]$ vim postgresql.conf
#增加以下配置(/搜索关键词,进行修改,避免配置重复):
primary_conninfo = 'host=192.168.79.101 port=5432 user=rsyn password=rsyn'
recovery_target_timeline = latest # 同步最新的数据
#从节点连接数要大于主节点
max_connections = 500 
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on

#启动数据库
[postgres@docker-slave-79102 12]$ pg_ctl start -D /PG/12 
waiting for server to start....2020-08-02 17:18:38.773 CST [13105] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2020-08-02 17:18:38.774 CST [13105] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-08-02 17:18:38.774 CST [13105] LOG:  listening on IPv6 address "::", port 5432
2020-08-02 17:18:38.787 CST [13105] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-08-02 17:18:38.819 CST [13105] LOG:  redirecting log output to logging collector process
2020-08-02 17:18:38.819 CST [13105] HINT:  Future log output will appear in directory "log".

验证:

主节点:

[postgres@docker-slave-79101 12]$ psql
psql (12.3)
Type "help" for help.

postgres=# select client\_addr,sync\_state from pg\_stat\_replication;
  client_addr   | sync_state 
----------------+------------
 192.168.79.102 | async
(1 row)

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 smart     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=# create database test
postgres-# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 smart     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=# create database test;
CREATE DATABASE
postgres=# 
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 smart     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 smart     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

test=# CREATE TABLE Person(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   TALL           CHAR(50),
   WEIGHT         REAL
);
CREATE TABLE
test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
test-# VALUES(2,'张三',18,185,60);
ERROR:  syntax error at or near ",18,185"
LINE 2: VALUES(2,'张三',18,185,60);
                       ^
test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
VALUES(2,'张三',18,185, 60);
ERROR:  syntax error at or near ",18,185"
LINE 2: VALUES(2,'张三',18,185, 60);
                       ^
test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
VALUES(2,'张三',18,185, 60);
INSERT 0 1
test=# 
test=# 
test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
(1 row)

test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
test-# VALUES(2,'李四',18,185, 60);
ERROR:  duplicate key value violates unique constraint "person\_pkey"
DETAIL:  Key (id)=(2) already exists.
test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
VALUES(3,'李四',18,185, 60);
INSERT 0 1
test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
  3 | 李四 |  18 | 185                                                |     60
(2 rows)

test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
  3 | 李四 |  18 | 185                                                |     60
(2 rows)

test=# \du Person
           List of roles
 Role name | Attributes | Member of 
-----------+------------+-----------

test=# \du Person;
           List of roles
 Role name | Attributes | Member of 
-----------+------------+-----------

test=# \tPerson;
invalid command \tPerson;
Try \? for help.
test=# \t Person;
unrecognized value "Person" for "tuples\_only": Boolean expected
test=# 
test=# 
test=# 
test=# 
test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
  3 | 李四 |  18 | 185                                                |     60
(2 rows)

test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
test-# VALUES(1,'王五',18,'185cm','60');
INSERT 0 1
test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
  3 | 李四 |  18 | 185                                                |     60
  1 | 王五 |  18 | 185cm                                              |     60
(3 rows)

test=# 
test=# select \* from Person;
 id | name | age |                        tall                        | weight 
----+------+-----+----------------------------------------------------+--------
  2 | 张三 |  18 | 185                                                |     60
  3 | 李四 |  18 | 185                                                |     60
  1 | 王五 |  18 | 185cm                                              |     60
(3 rows)

test=# 


从节点

[postgres@docker-slave-79102 12]$ ps -ef | grep postgres
root      12177   1820  0 16:14 pts/1    00:00:00 su - postgres
postgres  12178  12177  0 16:14 pts/1    00:00:01 -bash
postgres  13105      1  0 17:18 ?        00:00:00 /opt/postgres/bin/postgres -D /PG/12
postgres  13106  13105  0 17:18 ?        00:00:00 postgres: logger   
postgres  13107  13105  0 17:18 ?        00:00:00 postgres: startup   recovering 00000001000000000000000D
postgres  13108  13105  0 17:18 ?        00:00:00 postgres: checkpointer   
postgres  13109  13105  0 17:18 ?        00:00:00 postgres: background writer   
postgres  13110  13105  0 17:18 ?        00:00:00 postgres: stats collector   
postgres  13111  13105  1 17:18 ?        00:00:00 postgres: walreceiver   streaming 0/D000148
postgres  13112  12178  0 17:18 pts/1    00:00:00 ps -ef
postgres  13113  12178  0 17:18 pts/1    00:00:00 grep --color=auto postgres
[postgres@docker-slave-79102 12]$ 
[postgres@docker-slave-79102 12]$ vim pg_hba.conf 
[postgres@docker-slave-79102 12]$ pg_ctl reload -D /PG/12 
server signaled
[postgres@docker-slave-79102 12]$ psql
WARNING: password file "/home/postgres/.pgpass" has group or world access; permissions should be u=rw (0600) or less
psql (12.3)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 smart     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=# lc test
postgres-# \c test
WARNING: password file "/home/postgres/.pgpass" has group or world access; permissions should be u=rw (0600) or less
You are now connected to database "test" as user "postgres".
test-# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 
test-# VALUES(2,'李四',18,185, 60);
ERROR:  syntax error at or near "lc"
LINE 1: lc test
        ^
test=# INSERT INTO Person( ID, NAME, AGE, TALL, WEIGHT) 


### 最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

### 资料预览

给大家整理的视频资料:

![](https://img-blog.csdnimg.cn/img_convert/bb48696f9d6cdf8951504a5c47dc3c3e.png)

给大家整理的电子书资料:

  

![](https://img-blog.csdnimg.cn/img_convert/e9ee28cfe49c1fa77cc64cf720b55775.png)



**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618635766)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

子书资料:

  

[外链图片转存中...(img-ocSl36Ct-1715870132757)]



**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618635766)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值