Set up a PostgreSQL destination¶
The Replicator Service writes to a PostgreSQL database through an ODBC driver on the Stelo VM. It creates the destination tables and their indexes, loads baselines and snapshots, and applies incremental changes with SQL.
Supported configurations¶
| Supported | |
|---|---|
| PostgreSQL | PostgreSQL, and AlloyDB and EDB (EnterpriseDB), which are handled as PostgreSQL. For managed services (such as Amazon RDS or Aurora, Azure Database for PostgreSQL or Google Cloud SQL), ask Stelo Support. |
| Driver on the Stelo VM | psqlODBC, the PostgreSQL ODBC driver (recommended), or the SQDR bundled PostgreSQL driver (installed and licensed with SQDR). See ODBC drivers. |
| Snapshot replication | Yes. |
| Incremental replication | Yes, from any source with a Capture Agent. |
| Apply methods | Batch and Sequence; Merge with PostgreSQL 15 or later, the first version with MERGE (see Choosing the apply method). |
| Authentication | Remote Credentials: a PostgreSQL role with a password. |
| Encryption | SSL/TLS: SSLMode with psqlODBC, EncryptionMethod=1 with the bundled driver. See Connection strings. |
Docker
The SQDR container includes both drivers (see ODBC drivers in the container).
Prepare the database¶
The database administrator creates the database if needed (CREATE DATABASE mydb;), the role the Replicator Service
connects as, and the schema for the destination tables. In PostgreSQL, only a table's owner can alter, index or
drop it, so the simplest setup is a role that owns the schema, and so the tables the Replicator Service creates in it.
The privileges follow from the SQL the Replicator Service runs (see
What the Replicator Service runs on the database).
Create the role and the schema¶
Connect to the destination database as a superuser (or a role that can create roles and schemas for them), and run:
CREATE ROLE sqdruser WITH LOGIN PASSWORD 'mypassword';
GRANT CONNECT, TEMPORARY ON DATABASE mydb TO sqdruser;
CREATE SCHEMA myschema AUTHORIZATION sqdruser;
Replace sqdruser, mypassword, mydb and myschema with yours. If you name the schema like the role
(CREATE SCHEMA sqdruser AUTHORIZATION sqdruser), you can keep the destination's default Object Schema, the user
ID.
What each privilege is for: Why these privileges. For a schema or tables another role owns, see Tables in a schema the role doesn't own. How names are cased: Names and search_path.
Network¶
The Stelo VM must reach the PostgreSQL server's port, 5432 by default, and pg_hba.conf must accept the role's
connections from the Stelo VM's address (with hostssl to require SSL/TLS).
Create the destination¶
Create the destination as described in Create a destination, with these choices:
-
ODBC driver: install psqlODBC, the 64-bit driver from the PostgreSQL project, on the Stelo VM (see ODBC drivers). The SQDR bundled PostgreSQL driver, installed with SQDR, also works.
Docker
The SQDR container includes psqlODBC and the SQDR bundled PostgreSQL driver.
-
Name: choose the DBMS Type PostgreSQL.
-
Connection: choose ODBC Driver and the driver, then the Template for PostgreSQL, and fill in the fields (see Connection string).
-
Authentication: Remote Credentials with the role and its password. Click Verify.
- Tick Advanced to check the defaults: on the Default step, Object Schema is the schema where new
subscriptions create their tables; set it to
myschemaunless the schema has the role's name. The tables go in the connection string's database. See Defaults.
Test the connection. Verify tests the connection, not the privileges, which are first used when a
subscription runs. When you create subscriptions, you can test them first with a snapshot subscription of a small
table. Its replication events show each step and any
PostgreSQL error; permission denied for schema means the role can't create tables there.
Connection string¶
| Driver | Connection string | Notes |
|---|---|---|
| psqlODBC | Servername=myhost;Database=mydatabase;Port=5432;MaxVarcharSize=32767;MaxLongVarcharSize=214748364 |
Keep the size options from the template: they set the largest character types the driver reports. TLS: add SSLMode=require, or SSLMode=verify-ca;pqopt={sslrootcert=C:\\certs\\root.pem} to validate the certificate. |
| SQDR bundled PostgreSQL driver | HostName=myhost;Database=mydatabase;PortNumber=5432 |
For destinations the Replicator Service adds BatchMechanism=2, FetchTSWTZasTimestamp=1, MaxCharSize=255, MaxVarcharSize=32767 and MaxLongVarcharSize=214748364. TLS: add EncryptionMethod=1 (EM=1); ValidateServerCertificate=0 (VSC=0) skips validation, or VSC=1;TrustStore=C:\certs\root.pem validates it; CryptoProtocolVersion=TLSv1.2 (CPV) requires TLS 1.2. |
Group settings for PostgreSQL¶
When you create an incremental group to this destination, choose the DML Replication method (see Apply):
- Batch (the default) and Sequence work with every PostgreSQL version.
- Merge needs PostgreSQL 15 or later, the first version with
MERGE. The Data Replicator Manager offers it for any version, so check the version first (SELECT version();): on earlier versions every merge fails. It's the fastest method for high volumes (see Choosing the apply method) and needs a key on every member (see Tables without a usable key).
Check the destination¶
After the first baseline of the incremental group (for snapshots, after the first run):
- Group status. On the Groups page, the group shows Actively monitoring and applying change data (see
Group status). If a member failed, its
replication events show the PostgreSQL error;
permission denied for schemameans the role can't create tables there. -
Rows at the destination. Compare a table's row count with the source:
SELECT COUNT(*) FROM myschema.mytable; -
Changes. Change a few rows at the source; they're applied with the group's next poll (the Poll Interval; see Scheduling). Query the rows again to see them.
What the Replicator Service runs on the database¶
The privileges in Prepare the database follow from these statements:
| When | Statements |
|---|---|
| A subscription is saved or a baseline starts (depending on its Destination DDL Options) | DROP TABLE and CREATE TABLE for the destination table, named with its schema. |
| Before a baseline or snapshot copies the data | TRUNCATE TABLE, or, with Delete existing data before replication, DELETE FROM ... WHERE ctid IN (SELECT ctid FROM ... LIMIT n) in batches (see Baseline Replication Options). |
| During the copy | INSERT, with ODBC parameter arrays. |
| Around the copy | CREATE INDEX and DROP INDEX for the index on the replication key (suffix ir) and the source indexes you replicate; ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY and DROP CONSTRAINT for Allow UPSERT members and replicated primary keys; ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY ... DEFERRABLE for replicated foreign keys. See The key at the destination. |
| Applying changes with Batch or Sequence | INSERT, UPDATE ... WHERE <key> and DELETE ... WHERE <key>, one row at a time. |
| Applying changes with Merge | CREATE TEMPORARY TABLE SQDRWT_<id> (...), a multi-row INSERT into it, then one MERGE INTO <table> ... WHEN MATCHED ... THEN DELETE ... WHEN MATCHED ... THEN UPDATE ... WHEN NOT MATCHED ... THEN INSERT. The temporary table belongs to the connection, which the Replicator Service closes after each table. See How Merge works. |
| With Merge In Place | A permanent merge table next to the destination table (or in the schema of the Decoration), created, emptied with TRUNCATE TABLE and dropped like a destination table. |
| ALTER processing | ALTER TABLE ... ADD COLUMN, ALTER COLUMN ... SET DATA TYPE, ALTER COLUMN ... SET NOT NULL or DROP NOT NULL, DROP COLUMN ... CASCADE and RENAME COLUMN. ADD Copy loads a work table SQDRCP_<id> (a temporary table, or a regular table next to the destination table when the group allows checkpoint/restart) and updates the destination table from it. |
Data types¶
The Replicator Service maps the source columns to the types the driver reports; see Data type mapping and its Per-database notes. For PostgreSQL:
- Character data. Source character columns are treated as Unicode, and Character data destination precision
heuristic inflates Unicode columns too.
charcolumns are created asbpchar, long text astext. With the useMaxVARCHAR service property,varcharcolumns are created without a length, so UTF-8 data never exceeds the column. - Binary data. BLOBs map to
bytea. - Spatial data. With PostGIS, SQL Server and PostgreSQL
geometryandgeographycolumns keep their types (see Spatial and hierarchyid columns). - Identity columns. The Replicator Service doesn't create identity columns or sequences, and inserts the source's
values. A table you create yourself with a
GENERATED ALWAYS AS IDENTITYcolumn on a replicated column rejects those values.
Limitations¶
- Merge needs PostgreSQL 15 or later. On earlier versions the
MERGEstatement fails: use Batch. - Merge needs a key. The
MERGEmatches rows on the key columns; members without one are applied row by row with inserts only. - Allow disable/enable indexes has no effect on PostgreSQL (SQL Server and Oracle only).
- Truncate. PostgreSQL doesn't truncate a table that another table's foreign key references; use Delete existing data before replication for such tables.
- Replicated foreign keys are created
DEFERRABLE.
Troubleshooting¶
Problems and messages, with their causes and fixes, are in Troubleshooting › PostgreSQL.
Related¶
- More about preparing a PostgreSQL destination
- Prepare a PostgreSQL database (as a source)
- PostgreSQL documentation: Privileges, Schemas, MERGE