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

参考资料

相关推荐
一 乐21 小时前
个人健康系统|健康管理|基于java+Android+微信小程序的个人健康系统设计与实现(源码+数据库+文档)
android·java·数据库·vue.js·spring boot·生活
q***239221 小时前
【SQL技术】不同数据库引擎 SQL 优化方案剖析
数据库·sql
老华带你飞1 天前
医疗保健|医疗养老|基于Java+vue的医疗保健系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·医疗保健
Leon-Ning Liu1 天前
Oracle 19C 数据字典 DBA_HIST_SEG_STAT 详细说明
数据库·oracle·dba
⑩-1 天前
苍穹外卖Day(1)
java·数据库·spring boot·spring·java-ee·mybatis
朝新_1 天前
【统一功能处理】从入门到源码:拦截器学习指南(含适配器模式深度解读)
数据库·后端·mybatis·适配器模式·javaee
我要升天!1 天前
QT-- 理解项目文件
开发语言·数据库·qt
冉冰学姐1 天前
SSM基于WEB的教学质量评价系统的设计与实现p9ak6(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·教学质量评价·多角色管理、
liliangcsdn1 天前
sql中left join和inner join的区别
数据库·sql
l1t1 天前
DeepSeek辅助编写转换DuckDB json格式执行计划到PostgreSQL格式的Python程序
数据库·python·postgresql·json·执行计划