0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

配置发布

学习在主服务器上定义发布,以指定要复制的表和操作。

配置发布 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Publications?

Welcome! In PostgreSQL logical replication, a publication is like a channel on the master server that broadcasts data changes.

It defines what data (which tables) and which operations (INSERTs, UPDATEs, DELETEs, TRUNCATEs) should be replicated to subscribing servers.

Think of it as setting up a TV station: you decide which shows to broadcast and when.

Basic Publication Setup

Creating a publication is straightforward using the CREATE PUBLICATION command. Let's start with the simplest form.

This creates a publication that doesn't publish any tables yet, but it's ready to be configured.

CREATE PUBLICATION my_first_pub;

Publishing All Tables

If you want to replicate all tables in your current database (and any new ones created later), you can use the FOR ALL TABLES clause.

This is handy for replicating an entire database schema.

CREATE PUBLICATION all_db_changes FOR ALL TABLES;

Publishing Specific Tables

Often, you only need to replicate a subset of tables. You can specify these tables directly using the FOR TABLE clause.

This publication will only broadcast changes from the users and products tables.

CREATE PUBLICATION sales_data_pub FOR TABLE users, products;

Dynamic vs. Static

There's a key difference between FOR ALL TABLES and listing tables with FOR TABLE:

  • FOR ALL TABLES: Dynamically includes all current and future tables in the database.
  • FOR TABLE table1, table2: Statically lists specific tables. New tables must be added manually.

Choose based on your replication needs!

Replicating Specific Operations

Publications can be configured to replicate only certain types of data modifications. By default, INSERT, UPDATE, DELETE, and TRUNCATE are replicated.

You can use the WITH (publish = '...') clause to select specific operations. Here, we only replicate INSERT statements.

CREATE PUBLICATION new_orders_only FOR TABLE orders WITH (publish = 'insert');

Combining Operations

To replicate multiple, but not all, operation types, you can list them separated by commas within the publish option.

This publication will replicate INSERT, UPDATE, and DELETE operations, but not TRUNCATE.

CREATE PUBLICATION user_activity FOR TABLE user_logs WITH (publish = 'insert, update, delete');

Listing Publications

Want to see what publications are active on your server? PostgreSQL stores this information in its system catalogs.

You can query the pg_publication view to get details about each publication, including what operations it publishes.

SELECT pubname, puballtables, pubinsert, pubupdate, pubdelete, pubtruncate FROM pg_publication;

Managing Publications

You can modify an existing publication to add or remove tables, or even drop it entirely if it's no longer needed.

  • ALTER PUBLICATION ADD TABLE: Add tables.
  • ALTER PUBLICATION DROP TABLE: Remove tables.
  • DROP PUBLICATION: Delete a publication.
ALTER PUBLICATION sales_data_pub ADD TABLE accounts;
ALTER PUBLICATION sales_data_pub DROP TABLE products;
DROP PUBLICATION sales_data_pub;

Publication Configuration Quiz

Which of the following statements correctly configure a publication in PostgreSQL?

Recap: Configuring Publications

Great job! You've learned how to define publications in PostgreSQL.

  • Publications broadcast data changes for logical replication.
  • Use CREATE PUBLICATION to define them.
  • You can publish FOR ALL TABLES or FOR TABLE specific_tables.
  • The WITH (publish = '...') clause lets you select specific operations (insert, update, delete, truncate).
  • You can manage them with ALTER PUBLICATION and DROP PUBLICATION.

Next, we'll learn how to set up subscriptions to receive these changes!

常见问题解答

「配置发布」课时是免费的吗?

是的 — 「配置发布」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「配置发布」这节课中我会学到什么?

学习在主服务器上定义发布,以指定要复制的表和操作。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「配置发布」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 逻辑复制基础
  2. 配置发布
  3. 管理订阅
  4. 逻辑复制中的冲突解决
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication