Skip to contentManaged Kafka is live: Apache Kafka on AWS for half the cost of MSK

PostgreSQL

Connect PlanetScale for Postgres as a source

Set up a PlanetScale for Postgres branch so Artie streams its changes, with a replication slot that survives switchovers and failovers.

When you create the pipeline, choose PostgreSQL as the source, then set Hosting provider to PlanetScale on Prepare source. Artie then shows the PlanetScale steps instead of the self-hosted ones. This page covers only what differs on PlanetScale. For partitioned tables, tables without primary keys and other settings, see the PostgreSQL overview.

Prerequisites

  • PostgreSQL 17 or later on the branch, which the failover slot needs.
  • The password for the branch’s default postgres role.
  • Access to the branch’s Clusters → Parameters in the PlanetScale console.
  • A window for a brief switchover, because changing wal_level restarts the cluster.

Enable logical replication

In the PlanetScale console, go to your branch’s Clusters → Parameters and set these parameters:

ParameterValue
wal_levellogical
sync_replication_slotson, which keeps the slot through a switchover or failover
hot_standby_feedbackon, for the same reason
max_slot_wal_keep_sizeSized for your change rate and free disk (PlanetScale suggests over 4GB)
max_replication_slotsAt least 2× the number of replicas and change data capture (CDC) consumers
max_wal_sendersAt least 2× the number of replicas and CDC consumers, and no less than max_replication_slots

max_slot_wal_keep_size has no limit by default, so an idle slot holds write-ahead log (WAL) until the disk fills. Both max_replication_slots and max_wal_senders need room for one more, because Artie uses one replication slot.

To confirm, run this as postgres:

SQL
SHOW wal_level;

The query returns logical.

Create a service account

Connect to the primary as the branch’s default postgres role on port 5432. Run this to create Artie’s user, give it replication access and grant read access to current and future tables in public:

setup.sql
CREATE USER artie WITH PASSWORD 'PASSWORD';
ALTER USER artie REPLICATION;

GRANT USAGE ON SCHEMA public TO artie;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO artie;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO artie;

Replace PASSWORD with a password for the user. Repeat the three grants for each other schema you replicate. Default privileges cover only tables created by the role that runs the statement. If another role creates your tables, add FOR ROLE TABLE_OWNER after ALTER DEFAULT PRIVILEGES, where TABLE_OWNER is that role.

  • Artie connects as artie.<branch_id>. The branch ID is the part after the dot in the default username, for example postgres.nk35mx55qq.
  • SQL-created roles don’t appear on the PlanetScale Settings → Roles page. A role made in PlanetScale can have REPLICATION only if it inherits postgres, so this SQL role is the least-privileged option.

Create the heartbeat table

Heartbeats keep the slot advancing on a quiet branch, which matters on PlanetScale because max_slot_wal_keep_size has no limit by default. As postgres, run this in the database you replicate:

heartbeats.sql
CREATE TABLE test_heartbeat_table (id text PRIMARY KEY, ts timestamp);
GRANT SELECT, UPDATE ON TABLE test_heartbeat_table TO artie;
INSERT INTO test_heartbeat_table (id, ts) VALUES ('1', NOW());

Grant to artie without the branch suffix. The SQL under the Heartbeats toggle names the login you enter in User, branch suffix included, so run this block instead. With heartbeats on, Artie updates the row every 5 minutes.

The table must be in the publication. A FOR ALL TABLES publication includes it; a table list must name it.

Create a publication

Artie reads changes through a publication, dbz_publication unless you pick another, which you create as postgres. Artie’s user can’t create it, because a FOR ALL TABLES publication needs a superuser. Without it, deployment fails with publication "dbz_publication" does not exist.

Once a table is in a publication, PostgreSQL rejects UPDATE and DELETE on it unless it has a primary key or replica identity. First, list the tables that would break:

check.sql
SELECT n.nspname AS table_schema, c.relname AS table_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
  AND c.relpersistence = 'p'
  AND n.nspname NOT IN ('pg_catalog', 'information_schema')
  AND n.nspname NOT LIKE 'pg_toast%'
  AND (
    c.relreplident = 'n'
    OR (c.relreplident = 'd' AND NOT EXISTS (
      SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisprimary))
    OR (c.relreplident = 'i' AND NOT EXISTS (
      SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisreplident))
  )
ORDER BY 1, 2;

If it returns tables, set REPLICA IDENTITY FULL on each (details) or publish only specific tables. On Which tables do you want to stream?, set the Merge key of each table without a primary key to the columns that identify a row. They must exactly match one of the table’s unique indexes. Without a merge key, Artie fails with partition key is not set.

Then create the publication and leave Publication mode on All tables:

publication.sql
CREATE PUBLICATION dbz_publication FOR ALL TABLES WITH (publish_via_partition_root = true);

This includes tables created later, so give every new table a primary key or replica identity.

Publish only specific tables

Create the publication with a table list instead, and include the heartbeat table if you use heartbeats:

SQL
CREATE PUBLICATION dbz_publication
  FOR TABLE public.orders, public.customers, test_heartbeat_table
  WITH (publish_via_partition_root = true);

