I have a process (for backup and replication using binary logs) that works in MySQL that I want to replicate for Postgres databases (using WAL):
- Lock database (
FLUSH TABLES WITH READ LOCK;
) - Disable binary logging (
SET sql_log_bin = 0;
) - Flush binary logs (
FLUSH BINARY LOGS;
) - Copy binary logs and apply to a remote database for replication purposes
- Enable binary logging (
SET sql_log_bin = 1;
) - Unlock database (
UNLOCK TABLES;
)
This process works and I'm trying to do the same for Postgres. I was able to find that CHECKPOINT
is what I need to flush the binary logs for Postgres but locking and unlocking the database isn't as straightforward as I hoped it would be.
Here's what I found so far:
LOCK DATABASE
is a database-level locking mechanism but is not implementedBEGIN; LOCK TABLE users IN ACCESS EXCLUSIVE MODE NOWAIT;
seems to be able to lock the tables individually but apparently there is noUNLOCK TABLE
command because locks are always released at transaction enddefault_transaction_read_only
- I tried to look for the equivalent ofFLUSH TABLES WITH READ LOCK;
in Postgres and found this question
I tried the default_transaction_read_only
solution and it does prevent new records from being added but I'm a little worried about what the answer in there said:
This takes effect for new sessions, not for sessions that are already connected.
Also when I tried to revert the read-only mode: BEGIN read write; ALTER DATABASE my_database SET default_transaction_read_only to OFF;
it appears to have succeeded but when I try to add a new record, I get the following error:
ERROR: cannot execute INSERT in a read-only transaction
Any ideas on how I could perform the same process as I am doing for MySQL but for Postgres?