探索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.

参考资料

相关推荐
stark张宇1 小时前
MySQL 核心内幕:从索引原理、字段选型到日志机制与外键约束,一篇打通数据库任督二脉
数据库·mysql·架构
倔强的石头_1 小时前
融合数据库架构实践:关系型、JSON与全文检索的“一库多能”深度解析
数据库
星辰员3 小时前
KingbaseES数据库:ksql 命令行用户与权限全攻略,从创建到删除
数据库
华仔啊17 小时前
千万别给数据库字段加默认值 null!真的会出问题
java·数据库·后端
随风飘的云2 天前
MySQL的慢查询优化解决思路
数据库
IvorySQL2 天前
PostgreSQL 技术日报 (3月7日)|生态更新与内核性能讨论
数据库·postgresql·开源
赵渝强老师2 天前
【赵渝强老师】金仓数据库的数据文件
数据库·国产数据库·kingbase·金仓数据库
stark张宇2 天前
构建第一个AI聊天机器人:Flask+DeepSeek+Postgres实战
人工智能·postgresql·flask
随逸1772 天前
《Milvus向量数据库从入门到实战,手把手搭建语义检索系统》
数据库
神秘的猪头2 天前
🚀 React 开发者进阶:RAG 核心——手把手带你玩转 Milvus 向量数据库
数据库·后端·llm