Set Publication mode to Specific tables when you connect the source. If you already created the FOR ALL TABLES publication, drop it first with DROP PUBLICATION dbz_publication;. Otherwise deployment fails with please set the publication mode to 'all tables' to match the existing publication.

Add each new table to the publication before you add it to the pipeline:

SQL
ALTER PUBLICATION dbz_publication ADD TABLE public.refunds;

Create a failover-safe replication slot

A slot survives a PlanetScale switchover or failover only if it has failover = true. If PlanetScale finds a logical slot without it, PlanetScale notifies you and resumes planned failovers after three days.

Before you deploy, turn on Failover slot in Advanced options when you connect the source. Artie then creates its slot with failover. The setting doesn’t change a slot that already exists. Keep it on, so Artie recreates the slot with failover if the slot is ever dropped.

After deploying, check that failover is t:

SQL
SELECT slot_name, active, failover FROM pg_replication_slots WHERE slot_type = 'logical';

If it’s f, follow Repair a slot created without failover.

Create the slot yourself

Connected as artie.<branch_id>, run this before deploying:

slot.sql
SELECT pg_create_logical_replication_slot('artie', 'pgoutput', false, false, true); -- last arg: failover

If you set a Replication slot name, use it instead of artie. Keep Failover slot on as well.

Connect Artie to PlanetScale

The setup flow asks for the source in this order:

  1. On Choose your source, select PostgreSQL, then select Continue.

  2. On How would you like to connect your source?, choose SSH tunnel, Allowlist Artie IPs or PrivateLink.

  3. On Prepare your PostgreSQL database, set Hosting provider to PlanetScale, finish its tasks, then select Continue.

  4. On Connect PostgreSQL, enter the connection fields.

  5. Leave Publication blank to use dbz_publication, or pick your publication from the field’s suggestions.

  6. In Advanced options, turn on Failover slot, and Heartbeats if you created the heartbeat table.

  7. Select Verify source. When it shows Source verified, select Continue.

If you reuse a PostgreSQL connector that’s already set up, setup skips the network and prepare steps and shows them as done.

Connection fields

Connect PostgreSQL asks for these fields:

FieldValue
HostFrom the PlanetScale Connect page, for example nk35mx55qq-useast1-1.horizon.psdb.cloud
Port5432
Userartie.<branch_id>
PasswordThe password you set for artie
Database nameThe Postgres database your tables are in (postgres by default)

Picking a publication from Publication also sets Publication mode and Publish via partition root to match it. Publish via partition root is on by default and must match the publication, or the source fails validation. PlanetScale requires TLS, and Artie uses TLS whenever the server offers it, so no TLS setting is needed.

Change settings on an existing pipeline

To change the failover, heartbeat and publication settings on an existing pipeline, select Edit pipeline, then Source: PostgreSQL. Under Advanced settings → Postgres settings, they have these labels:

  • Use PostgreSQL 17 failover replication slot
  • Enable heartbeats
  • Publication mode, with All tables (recommended) or Filtered
  • Publish partitioned table changes via partition root (recommended)

Advanced

Repair a slot created without failover

Enable failover on an existing slot without dropping it (PlanetScale’s procedure). sync_replication_slots and hot_standby_feedback must be on first.

  1. Pause the pipeline

    On the pipeline page, select Pause pipeline…, choose Pause pipeline, then confirm. The default, Pause delivery only, keeps capturing changes. Don’t drop the slot, even though the dialog suggests it for a long pause. If the pipeline has its own source reader, this stops Artie reading from the source, so the slot is released. A shared source reader keeps running while another pipeline uses it. If its page under Settings > Source readers (available when shared source readers are on for your account) still offers Pause, select it, or use Update source reader status. That stops every pipeline on it.

  2. Open a replication connection as the default role

    Bash
    psql "host=nk35mx55qq-useast1-1.horizon.psdb.cloud port=5432 dbname=my_database user=postgres.nk35mx55qq replication=database sslmode=verify-full sslrootcert=system"
  3. Alter the slot

    SQL
    ALTER_REPLICATION_SLOT artie (FAILOVER true);

    If it hangs, a session is still attached. From a normal (non-replication) postgres connection, run:

    SQL
    SELECT pg_terminate_backend(active_pid) FROM pg_replication_slots WHERE slot_name = 'artie' AND active_pid IS NOT NULL;
  4. Verify and resume

    Confirm failover is t in pg_replication_slots, then select Resume pipeline. This also resumes a shared source reader you paused. Artie continues from its last acknowledged position.

What happens during a switchover

PlanetScale switches over for parameter changes that need a restart, resizes and upgrades, and fails over if the primary goes down. With the settings on this page, the slot moves to the new primary. Artie resumes from its last acknowledged position after a brief gap.

Replicate another branch

Each branch is an isolated database. A new branch has none of the parameters, SQL-created roles, heartbeat table or publication from these steps. Repeat these steps on that branch and create a separate Artie source with its credentials. A branch restored from a backup includes the backup’s data, so check for the role, heartbeat table and publication first.

Private connectivity

PlanetScale supports AWS PrivateLink, which Artie offers on Enterprise plans. Otherwise, Artie connects from its fixed IP addresses. If the database has IP restrictions, allow those addresses before you verify the source.

Was this page helpful?