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

探索ClickHouse——使用MaterializedPostgreSQL同步PostgreSQL数据库

安装PostgreSQL

sudo apt install postgresql

修改配置

sudo vim /etc/postgresql/14/main/postgresql.conf 

解开并修改wal_level 的配置项

wal_level = logical

重启服务

/etc/init.d/postgresql restart

Restarting postgresql (via systemctl): postgresql.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart ‘postgresql.service’.
Multiple identities can be used for authentication:

  1. fangliang
  2. , (kafka)

Choose identity to authenticate as (1-2): 1
Password:
==== AUTHENTICATION COMPLETE ===
.

设置密码

切换用户

sudo su - postgres

使用下面命令进入PostgreSQL命令行交互页面

psql

输入下面命令设置密码为postgres_pwd

\password postgres

Enter new password for user “postgres”:
Enter it again:

创建数据库和表

使用下面命令创建数据库

CREATE DATABASE test_db;
\c test_db
CREATE TABLE accounts(user_id INT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(50) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL);

数据同步

开启一个新的窗口,使用下面指令进入交互页面

clickhouse-client

创建表

CREATE TABLE postgresql_replica(user_id UInt64, username String, password String, email String) ENGINE = MaterializedPostgreSQL('127.0.0.1:5432', 'test_db', 'accounts', 'postgres', 'postgres_pwd') PRIMARY KEY user_id;

CREATE TABLE postgresql_replica
(
user_id UInt64,
username String,
password String,
email String
)
ENGINE = MaterializedPostgreSQL(‘127.0.0.1:5432’, ‘test_db’, ‘accounts’, ‘postgres’, ‘postgres_pwd’)
PRIMARY KEY user_id
Query id: 16f796bc-e979-47b4-8f1c-33471cfd7b12
0 rows in set. Elapsed: 0.013 sec.
Received exception from server (version 23.7.5):
Code: 1001. DB::Exception: Received from localhost:9000. DB::Exception: pqxx::sql_error: ERROR: logical decoding requires wal_level >= logical
. (STD_EXCEPTION)

数据库写入数据

在psql的交互页面输入

INSERT INTO accounts(user_id, username, password, email) VALUES(0, 'fangliang', 'pwd', 'fangliang@123.com');

检查数据

select * from accounts;

user_id | username | password | email
---------±----------±---------±------------------
0 | fangliang | pwd | fangliang@123.com
(1 row)

ClickHouse同步数据

在clickhouse-client交互页面输入

select * from postgresql_replica;

SELECT *
FROM postgresql_replica
Query id: eef3fc8e-82ce-4e69-bf0b-0c2afe047daf
┌─user_id─┬─username──┬─password─┬─email─────────────┐
│ 0 │ fangliang │ pwd │ fangliang@123.com │
└─────────┴───────────┴──────────┴───────────────────┘
1 row in set. Elapsed: 0.012 sec.

参考资料

  • https://clickhouse.com/docs/zh/engines/table-engines/integrations/materialized-postgresql
http://www.lryc.cn/news/187671.html

相关文章:

  • 《向量数据库指南》——向量数据库 有必要走向专业化吗?
  • 你必须知道的数据查询途径!!
  • 火焰原子吸收光谱法、容量法和电感耦合等离子体发射光谱法
  • 亚马逊云科技 2023 柏林峰会主题演讲总结
  • CentOS Stream9 安装远程桌面服务 Xrdp
  • 实施运维01
  • MySQL大表直接复制文件的copy方式
  • Redis-集群
  • 使用CrawlSpider爬取全站数据。
  • 【JUC】Java并发编程从挖坑到入土全解(4-一文讲通LockSupport与线程中断->长图预警)
  • Springboot学习笔记——3
  • jupyter 切换虚拟环境
  • 如何在Apache和Resin环境中实现HTTP到HTTPS的自动跳转:一次全面的探讨与实践
  • 安全防御—密码学
  • 灯具从深圳寄国际物流到墨西哥
  • spark3使用hive zstd压缩格式总结
  • 直线导轨精度等级在设备中有什么影响?
  • windows平台FairMOT的实现
  • 系统架构设计:12 论软件维护方法及其应用
  • SS命令使用介绍
  • 让你的对象变得拗口:JSON.stringify(),我把对象夹进了 JSON 魔法帽!
  • TDengine时序数据库学习使用
  • 算法通过村第十三关-术数|青铜笔记|数字与数学
  • 【SpringMVC篇】详解SpringMVC入门案例
  • Programming abstractions in C阅读笔记:p166-p175
  • 【List-Watch】
  • Pytorch因nn.Parameter导致实验不可复现的一种情况
  • MySQL表名区分不区分大小写,规则是怎样
  • Design patterns--观察者模式
  • 【Spring Boot】SpringBoot 单元测试