<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tolu Aina on Medium]]></title>
        <description><![CDATA[Stories by Tolu Aina on Medium]]></description>
        <link>https://medium.com/@toluaina?source=rss-4acaa0443f75------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*UYFx2F0Epvx6ZXicvpzwYg.jpeg</url>
            <title>Stories by Tolu Aina on Medium</title>
            <link>https://medium.com/@toluaina?source=rss-4acaa0443f75------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 21:44:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@toluaina/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Real-time integration of PostgreSQL with Elasticsearch with PGSync]]></title>
            <link>https://medium.com/@toluaina/real-time-integration-of-postgresql-with-elasticsearch-with-pgsync-9425ffa9b4e9?source=rss-4acaa0443f75------2</link>
            <guid isPermaLink="false">https://medium.com/p/9425ffa9b4e9</guid>
            <category><![CDATA[change-data-capture]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Tolu Aina]]></dc:creator>
            <pubDate>Mon, 13 Apr 2020 19:41:14 GMT</pubDate>
            <atom:updated>2020-09-15T20:03:58.661Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oUZ_dx2rJ2aHU8SehfZU-w.png" /></figure><p><a href="https://pgsync.com/">PGSync</a> is a change data capture tool for moving data from <a href="https://www.postgresql.org/">Postgres</a> to <a href="https://www.elastic.co/">Elasticsearch</a>. It allows you to keep Postgres as your source-of-truth and expose structured denormalized documents in <a href="https://www.elastic.co/">Elasticsearch</a>.</p><p>This can be useful for building Backend services for driving text search applications or building real-time dashboard applications.<br>Changes to nested entities are propagated to <a href="https://www.elastic.co/">Elasticsearch</a>.<br>PGSync’s advanced query builder then generates SQL queries based on your schema.</p><p>Simply describe your schema in JSON and PGSync will continuously capture changes in your data and load it into <a href="https://www.elastic.co/">Elasticsearch</a>.<br>PGSync provides a self-managed solution for change data capture.</p><h3>Why?</h3><p>At a high level, you have data in a Postgres database and you want to expose it in <a href="https://www.elastic.co/">Elasticsearch</a>. This means every change to your data needs to be replicated to Elasticsearch.</p><p>At first, this seems easy and then it’s not. Simply add some code to copy the data to <a href="https://www.elastic.co/">Elasticsearch</a> after updating the database or perform the so-called dual writes at your application level. Writing SQL queries spanning multiple tables and involving multiple relationships can be nontrivial. Detecting changes within nested documents can also be quite hard. <br>Of course, if your data never changed, then you could just take a snapshot in time and load it into <a href="https://www.elastic.co/">Elasticsearch</a>. Keep in mind, you shouldn’t really store your primary data in <a href="https://www.elastic.co/">Elasticsearch</a>.<br>Elasticsearch is more suited as a secondary denormalized search engine to be used alongside a traditional normalized datastore.</p><p>One of the challenges is getting the data out of the source of truth and into the secondary store in a reasonable timeframe. <br>Existing tools such as Apaches’ Kafka, Amazons’ Kinesis or Elastics’ Logstash require a fair amount of engineering and expertise.</p><h3>How it works</h3><p><a href="https://pgsync.com/">PGSync</a> leverages the <a href="https://www.postgresql.org/docs/current/logicaldecoding.html">logical decoding</a> feature of Postgres introduced in PostgreSQL 9.4 to capture a continuous stream of change events.</p><p>PGSync’s query builder is capable of building advanced relational queries dynamically from your schema.</p><p>Simply, define a schema (JSON) describing the structure of your data in Elasticsearch, bootstrap the databases and start the PGSync daemon.</p><p>It operates both a polling and an event-driven model to capture changes made to date and notification for changes that occur at a point in time. The initial sync polls the database for changes since the last iteration and thereafter reverts to event notifications (based on triggers and handled by <a href="https://www.postgresql.org/docs/current/sql-notify.html">pg-notify</a>) for changes to the database.</p><p>There is no need to pollute your database with fields such as `<strong><em>updated_at</em></strong>`, `<strong><em>timestamp</em></strong>` or `<strong><em>status</em></strong>` flags to detect and track row-level changes.</p><h3>When to use PGSync</h3><p><a href="https://pgsync.com/">PGSync</a> reduces the complexity of most application stacks.</p><ul><li>Postgres is your read/write source of truth whilst Elasticsearch is your read-only search layer.</li><li>You have data that is constantly changing.</li><li>You have data in an existing relational database such as Postgres and you need a secondary NoSQL database like Elasticsearch for text-based or auto-complete queries.</li><li>You want to avoid the development overhead and complexity mandated by other tools.</li></ul><h3>Installing PGSync</h3><p>Prerequisites: <strong>Python 3.6+</strong>, <strong>Redis 3.1.0+</strong>, <strong>Elasticsearch 5.0+</strong>, <strong>PostgreSQL 9.4+.</strong></p><p>Install <a href="https://pgsync.com/">PGSync</a> from <a href="https://pypi.org/project/pgsync/">PyPi</a>.</p><pre>$ pip install pgsync</pre><p>Create a schema configuration file in JSON format:</p><p>This should mirror the structure of the resulting document in <a href="https://www.elastic.co/">Elasticsearch</a>.</p><p>Here is an example schema:</p><pre>{<br>    &quot;nodes&quot;: [<br>        {<br>            &quot;table&quot;: &quot;book&quot;,<br>            &quot;schema&quot;: &quot;public&quot;,<br>            &quot;columns&quot;: [<br>                &quot;isbn&quot;,<br>                &quot;title&quot;,<br>                &quot;description&quot;<br>            ]<br>        }<br>    ]<br>}</pre><h3>Usage</h3><pre>$ pgsync --config &lt;absolute path to JSON schema config&gt; --daemon</pre><p>More details about <a href="https://pgsync.com/">PGSync</a> can be found on its <a href="https://github.com/toluaina/pg-sync/">Github repository</a>.</p><h3>Market Alternatives</h3><ul><li><a href="https://kafka.apache.org/"><strong>Kafka</strong></a>: This is an Apache project which is an open-source stream-processing software platform.</li><li><a href="https://aws.amazon.com/kinesis/"><strong>Kinesis</strong></a>: Amazon managed service similar to Kafka.</li><li><a href="https://www.elastic.co/products/logstash"><strong>Logstash</strong></a>: This is a product of <a href="https://www.elastic.co/">Elastic</a> that collects data from various sources, then parses, transforms and ships to various destinations.</li><li><a href="https://www.zombodb.com/"><strong>ZomboDB</strong></a>: Postgres extension that provides full-text search via the use of Elasticsearch indices.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9425ffa9b4e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>