简体   繁体   English

如何在 Google Cloud SQL (PostgreSQL) 中使用 SSL 证书创建逻辑复制订阅?

[英]How to create a logical replication subscription with SSL certificates in Google Cloud SQL (PostgreSQL)?

I would like to use CDC with PostgreSQL's logical replication feature to gather data from different databases in the same instance for a materialized view.我想将 CDC 与 PostgreSQL 的逻辑复制功能一起使用,以从同一实例中的不同数据库收集数据以实现物化视图。 However, I cannot get this to work with SSL.但是,我不能让它与 SSL 一起使用。

To test the logical replication, I wrote a zsh script that takes care of the publication and subscription.为了测试逻辑复制,我编写了一个负责发布和订阅的 zsh 脚本。

HOST=?
PORT=?
DB_NAME=?

SSL_CERT=?/client-cert.pem
SSL_KEY=?/client-key.pem
SSL_ROOT_CERT=?/root.crt

SU_PASSWORD=
SU_USER="postgres"

R_USER=?
R_PASSWORD=?

PUBLICATION_NAME="stock_publication"
SUBSCRIPTION_NAME="stock_subscription"
# CONNECTION_INFO="host=$HOST port=$PORT dbname=$DB_NAME \
#     user=$R_USER password=$R_PASSWORD"
CONNECTION_INFO="host=$HOST port=$PORT dbname=$DB_NAME \
    sslmode=allow sslcert=$SSL_CERT sslkey=$SSL_KEY \
    user=$R_USER password=$R_PASSWORD"

exec_query() {
    # Execute a query with SSL connection

    PGPASSWORD=$R_PASSWORD \
    psql "sslmode=allow \
    sslcert=$SSL_CERT \
    sslkey=$SSL_KEY \
    hostaddr=$HOST \
    port=5432 user=$R_USER dbname=$DB_NAME" \
    --command=$1
}

# exec_query
exec_query "DROP PUBLICATION IF EXISTS $PUBLICATION_NAME;"
exec_query "CREATE PUBLICATION $PUBLICATION_NAME FOR TABLE t1, t2, ...;"
exec_query "DROP SUBSCRIPTION IF EXISTS $SUBSCRIPTION_NAME;"
exec_query "CREATE SUBSCRIPTION $SUBSCRIPTION_NAME CONNECTION '$CONNECTION_INFO' PUBLICATION $PUBLICATION_NAME;"

However, creating the subscription fails due to SSL restrictions.但是,由于 SSL 限制,创建订阅失败。 So I consequently added the required SSL mode and certificates, but that raised another error.因此,我添加了所需的 SSL 模式和证书,但这引发了另一个错误。

DROP PUBLICATION
CREATE PUBLICATION
NOTICE:  subscription "stock_subscription" does not exist, skipping
DROP SUBSCRIPTION
ERROR:  certificate is not allowed
DETAIL:  Non-superusers cannot use certificate in the connection setting.

The error surprises me, since Google Cloud SQL does not offer support for the super user role.这个错误让我很吃惊,因为 Google Cloud SQL 不支持超级用户角色。 Instead, the 'cloudsqlsuperuser' role exists.相反,存在“cloudsqlsuperuser”角色。 This role is not the same as a traditional PostgreSQL super user, so how am I supposed to mitigate this issue?这个角色与传统的 PostgreSQL 超级用户不同,那么我应该如何缓解这个问题呢?

For as far as I know, there is not an alternative way to pass the SSL certificates to the 'CREATE SUBSCRIPTION' statement.据我所知,没有其他方法可以将 SSL 证书传递给“CREATE SUBSCRIPTION”语句。

I guess that I can try Apache Debezium for CDC instead, but using Postgres' built-in CDC support seems much simpler to me.我想我可以尝试使用 Apache Debezium 代替 CDC,但是使用 Postgres 的内置 CDC 支持对我来说似乎要简单得多。 This is, for streaming data from one Postgres instance to another.这是为了将数据从一个 Postgres 实例流式传输到另一个实例。

As you know,Cloud SQL is a managed service,Please read the documentation for more information on the replication and configuration settings.如您所知,Cloud SQL 是一项托管服务,请阅读文档以获取有关复制和配置设置的更多信息。
Please check and make sure that you have completed all the prerequisite steps as mentioned on the public docs [3] before running the command.在运行命令之前,请检查并确保您已完成公共文档 [3] 中提到的所有先决条件步骤。 Kindly try the following suggestions and let me know the result.请尝试以下建议并让我知道结果。

By following the guide [1] but without a SSL flag and unchecking the 'Allow only SSL connections' on the instance, were you able to successfully run the command.And at the same time, on the command, you can set the flag sslmode=require in the create subscription call so the subscriber connection to the primary is always SSL-encrypted.通过遵循指南 [1] 但没有 SSL 标志并取消选中实例上的“仅允许 SSL 连接”,您是否能够成功运行命令。同时,在命令上,您可以设置标志 sslmode =require 在创建订阅调用中,因此订阅者与主节点的连接始终是 SSL 加密的。 You may also check the documentation for SuperUser [2].您还可以查看 SuperUser [2] 的文档。

[1] https://cloud.google.com/sql/docs/postgres/replication/configure-logical-replication [1] https://cloud.google.com/sql/docs/postgres/replication/configure-logical-replication
[2] https://cloud.google.com/sql/docs/postgres/users#superuser_restrictions [3] https://cloud.google.com/sql/docs/postgres/replication/configure-logical-replication#setting-up-logical-replication-with-external-replica [2] https://cloud.google.com/sql/docs/postgres/users#superuser_restrictions [3] https://cloud.google.com/sql/docs/postgres/replication/configure-logical-replication#setting -up-logical-replication-with-external-replica

I was using psql to connect to my Postgres instance.我正在使用 psql 连接到我的 Postgres 实例。 Instead, I should have used gcloud sql connect while running the Cloud SQL Proxy.相反,我应该在运行 Cloud SQL 代理时使用gcloud sql connect Google Cloud SQL manages SSL connections for you with gcloud sql connect , so that removes the need for passing certificates if setup properly. Google Cloud SQL 使用gcloud sql connect为您管理 SSL 连接,因此如果设置正确,则无需传递证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM