PGSync Documentation
repository·main·Indexed 23 days ago
https://github.com/toluaina/pgsyncA real-time Change Data Capture (CDC) tool that synchronizes data from PostgreSQL, MySQL, and MariaDB to Elasticsearch or OpenSearch. PGSync uses JSON schemas to automate the denormalization of relational data into searchable documents via logical replication (WAL/Binlog). It includes a daemon for continuous replication, a bootstrap CLI for environment setup, an es_mapping tool for NGram analyzers, and a parallel_sync CLI for high-throughput initial synchronization.
What's inside PGSync
- PGSync is a change data capture (CDC) tool designed to sync data from PostgreSQL, MySQL, or MariaDB to Elasticsearch or OpenSearch in real-time. It allows you to maintain your relational database as the single source of truth while powering search engines with denormalized, nested documents. You define your document structure using a JSON schema, and PGSync automatically handles the complex JOINs and data propagation via logical replication (WAL/Binlog).
Define a PGSync schema
mainA schema is a JSON file that maps database tables to document structures. You can define which columns to include and specify
childrento automatically denormalize related tables into nested documents.Example Schema:
{ "table": "book", "columns": ["isbn", "title", "description"], "children": [{ "table": "author", "columns": ["name"] }] }This schema will produce documents like:
{ "isbn": "9785811243570", "title": "Charlie and the Chocolate Factory", "authors": ["Roald Dahl"] }How PGSync works and its core components
mainPGSync is a middleware designed to synchronize data from PostgreSQL to Elasticsearch or OpenSearch. It allows you to maintain PostgreSQL as your primary source of truth while exposing denormalized, structured documents in a search engine.
Core Components
- PostgreSQL: The source relational database. PGSync uses PostgreSQL's logical decoding to capture real-time changes.
- Redis/Valkey: Acts as a message broker to manage sync state, queues, checkpointing, and locking.
- Elasticsearch/OpenSearch: The destination search engine where documents are indexed.
- PGSync Daemon: The core service that orchestrates the process by reading changes from PostgreSQL and indexing them into the search engine.
Data Flow
- Schema Definition: Users provide a
schema.jsonto map PostgreSQL tables to search engine documents. - Bootstrap: PGSync initializes the necessary replication slots and triggers in PostgreSQL.
- Change Capture: The daemon listens for inserts, updates, and deletes via logical decoding.
- Transformation: Data is transformed into structured documents based on the provided schema.
- Indexing: Documents are sent to Elasticsearch/OpenSearch in near real-time.
Configure PGSync using a JSON schema
mainTo map PostgreSQL tables to Elasticsearch/OpenSearch documents, you must define a JSON schema (schema.json). This schema handles the mapping and transformations required to turn relational rows into structured, denormalized documents. PGSync supports complex nested relationships and joins through this schema-based approach.Configure MySQL or MariaDB for binary logging
mainTo enable sync via binary logs in MySQL or MariaDB, update your
my.cnfconfiguration and create a dedicated replication user.1. Update
my.cnf:server-id = 1 log_bin = mysql-bin binlog_row_image = FULL binlog_expire_logs_seconds = 6048002. Create replication user:
CREATE USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'%'; FLUSH PRIVILEGES;Install PGSync via pip
mainInstall PGSync from PyPI using pip to begin syncing data from relational databases to search engines.
pip install pgsyncRun PGSync from a Python script
mainTo integrate PGSync into a Python project, import the
syncmodule from thepgsyncpackage and callsync.main(). This serves as the entry point to initiate the synchronization process.from pgsync import sync sync.main()Configure MySQL / MariaDB for binary logging
mainTo use PGSync with MySQL or MariaDB, enable binary logging in your
my.cnfand create a replication user.1. Update
my.cnf:server-id = 1 log_bin = mysql-bin binlog_row_image = FULL binlog_expire_logs_seconds = 6048002. Create replication user:
CREATE USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'%'; FLUSH PRIVILEGES;server-id = 1 log_bin = mysql-bin binlog_row_image = FULL binlog_expire_logs_seconds = 604800CREATE USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'%'; FLUSH PRIVILEGES;Configure PostgreSQL for logical replication
mainTo use PGSync with PostgreSQL, you must enable logical decoding in your
postgresql.conffile:wal_level = logical max_replication_slots = 1Optionally, you can limit the WAL size to prevent disk exhaustion:
max_slot_wal_keep_size = 100GBwal_level = logical max_replication_slots = 1 # Optional max_slot_wal_keep_size = 100GBConfigure PostgreSQL for logical decoding
mainTo use real-time sync via logical decoding in PostgreSQL, you must update your
postgresql.conffile with the following settings:wal_level = logicalmax_replication_slots = 1(or higher depending on your needs)
wal_level = logical max_replication_slots = 1Run PGSync with Docker Compose
mainPGSync provides Docker Compose configurations for quick local development.
For Elasticsearch + Kibana (Default):
git clone https://github.com/toluaina/pgsync cd pgsync docker-compose upFor OpenSearch:
docker-compose --profile opensearch upDefault Ports:
- PostgreSQL:
15432 - Elasticsearch:
9201 - Kibana:
5601 - OpenSearch:
9400(when using theopensearchprofile)
# Default (Elasticsearch + Kibana) git clone https://github.com/toluaina/pgsync cd pgsync docker-compose up # For OpenSearch docker-compose --profile opensearch up- PostgreSQL:
Bootstrap and run PGSync
mainTo set up your sync environment, follow these two steps:
- Bootstrap: Run this once to set up database triggers and replication slots.
- Run as daemon: Start the continuous sync process.
Note: Both commands require the
--configflag pointing to your JSON schema file.