探索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', '[email protected]');

检查数据

bash 复制代码
select * from accounts;

user_id | username | password | email

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

0 | fangliang | pwd | [email protected]

(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 │ [email protected]

└─────────┴───────────┴──────────┴───────────────────┘

1 row in set. Elapsed: 0.012 sec.

参考资料

相关推荐
一只爱撸猫的程序猿1 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc
nanzhuhe1 小时前
sql中group by使用场景
数据库·sql·数据挖掘
消失在人海中2 小时前
oracle sql 语句 优化方法
数据库·sql·oracle
Clang's Blog2 小时前
一键搭建 WordPress + MySQL + phpMyAdmin 环境(支持 PHP 版本选择 & 自定义配置)
数据库·mysql·php·wordpr
zzc9212 小时前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
未来之窗软件服务2 小时前
JAVASCRIPT 前端数据库-V1--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟数据库
LjQ20402 小时前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
烙印6012 小时前
MyBatis原理剖析(二)
java·数据库·mybatis
RestCloud2 小时前
如何通过ETLCloud实现跨系统数据同步?
数据库·数据仓库·mysql·etl·数据处理·数据同步·集成平台
你是狒狒吗2 小时前
TM中,return new TransactionManagerImpl(raf, fc);为什么返回是new了一个新的实例
java·开发语言·数据库