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

安装PostgreSQL

bash 复制代码
sudo apt install postgresql

修改配置

bash 复制代码
sudo vim /etc/postgresql/14/main/postgresql.conf 

解开并修改wal_level 的配置项

wal_level = logical

重启服务

bash 复制代码
/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 ===

.

设置密码

切换用户

bash 复制代码
sudo su - postgres

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

bash 复制代码
psql

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

bash 复制代码
\password postgres

Enter new password for user "postgres":

Enter it again:

创建数据库和表

使用下面命令创建数据库

bash 复制代码
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);

数据同步

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

bash 复制代码
clickhouse-client

创建表

bash 复制代码
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的交互页面输入

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

检查数据

bash 复制代码
select * from accounts;

user_id | username | password | email

---------±----------±---------±------------------

0 | fangliang | pwd | fangliang@123.com

(1 row)

ClickHouse同步数据

在clickhouse-client交互页面输入

bash 复制代码
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.

参考资料

相关推荐
ai_coder_ai26 分钟前
编写自动化脚本,在自己后端服务中使用云原生Baas服务进行设备相关操作
数据库·云原生·自动化
oradh39 分钟前
Oracle DG主备架构的基本维护操作总结
数据库·oracle·架构·dg主备架构的基本维护操作总结
xieliyu.1 小时前
MySQL 六大基础约束详解:NOT NULL/DEFAULT/UNIQUE/ 主键 / 外键 / CHECK
android·数据库·sql·mysql
JSON_L2 小时前
MongoDB文档关系详解
数据库·mongodb
卓怡学长2 小时前
w268基于springboot + vue 物流系统
java·数据库·vue.js·spring boot·spring·intellij-idea
CodeStats2 小时前
《源纹天书》第一百四十一章至第一百四十五章:etcd升级、2PC的阻塞困境、3PC的超时突破、TCC的补偿之道、Saga的最终选择!
java·大数据·数据库·etcd·源纹天书
.Cnn2 小时前
Redis基础day02
java·数据库·redis
能有时光2 小时前
[MAF的Agent管道详解-07]利用AIAgent中间件构建Agent管道
java·数据库
迷枫7123 小时前
简单sql与dm功能实验记录
数据库·sql·oracle
蚁库3 小时前
Oracle 19c 文件系统安装实战教程:从规划到日常管理完整流程
数据库·oracle