安装PostgreSQL
代码语言:javascript复制sudo apt install postgresql
修改配置
代码语言:javascript复制sudo vim /etc/postgresql/14/main/postgresql.conf
解开并修改wal_level 的配置项
wal_level = logical
重启服务
代码语言:javascript复制/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:
- fangliang
- , (kafka)
Choose identity to authenticate as (1-2): 1 Password: ==== AUTHENTICATION COMPLETE === .
设置密码
切换用户
代码语言:javascript复制sudo su - postgres
使用下面命令进入PostgreSQL命令行交互页面
代码语言:javascript复制psql
输入下面命令设置密码为postgres_pwd
代码语言:javascript复制password postgres
Enter new password for user “postgres”: Enter it again:
创建数据库和表
使用下面命令创建数据库
代码语言:javascript复制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);
数据同步
开启一个新的窗口,使用下面指令进入交互页面
代码语言:javascript复制clickhouse-client
创建表
代码语言:javascript复制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,
数据库写入数据
在psql的交互页面输入
代码语言:javascript复制INSERT INTO accounts(user_id, username, password, email) VALUES(0, 'fangliang', 'pwd', 'fangliang@123.com');
检查数据
代码语言:javascript复制select * from accounts;
user_id | username | password | email ---------±----------±---------±------------------ 0 | fangliang | pwd | fangliang@123.com (1 row)
ClickHouse同步数据
在clickhouse-client交互页面输入
代码语言:javascript复制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