GithubHelp home page GithubHelp logo

bechir-brahem / postgres-database-replicator Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 4.0 9 KB

replicating PostgreSQL databases using async python and RabbitMQ for availability and customizability

PLpgSQL 7.94% Python 92.06%
postgres postgresql rabbitmq replication availability

postgres-database-replicator's Introduction

Postgres Database Replicator

TLDR

this program is used to replicate a postgres server using triggers for inserts and deletes and rabbitmq to broadcast the changes to all database replicators.

How It Works

postgres supports LISTEN and NOTIFY commands. meaning that one client can listen to events on a certain 'channel' and the server can notify the listeners using the NOTIFY command.

we define SQL triggers on inserts and updates(and possibly more) so that on each change we notify the listeners about the changes done.

changes_publisher.py is a listener to these database notifications. and when it receives the incoming data it broadcasts them to replicators through rabbitmq messaging service. this way only one connection is made with the postgres server and it uses async function.

messages about changes are persisted in the rabbitmq server.

changes are then received by changes_listener.py which then commit the changes to the replica database.also using async functions.

setup

  • first run the postgres server
  • add the triggers for the tables (in this case the table is called users)
CREATE OR REPLACE FUNCTION notify_account_changes()
    RETURNS trigger AS
$$
BEGIN
    PERFORM pg_notify(
            'users_changed',
            json_build_object(
                    'table', TG_TABLE_NAME,
                    'operation', TG_OP,
                    'new_record', row_to_json(NEW),
                    'old_record', row_to_json(OLD)
                )::text
        );

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER users_changed
    AFTER INSERT OR UPDATE
    ON users
    FOR EACH ROW
EXECUTE PROCEDURE notify_account_changes();
  • start the rabbitmq server and configure the credentials in the python files
  • run pipenv install and pipenv shell (install pipenv if not found)
  • start the changes publisher python3 changes_publisher.py
  • start the change listener programs for as many replicas as you want
  • insert or update rows and observer the listener logs and the databases

postgres-database-replicator's People

Contributors

bechir-brahem avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.