postgresqldatabase-replication

how to retain data in postgres slave after a change in master


I am using bitnami Postgres docker image for creating my master-slave database.

I am making changes to my master and serving my app using slave. my script needs to remove a table in order to recreate it with updated data. But as soon as I delete these tables, they will be removed from slave as well, and hence can cause my app to break. Is there a way to retain tables on slave until I am done recreating them?

Am I using the right approach and intuition? Any help is much appreciated Thanks


Solution

  • You can suspend replication by running this statement on the standby server:

    SELECT pg_wal_replay_pause();
    

    Then, when you are done, you can resume replication with

    SELECT pg_wal_replay_resume();
    

    Note that when you resume replication, the tables on the standby will be deleted as well. But replay will happen as fast as possible, so the time when you have no tables on the standby will be no longer than necessary.

    Concerning permissions: the documentation says:

    This function is restricted to superusers by default, but other users can be granted EXECUTE to run the function.

    So you could

    GRANT EXECUTE ON FUNCTION pg_wal_replay_pause() TO some_user;