节点规划

准备3台安装并启动postgresql数据库的服务器

hostnameipport角色
citus0110.0.0.415432cn
citus0210.0.0.425432wn01
citus0310.0.0.435432wn02

1.安装citus

[root@citus01 ~]# cd /opt
[root@citus01 opt]# wget https://github.com/citusdata/citus/archive/refs/tags/v13.1.0.tar.gz
[root@citus01 opt]# tar xf citus-13.1.0.tar.gz
[root@citus01 opt]# cd citus-13.1.0
[root@citus01 citus-13.1.0]# ./configure
[root@citus01 citus-13.1.0]# make && make install

2.在数据库中创建citus扩展

## 所有节点执行

(1) 数据库配置文件中添加配置
[postgres@citus01 ~]$ vi $PGDATA/postgresql.conf
shared_preload_libraries = 'citus'

(2)重启数据库
[postgres@citus01 ~]$ pg_ctl restart

(3)在数据库中创建扩展
[postgres@citus01 ~]$ psql
psql (15.5)
Type "help" for help.

postgres=# CREATE EXTENSION citus;
CREATE EXTENSION

3.配置节点间通信

3.1 修改postgres用户密码

## 在所有节点执行
alter user postgres password 'Postgres@123';

3.2 修改访问控制文件

## 在所有节点执行
[postgres@citus01 ~]$ > $PGDATA/pg_hba.conf
[postgres@citus01 ~]$ vi $PGDATA/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             postgres                                scram-sha-256
# IPv4 local connections:
host    all             postgres        127.0.0.1/32            scram-sha-256
host    all             postgres        ::1/128                 scram-sha-256
# 节点间互通
host    all             postgres        10.0.0.0/24             scram-sha-256

[postgres@citus01 ~]$ pg_ctl reload

3.3 配置免密登录

## 在所有节点执行
[postgres@citus01 ~]$ vim ~/.pgpass
# hostname:port:database:username:password
localhost:5432:postgres:postgres:Postgres@123
10.0.0.41:5432:postgres:postgres:Postgres@123
10.0.0.42:5432:postgres:postgres:Postgres@123
10.0.0.43:5432:postgres:postgres:Postgres@123
                           
[postgres@citus01 ~]$ chmod 600 ~/.pgpass

4.在CN节点添加work节点

[postgres@citus01 ~]$ psql
postgres=# SELECT master_add_node('10.0.0.42',5432);
postgres=# SELECT master_add_node('10.0.0.43',5432);

postgres=# SELECT * FROM master_get_active_worker_nodes();
 node_name | node_port 
-----------+-----------
 10.0.0.43 |      5432
 10.0.0.42 |      5432

在这里插入图片描述

5.验证数据分布

postgres=# CREATE TABLE  t1(id bigint primary key,updatetime timestamp);
postgres=# SELECT * FROM create_distributed_table('t1','id');
postgres=# \d t1

postgres=# insert into t1 select generate_series(1,100),now();
查看执行计划
postgres=# explain select * from t1 where id=1;
postgres=# explain select * from t1 where id=2;

在这里插入图片描述

更多推荐